Basic SQL Statement--sub-query

Source: Internet
Author: User

There are three sub-queries: Where sub-query, from sub-query, exist subquery. where sub-queryWhere subquery: refers to the query result of the inner layer as the comparison condition of the outer query.                                       Example: Check out the latest product: Select Good_id,good_name from good where good_id = (select Max (good_id) from good);--this sentence is a subquery Select Good_id,good_name from good order by good_id DESC limit 1; From sub-query  From subquery: The query results of the inner layer are treated as temporary tables for the outer SQL to query again. Example: Query the latest item under each column: Select Good_id,cat_id,good_name from Good where good_id in (select Max (good_id) from good Group B y cat_id);---Typical where subquery select * FROM (select Good_id,cat_id,good_name from Good order by cat_id asc,good_id de SC) as TMP GROUP BY Cat_id;--from Sub-query exists sub-queryExists sub-query: The outer query results, get the inner layer, look at the inner layer of the query is set up. Typical example: Select Cat_id,cat_name from category where exists (select * from good where good.cat_id = category.cat_id), and this subquery is now less used , has been replaced by join. Select Cat_id,cat_name from category join good on category.cat_id = good.cat_id;

Basic SQL Statement--sub-query

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.