SQL statement execution sequence and SQL statement Sequence
Unlike other programming languages, SQL is most characteristic of code processing. In programming languages with large numbers, the code is processed in the encoding order, but in SQL, the first clause to be processed is the FROM clause. Although the first SELECT statement appears, but it is almost always processed.
Each step generates a virtual table, which is used as the input for the next step. These virtual tables are unavailable to callers (client applications or external queries. Only the table generated in the last step will be returned to the caller. If a clause is not specified in the query, the corresponding steps are skipped. The following is a brief description of each logical step for SQL server 2000 and SQL Server 2005.
(8)SELECT (9)DISTINCT (11)<Top Num> <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
Introduction to the logical Query Processing Stage
Note: Step 10: sort the rows returned in the previous step BY column list in the order by clause, and return the cursor VC10. this step is the first and only step to use the column alias in the SELECT list. Unlike other steps, this step returns a cursor instead of a valid table. SQL is based on the set theory. A set does not sort its rows in advance. It is only a logical set of members, and the order of members is irrelevant. You can return an object for sorting a table, including rows organized in a specific physical order. ANSI calls this object a cursor. Understanding this step is the basis for a correct understanding of SQL.
Therefore, remember not to assume any specific sequence for the rows in the table. In other words, do not specify the order by Clause unless you are sure to ORDER the rows. Sorting requires cost. SQL Server needs to perform an ordered index scan or use a sort operator.