SQL is required. note Chapter 1 use subquery. SQL is required.

Source: Internet
Author: User

SQL is required. note Chapter 1 use subquery. SQL is required.
11.1 subquery

Query any SQL statement is a query. However, this term generally refers to a SELECT statement.
SQL also allows you to create subqueries, that is, queries nested in other queries.

11.2 Filter Using subqueries
SELECT cust_idFROM OrdersWHERE order_num IN (SELECT order_num FROM OrderItems WHERE prod_id = 'rga01 ');

In SELECT statements, subqueries are always processed from the inside out.

SELECT cust_name, cust_contactFROM CustomersWHERE cust_id IN (SELECT cust_id FROM Orders WHERE order_num IN (SELECT order_num FROM OrderItems WHERE prod_id = 'rga01 '));

It can only be a single column: SELECT statements used as subqueries can only query a single column. An error is returned when you attempt to retrieve multiple columns.

11.3 use subquery as a calculated Field

To execute COUNT (*) for each customer, it should be used as a subquery.

SELECT cust_name, cust_state, (select count (*) FROM Orders WHERE Orders. cust_id = MERs. cust_id) AS ordersFROM CustomersORDER BY cust_name;

Related Article

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.