Search

Jul 23, 2008

Query operators evaluation

 

Hi all,

Here is the order in which query operators are evaluated. There are 11 levels.

1. FROM
2. ON
3. JOIN
4. WHERE
5. GROUP BY
6. WITH{CUBE | ROLLUP}
7. HAVING
8. SELECT
9. DISTINCT
10. HAVING
11. TOP

First the query processor reads all the rows from the FROM the left table and apply the ON condition with the right table provided in JOIN.

If there are more JOINs the same is done for all the JOINs.

Then the WHERE clause is applied to filter rows.

Then GROUP BY is done

There WITH clause get evaluated followed by HAVING.

Then the columns are selected. (This is the reason why you cannot use a column alias in WHERE or GROUP BY)

Then DISTINCT clause applied

Then ORDER BY is processed...(This is the reason why you CAN use a column alias in the ORDER BY clause)

Then TOP clause get evaluated.
Refrence: SQL Server 2005: Query processing basics. An event presented by Vinod Kumar
 

Here is the screen-shot of the video captured during the event presented by Vinod Kumar in event.


 

No comments: