Execution Order:
1.FROM: Performs a cartesian product of the first two tables in the FROM clause to generate a virtual table VT1
2.ON: Apply on filter to VT1 table only rows that satisfy true are inserted vt2
3.OUTER (Join): Adds rows that are not found in the OUTER join reservation table (preserved table) as outer rows to the VT2 Generate T3 If the from contains more than two tables repeat steps and steps for the result table and the next table generated by the previous junction directly end
4.WHERE: Apply the WHERE filter to VT3 only the rows that are true are inserted VT4
5.GROUP by: Grouping rows in VT4 by the list of columns in the GROUP BY clause VT5
6.cube| ROLLUP: Inserts a Hyper-group (supergroups) into the VT6 generation VT6
7.HAVING: Apply a have filter to VT6 only the groups that are true are inserted vt7
8.SELECT: Process SELECT list Generation Vt8
9.DISTINCT: Remove duplicate rows from vt8 to produce VT9
10.ORDER by: Sorts the rows of VT9 by the column list in the ORDER BY clause to generate a cursor VC10
11.TOP: Select a specified number or proportion of rows from the beginning of VC10 to generate vt11 and return the caller
SQL Server SQL statement Execution order