Order of execution of SELECT statements in SQL statements

Source: Internet
Author: User

Logical query processing steps for the execution of the SELECT statement:

Execution order Statement
8 SELECT
9 DISTINCT
11 <TOP_specification> <select_list>
1 From <left_table>
3 <join_type> Join <right_table>
2 On <join_condition>
4 WHERE <where_condition>
5 GROUP by<group_by_list>
6 with {CUBE | ROLLUP}
7 Having
10 ORDER by <order_by_list>

Note:

1,from: Performs a Cartesian product on the first two tables in the FROM clause, generating a virtual table VT1;

2,on: Apply an on filter to the VT1. Only those that make <join_condition> true are inserted into the VT2;

3,outer (Join): If OUTER join is specified, no matching rows in the reserved table are added to VT2 as outer rows, VT3 is generated, and if the FROM clause contains more than two tables, repeat steps 1 through 3 for the result table and the next table that were generated for the previous join. Until all tables have been processed;

4, apply the where filter to the VT3. Only lines that make <where_condition> true are inserted into the VT4;

5,group by: Groups The rows in the VT4 according to the list in the GROUP BY clause, generating VT5;

6,cube | ROLLUP: Inserting the super-group into the VT5 to generate VT6;

7,having: Has a having filter applied to the VT6. Only groups that make

8,select: Process SELECT list, generate VT8;

9,distinct: Remove duplicate rows from VT8, resulting in VT9;

10,order by: The rows in the VT9 are sorted by the list in the ORDER by clause, generating the table VT10;

11,top: Selects the specified number or scale of rows from the beginning of the VT10, generates the table VT11, and returns it to the caller.

Order of execution of SELECT statements in SQL statements

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.