SQL Logical Query Processing Steps

Microsoft documentation – scroll to the “Logical Processing Order of the SELECT statement” of this link

Note, the information below 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.

  1. FROM clause. A cartesian product (also known as a Cartesian product) is made of the first two tables listed.
  2. ON filter is applied
  3. OUTER join (rows are added, if an OUTER join was specified).
  4. WHERE filter applied.
  5. GROUP BY
  6. CUBE or ROLLUP
  7. HAVING
  8. SELECT
  9. DISTINCT (duplicate rows are eliminated).
  10. ORDER BY (a cursor is generated).
  11. 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.