Pages

Saturday, January 31, 2009

SQL Query Basics

i have read one good blog which explains basics of query process. 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.

Reference:
https://msevents.microsoft.com/CUI/WebCastEventDetails.aspx?culture=en-IN&EventID=1032345689&CountryCode=IN

No comments: