Moon subquery SQL

Source: Internet
Author: User

Subquery definition: Using a SELECT statement inside an external SQL statement, this SELECT statement is called a subquery

Sub-query type:

Single-line subquery: Either no results are returned for external SQL, or only one row is returned. Single-row subquery special case: Is this number of queries a column, this seed query is called

"Scalar Quantum Query"

Multiline subquery: Returns one or more rows to an external SQL statement

and sub-query can be divided into:

Dolez query: Returning a queue to an external SQL statement

Associated subquery (correlated Sbuquery): associated with an external SQL statement by the same column (one or more columns referencing an external SQL statement)

Nested subqueries: In another subquery

6.2 Writing a single-line subquery (which can be placed in the WHERE/HAVING/FROM clause)

1. Place in the WHERE clause

Select First_Name, last_name

From customers

where customer_id=

(Select customer_id

From customers

where last_name= "Brown");

This subquery in the WHERE clause executes first, returns the ID of the last_name= "Brown" line, and then passes it to the external WHERE clause to execute the external query.

Use other operators: <,<=,>,>=

For example

Select product_id, Name, price

Form Products

where prict>

(select AVG (Price)

From products)

2 using subqueries in a HAVING clause

The HAVING clause acts to filter a row group and use a subquery in the HAVING clause of an outer query: The HAVING clause returns based on a subquery

The result of filtering the row group

Moon subquery SQL

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.