SQL Server
SQL Query Order: Written Order vs Logical Processing Order
You write SELECT first, but SQL Server logically starts with FROM. Learn the logical processing order of a SELECT statement and how it explains aliases, WHERE vs HAVING and GROUP BY errors.
On this page
In the previous lesson you learned the clauses of a SELECT statement and the order you must write them in. That order starts with SELECT. It is natural to assume SQL Server reads the query the same way: first the columns, then the table, then the filter.
It does not. This lesson, the fifth step of SQL Server Foundations, shows the order in which the clauses are logically processed, and why knowing it turns several confusing errors into obvious ones.
Two orders for the same query
The written order is the syntax: the order the clauses must appear in your query.
SELECTFROMJOINWHEREGROUP BYHAVINGORDER BY
The logical processing order is the order in which the clauses are evaluated conceptually, each one working on the result of the previous one:
FROMJOINWHEREGROUP BYHAVINGSELECTORDER BY
- Written orderHow you type the query
- 1SELECT
- 2FROM
- 3JOIN
- 4WHERE
- 5GROUP BY
- 6HAVING
- 7ORDER BY
- Logical processingHow the meaning is evaluated
- 1FROM
- 2JOIN
- 3WHERE
- 4GROUP BY
- 5HAVING
- 6SELECT
- 7ORDER BY
Read the logical order as a story. First SQL Server decides where the rows come from (FROM, JOIN). Then it throws away rows that do not match (WHERE). Then it groups what is left (GROUP BY) and throws away groups that do not match (HAVING). Only then does it build the output columns (SELECT), and finally it sorts them (ORDER BY).
A logical model, not the physical execution plan
This is the most important caveat in the lesson. The logical processing order describes what a query means: which names are visible in which clause and what each step receives. It is not a description of what SQL Server physically does, step by step, when it runs your query.
The physical work is decided by the query optimizer, which can read tables in a different order, apply filters earlier, use indexes, or combine steps, as long as the result is the same as the logical model says. Microsoft’s documentation makes the same distinction: the actual physical execution order is determined by the query processor and can vary. That is a topic for the Performance & Tuning side of the site. For writing correct queries, the logical model is the one to keep in your head.
Why an alias does not work in WHERE
In the previous lesson you gave a calculated column a name with AS. Suppose you want to find one customer by that full name:
SELECT
FirstName + ' ' + LastName AS FullName
FROM dbo.Customers
WHERE FullName = 'Ana Torres';SQL Server answers with “Invalid column name ‘FullName’.”
The logical order explains it. WHERE is evaluated in step 3. The alias FullName is created in SELECT, which is step 6. When WHERE runs, FullName does not exist yet; the only names available are the columns of dbo.Customers.
Two ways to write it correctly:
-- Filter on the real columns.
SELECT
FirstName + ' ' + LastName AS FullName
FROM dbo.Customers
WHERE FirstName = 'Ana'
AND LastName = 'Torres';
-- Or repeat the expression in WHERE.
SELECT
FirstName + ' ' + LastName AS FullName
FROM dbo.Customers
WHERE FirstName + ' ' + LastName = 'Ana Torres';Both return Ana Torres. The first is usually better: it compares plain columns, which is simpler to read and, as you will see in the performance lessons much later, easier for SQL Server to use indexes with.
Why ORDER BY can use an alias
Now sort by the same alias:
SELECT
FirstName + ' ' + LastName AS FullName
FROM dbo.Customers
ORDER BY FullName;This works. ORDER BY is step 7, after SELECT has created FullName, so the alias is visible. The ten customers come back sorted by full name, from Ana Torres to Jonas Weber.
The same rule tells you why GROUP BY cannot use a SELECT alias in SQL Server either: GROUP BY is step 4, before the alias exists.
WHERE vs HAVING
Both clauses filter, but at different moments in the logical order, and on different things.
WHERE(step 3) filters rows, before any grouping.HAVING(step 5) filters groups, afterGROUP BYhas built them.
That is why this fails:
SELECT
State,
COUNT(*) AS CustomerCount
FROM dbo.Customers
WHERE COUNT(*) > 1
GROUP BY State;SQL Server reports that an aggregate may not appear in the WHERE clause. At step 3 there are no groups yet, so there is nothing to count per state. Move the condition to HAVING, which runs after the groups exist:
SELECT
State,
COUNT(*) AS CustomerCount
FROM dbo.Customers
GROUP BY State
HAVING COUNT(*) > 1;It returns FL with 3 and TX with 2.
The two can work together. A condition on individual rows belongs in WHERE; a condition on the group belongs in HAVING:
SELECT
State,
COUNT(*) AS CustomerCount
FROM dbo.Customers
WHERE CreatedDate >= '20250201'
GROUP BY State
HAVING COUNT(*) > 1;WHERE first removes Ana Torres, the only customer created before February 2025. Florida is left with two customers and Texas with two, so both groups still pass HAVING. Filtering rows early in WHERE is also the natural thing to do: there is no reason to group rows you are going to discard.
GROUP BY comes before SELECT
Because GROUP BY (step 4) runs before SELECT (step 6), by the time SELECT builds the output there are no individual customers any more, only groups. That is the reason behind this error:
SELECT
State,
City,
COUNT(*) AS CustomerCount
FROM dbo.Customers
GROUP BY State;“Column ‘dbo.Customers.City’ is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.” The Florida group holds Miami, Orlando and Tampa. SELECT receives one group and is asked for one city, and there is no correct answer. Either group by city as well (GROUP BY State, City, one row per state and city) or summarize it with an aggregate, such as COUNT(DISTINCT City).
The complete example, in logical order
Take the complete query from the previous lesson and follow it through the seven logical steps, counting rows as you go:
SELECT
State,
COUNT(*) AS CustomerCount
FROM dbo.Customers
WHERE State IS NOT NULL
GROUP BY State
HAVING COUNT(*) > 1
ORDER BY CustomerCount DESC;- FROM reads
dbo.Customers: 10 rows. - JOIN: there is none, so still 10 rows.
- WHERE keeps rows where
State IS NOT NULL: every customer has a state, so 10 rows remain. - GROUP BY turns those 10 rows into 7 groups: FL with 3 customers, TX with 2, and WA, CO, OR, MA and IL with 1 each.
- HAVING keeps groups where
COUNT(*) > 1: 2 groups remain, FL and TX. - SELECT builds the output for each group: the state, and its count, named
CustomerCount. - ORDER BY sorts by
CustomerCount, largest first: FL (3), then TX (2).
Two details in that query make sense only in logical order. HAVING repeats COUNT(*) instead of using the alias: HAVING CustomerCount > 1 fails with “Invalid column name ‘CustomerCount’”, because step 5 runs before step 6 creates the name. ORDER BY, at step 7, can use CustomerCount, because by then it exists. Once you see the steps, the rule stops being something to memorize: a name can be used only in a step that comes after the step that creates it.
Putting the two orders together
When a query fails or returns something surprising, walk through it in logical order instead of reading it top to bottom:
- FROM / JOIN: which rows are in play?
- WHERE: which of those rows survive?
- GROUP BY: how are the survivors grouped?
- HAVING: which groups survive?
- SELECT: what is calculated and named for each remaining row or group?
- ORDER BY: in what order is the result returned?
Two more details fit into this model: DISTINCT is applied right after SELECT, and TOP is applied last, after ORDER BY, which is why TOP with ORDER BY gives you a predictable set of rows.
Next step
You now know two orders: the one you write and the one that gives a query its meaning. The next lesson, Filtering Data with WHERE in SQL Server, goes deep on step 3: comparisons, AND and OR, IN, BETWEEN, LIKE and NULL.
The complete path is on the SQL Server Foundations hub.
Check what you learned
2 quick questions
SQL Server Foundations
Lesson 5 of 25
Was this helpful?
What could be better?
Thanks — that helps me improve Javi Builds.
Continue learning
- Cheat sheetFind High Read QueriesIdentify SQL Server queries performing the most logical and physical reads, separate read-heavy workload from CPU-heavy workload, and choose the right next tuning step.Open cheat sheet
- RoadmapSQL Server FoundationsYou write SELECT first, but SQL Server logically starts with FROM. Learn the logical processing order of a SELECT statement and how it explains aliases, WHERE vs HAVING and GROUP BY errors.View roadmap
What should I build next?
I'm building Javi Builds around practical problems data engineers actually face. Tell me what you'd like to see next.
Stay in the loop
Get the Javi Builds updates you actually care about.
Check your inbox
I sent you a confirmation link. Your subscription will be active after you confirm it.