Friday, April 10, 2015

Order of Execution of SQL Query

It is very easy to understand the flow of query execution order. Here I have given a simple sql query for example. Please go through the query first then look into Execution Order


Select Distinct Top 10 Tbl1.Col1, Tbl2.Col2 from Tbl1
Inner join Tbl2 on Tbl1.Col1=Tbl2.Col1
Where Tbl1.col2=2Group by Tbl1.Col1,Tbl2.Col1
Having Tbl2.Col1>20Order by Tbl2.Col1

Here is the order of Execution
  1. FROM
  2. ON
  3. JOIN (1. CROSS, 2. INNER, 3. OUTER), APPLY (1. CROSS, 2. OUTER), PIVOT, UNPIVOT
  4. WHERE
  5. GROUP BY
  6. HAVING
  7. SELECT
  8. DISTINCT
  9. ORDER BY
  10. TOP
— got reference from Inside Microsoft SQL server 2008 T-sql querying

No comments:

Post a Comment