The order in which a query is written is not the order in which it is evaluated by SQL Server.
5: SELECT <select list>
1: FROM <table source>
2: WHERE <search condition>
3: GROUP BY <group by list>
4: HAVING <search condition>
6: ORDER BY <order by list>
Applying the logical order of operations to writing SELECT statements
USE AdventureWork2012;
SELECT SalesPersonID, YEAR(OrderDate) AS OrderYear
FROM Sales.SalesOrderHeader
WHERE CustomerID = 29974
GROUP SalesPersonID, YEAR(OrderDate)
HAVING COUNT(*) > 1
ORDER BSalesPersonID, OrderYear;
0 Comments