Note, this information came from Itzik Ben-Gan’s outstanding book “Inside Microsoft SQL Server 2005: T-SQL Querying”.
Each step in the processing order produces a virtual table which is passed on to the next step for further processing, as necessary.
- FROM clause. A cartesian product (also known as a Cartesian product) is made of the first two tables listed.
- ON filter is applied
- OUTER join (rows are added, if an OUTER join was specified).
- WHERE filter applied.
- GROUP BY
- CUBE or ROLLUP
- DISTINCT (duplicate rows are eliminated).
- ORDER BY (a cursor is generated).
- TOP is processed.
Results are then returned to the client.
See also this article.
Basic T-SQL code written order of Select statement Elements
SELECT ______ [List of columns]
FROM ______ [Name of Table or Rowset Provider] [This is also called a “table operator”. This is where JOINs and APPLY live.]
WHERE ______ [A condition that restricts the rows that are returned.]
GROUP BY ______ [Combines data into partitions, or “bands” of data.]
HAVING ______ [Can impose restrictions on the results of “GROUP BY”.]
ORDER BY ______ [Used to guarantee a sort order on the results.]
— Unlike most programming languages, SQL code elements are not processed in order…
— …they are processed in a logical order as described above.