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

Making Dynamic SQL Safe

Erik Darling explains patiently that if you use sp_executesql wrong, you don’t get the benefits of using it right: The gripes I hear about fully fixing dynamic SQL are: – The syntax is hard to remember (setting up and calling parameters)– It might lead to parameter sniffing issues I can sympathize with both. Trading one […]

Read More

CAST and CONVERT Make Expressions Nullable

Daniel Hutmacher points out a side effect of using CAST() and CONVERT(): Suppose we want to set up a view in the new solution that mirrors the names and definitions of the old table, so the legacy integration can use that view going forward: CREATE OR ALTER VIEW new.the_table_like_beforeASSELECT CAST(id AS varchar(32)) AS id, CAST([row] […]

Read More

Categories

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