Programmer's Rules

SQL Server

The order SQL Server evaluates clauses in Transact-SQL statements

The order in which SELECT statement is evaluated and processed

SQL Serverjob interviewTransact-SQL70-761

Let's take into consideration the following statement:

SQL Server evaluates the above statement in thew following way:

1. The FROM clause is evaluated first and a virtual table is created in order to provide rows for the rest of the statement.

2. WHERE is next, filtering rows and filtered virtual table is passed to the next step.

3. GROUP BY is the next step, organizing the rows in the virtual table according to unique values found in the GROUP BY list.

4. The HAVING clause is next to be evaluated filtering whole groups.

5. Next the SELECT clause is executed determining which columns will appear in the query result.

6. Finally, the ORDER BY clause is executed, sorting the rows.

Tutorial statistics