30

I have below SQL Query :

SELECT TOP 5 C.CustomerID,C.CustomerName,C.CustomerSalary
FROM Customer C
WHERE C.CustomerSalary > 10000
ORDER BY C.CustomerSalary DESC

What will be execution order of the following with proper explanation ?

  1. TOP Clause
  2. WHERE Clause
  3. ORDER BY Clause
Md. Suman Kabir
  • 5,243
  • 5
  • 25
  • 43
Manoj
  • 863
  • 1
  • 8
  • 12

8 Answers8

64

Check out the documentation for the SELECT statement, in particular this section:

Logical Processing Order of the SELECT statement

The following steps show the logical processing order, or binding order, for a SELECT statement. This order determines when the objects defined in one step are made available to the clauses in subsequent steps. For example, if the query processor can bind to (access) the tables or views defined in the FROM clause, these objects and their columns are made available to all subsequent steps. Conversely, because the SELECT clause is step 8, any column aliases or derived columns defined in that clause cannot be referenced by preceding clauses. However, they can be referenced by subsequent clauses such as the ORDER BY clause. Note that the actual physical execution of the statement is determined by the query processor and the order may vary from this list.

which gives the following order:

FROM
ON
JOIN
WHERE
GROUP BY
WITH CUBE or WITH ROLLUP
HAVING
SELECT
DISTINCT
ORDER BY
TOP
Fabricio Araujo
  • 3,810
  • 3
  • 28
  • 43
Brian Agnew
  • 268,207
  • 37
  • 334
  • 440
  • 15
    +1 You should emphasise **that the actual physical execution of the statement is determined by the query processor and the order may vary from this list**. In practice I'd expect the order to most likely be `TOP`, `ORDER BY`, `WHERE` but noting that each step depends on the successive step so while `TOP` would **start** before `ORDER BY` which would **start** before `WHERE`, in practice `WHERE` would **finish** before `ORDER BY` which would **finish** before `TOP`. Read the execution plan: it goes from left to right. – ta.speot.is Jul 01 '13 at 12:00
  • The `ON` should not be on this list. It's used in the `FROM` clause but it doesn't stand on its own. – ypercubeᵀᴹ Jul 01 '13 at 15:17
  • See this **[poster](http://www.sql.co.il/books/insidetsql2008/Logical%20Query%20Processing%20Poster.pdf)** by [Itzik Ben-Gan](http://tsql.solidq.com/about.htm), a SQL-Server MVP with many published books about SQL-Server. (and even this poster has a small mistake, putting `TOP` before `ORDER BY`.) – ypercubeᵀᴹ Jul 01 '13 at 15:32
  • [Here is a good example](http://stackoverflow.com/questions/16487264/i-still-get-a-arithmetic-overflow-when-i-filter-on-a-cast-datetime-even-if-i-u) of how the order is not so set in stone (its a question of mine where I was getting bit by a cast happening before a filter). – Scott Chamberlain Jul 01 '13 at 16:01
  • does this Logical Processing Order of the SELECT statement apply to MySQL? – Don Cheadle Dec 19 '14 at 16:10
15
  1. WHERE
  2. ORDER BY
  3. TOP

Here is a good article about that: http://blog.sqlauthority.com/2009/04/06/sql-server-logical-query-processing-phases-order-of-statement-execution/

Andrey Gordeev
  • 30,606
  • 13
  • 135
  • 162
12

Simply remember this phrase:- Fred Jones' Weird Grave Has Several Dull Owls

Take the first letter of each word, and you get this:-

FROM
(ON)
JOIN
WHERE
GROUP BY
(WITH CUBE or WITH ROLLUP)
HAVING
SELECT
DISTINCT
ORDER BY
TOP

Hope that helps.

11

This is exact execution order, with your case.

1-FROM
2-WHERE
3-SELECT 
4-ORDER BY
5-TOP
Muhammad
  • 3,169
  • 5
  • 41
  • 70
  • is this the general execution order for all SQL/RDBMS? I know every RDBMS does it's own optimization, but can we at least depend on this logical order of steps when making rows available? – Don Cheadle Dec 19 '14 at 15:58
6

TOP, WHERE, and ORDER BY are not "executed" - they simply describe the desired result and the database query optimizer determines (hopefully) the best plan for the actual execution. The separation between "declaring the desired result" and how it is physically achieved is what makes SQL a "declarative" language.

Assuming there is an index on CustomerSalary, and the table is not clustered, your query will likely be executed as an index seek + table heap access, as illustrated in this SQL Fiddle (click on View Execution Plan at the bottom):

enter image description here

As you can see, first the correct CustomerSalary value is found through the Index Seek, then the row that value belongs to is retrieved from the table heap through RID Lookup (Row ID Lookup). The Top is just for show here (and has 0% cost), as is the Nested Loops for that matter - the starting index seek will return (at most) one row in any case. The whole query is rather efficient and will likely cost only a few I/O operations.

If the table is clustered, you'll likely have another index seek instead of the table heap access, as illustrated in this SQL Fiddle (note the lack of NONCLUSTERED keyword in the DDL SQL):

enter image description here

But beware: I was lucky this time to get the "right" execution plan. The query optimizer might have chosen a full table scan, which is sometimes actually faster on small tables. When analyzing query plans, always try to do that on realistic amounts of data!

Branko Dimitrijevic
  • 50,809
  • 10
  • 93
  • 167
3

Visit https://msdn.microsoft.com/en-us/library/ms189499.aspx for a better explanation.

The following steps show the logical processing order, or binding order, for a SELECT statement. This order determines when the objects defined in one step are made available to the clauses in subsequent steps. For example, if the query processor can bind to (access) the tables or views defined in the FROM clause, these objects and their columns are made available to all subsequent steps. Conversely, because the SELECT clause is step 8, any column aliases or derived columns defined in that clause cannot be referenced by preceding clauses. However, they can be referenced by subsequent clauses such as the ORDER BY clause. Note that the actual physical execution of the statement is determined by the query processor and the order may vary from this list.

FROM

ON

JOIN

WHERE

GROUP BY

WITH CUBE or WITH ROLLUP

HAVING

SELECT

DISTINCT

ORDER BY

TOP

2

My $0,02 here.

There's two different concepts in action here: the logical execution order and the plan of query execution. An other was to see it is who answers the following questions:

  1. How MSSQL understood my SQL Query?
  2. What it'll do to execute it in the best possible way given the current schema and data?

The first question is answered by the logical execution order. Brian's answer show what it is. It's the way SQL understood your command: "FROM Customer table (aliased as C) consider only the rows WHERE the C.CustomerSalary > 10000, ORDER them BY C.CustomerSalary in descendent order and SELECT the columns listed for the TOP 5 rows". The resultset will obey that meaning

The second question's answer is the query execution plan - and it depends on your schema (table definitions, selectivity of data, quantity of rows in the customer table, defined indexes, etc) since is heavily dependant of SQL Server optimizer internal workings.

Community
  • 1
  • 1
Fabricio Araujo
  • 3,810
  • 3
  • 28
  • 43
0

Here is the complete sequence for sql server :

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

So from the above list, you can easily understand the execution sequence of TOP, WHERE and ORDER BY which is :

1.  WHERE
2.  ORDER BY
3.  TOP

Get more information about it from Microsoft

Md. Suman Kabir
  • 5,243
  • 5
  • 25
  • 43