Order Of Execution With SQL Server

Kevin Feasel

2017-12-21

Syntax

Andrew Tobin explains the order of how a SQL statement gets processed:

For a particular query you may have the following components and they act in this order:

1 FROM / JOIN
2 WHERE
3 GROUP BY
4 HAVING
5 SELECT
6 ORDER BY
7 TOP / OFFSET-FETCH

Read on for more details.  This is why, for example, you can use an aliased column or calculation in the ORDER BY clause but not in the WHERE clause.

Related Posts

Indexed View Matching

Erik Darling has a series of posts on indexed views, with the latest covering query matching even when using a keyword in creation of the indexed view itself: There are a whole bunch of limitations in creating indexed views. One of them is that you can’t base the query on DISTINCT. Fair enough, but you can do […]

Read More

Adding Constraints In The CREATE TABLE Statement

Steve Jones shows how you can add constraints in your CREATE TABLE statement: A good habit to get into is to explicitly name your constraints. I try to do this when I create tables to be sure that a) I have a PK and b) it’s named the same for all environments. I can create […]

Read More

Categories

December 2017
MTWTFSS
« Nov Jan »
 123
45678910
11121314151617
18192021222324
25262728293031