In which order does SQL Server process SQL statements
The main SQL/Select statement is:
SELECT DISTINCT TOP (list) FROM (LeftTable) (join type) JOIN (RightTable) ON (condition]) WHERE (condition) GROUP BY (list) WITH (CUBE | ROLLUP) HAVING (condition) ORDER BY (list)
- FROM (LeftTable)
- ON (condition)
- (join type) JOIN (RightTable)
- WHERE (condition)
- GROUP BY (list)
- WITH (CUBE | ROLLUP)
- HAVING (condition)
- ORDER BY (list)
- TOP (list)
- SQL first evaluate the FROM files.
- Then the conditions of the FROMs.
- Then how it will group/summaries...
- Then finally the SELECT statement.
This means that everything has to be defined in the order of evaluation by SQL (ie: aliases, calculated fields...)