SELECT statement – Order of Execution of SQL Clauses

SELECT statements retrieves rows from the database and enables the selection of one or many rows or columns from one or many tables. They do not modify data in any way and are simply a method of looking at the data stored in a single table or in many related tables.

SELECT statements do not generate a transaction in Microsoft SQL Server either.

Syntax of the SELECT statement

SELECT [ ALL | DISTINCT ]
[TOP ( expression ) [PERCENT] [ WITH TIES ] ]
< select_list >
[ INTO new_table ]
[FROM {table_name | view_name}[(optimizer_hints)]
[[, {table_name2 | view_name2}[(optimizer_hints)]
[..., {table_name16 | view_name16}[(optimizer_hints)]]]
[WHERE <search_condition>]
[GROUP BY clause]
[HAVING <search_condition>]
[ORDER BY clause]
[COMPUTE clause]
[FOR BROWSE]

Logical Processing Order of the SELECT statement


The order execution of the clauses in the SELECT statement is significant. Given below is the order in which SQL clauses get executed:
  1. FROM
  2. ON
  3. JOIN
  4. WHERE
  5. GROUP BY
  6. WITH CUBE or WITH ROLLUP
  7. HAVING
  8. SELECT
  9. DISTINCT
  10. ORDER BY
  11. TOP

No comments:

Post a Comment