TSQL - What Is Logical Query Processing Order In SQL Server

The way you write TSQL query in SSMS ( SQL Server Management Studio) or any other Editor that is going to be executed on SQL Server does not run in the order it is written in. When we write query we start with Select, Update or Delete and then use conditions etc. SQL Server take this query and evaluates order of the query in different order in which it is written. The way SQL Server evaluates the order of execution is called Logical Query Processing Order.

Let's say we have a simple query


SELECT Name, SUM(Sale) AS TotalSale
FROM Dbo.Sale
WHERE Name LIKE 'A%'
GROUP BY Name
HAVING SUM(Sale)>100
ORDER BY Name DESC

The order of our above query is
1
Select
2
From
3
Where
4
Group by
5
Having
6
Order By
Fig 1: Written Query Order

But when SQL Server is going to evaluate this query, It will not start with Select statement. Below is the Logical Query Processing Order for above query

1
From  (Table or Tables)
2
Where (Search Criteria)
3
Group By ( Group by Column/s)
4
Having ( Search or Filter Criteria)
5
Select ( Select list of columns)
6
Order By ( Order by column/s)
Fig 2: Logical Query Processing Order

As we can see that the order in which query is written is not the order in which it will be evaluated/executed by SQL Server. Fig 1 , the start point of query is Select but in Logical query processing order ( Fig 2) is From clause.