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.

By JaviPublished 10 min readBeginner

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.

  1. SELECT
  2. FROM
  3. JOIN
  4. WHERE
  5. GROUP BY
  6. HAVING
  7. ORDER 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:

  1. FROM
  2. JOIN
  3. WHERE
  4. GROUP BY
  5. HAVING
  6. SELECT
  7. ORDER BY
  • Written orderHow you type the query
    1. 1SELECT
    2. 2FROM
    3. 3JOIN
    4. 4WHERE
    5. 5GROUP BY
    6. 6HAVING
    7. 7ORDER BY
  • Logical processingHow the meaning is evaluated
    1. 1FROM
    2. 2JOIN
    3. 3WHERE
    4. 4GROUP BY
    5. 5HAVING
    6. 6SELECT
    7. 7ORDER BY
Written order vs logical processing order. Same clauses, different sequence. SELECT is written first but logically evaluated near the end, after the rows have been read, filtered and grouped.

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:

SQL query: Why an alias does not work in WHERET-SQL
SELECT
    FirstName + ' ' + LastName AS FullName
FROM dbo.Customers
WHERE FullName = 'Ana Torres';
T-SQL4 linesRead-only query display. It is not connected to a database.

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:

SQL query: Why an alias does not work in WHERE (2)T-SQL
-- 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';
T-SQL12 linesRead-only query display. It is not connected to a database.

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:

SQL query: Why ORDER BY can use an aliasT-SQL
SELECT
    FirstName + ' ' + LastName AS FullName
FROM dbo.Customers
ORDER BY FullName;
T-SQL4 linesRead-only query display. It is not connected to a database.

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, after GROUP BY has built them.

That is why this fails:

SQL query: WHERE vs HAVINGT-SQL
SELECT
    State,
    COUNT(*) AS CustomerCount
FROM dbo.Customers
WHERE COUNT(*) > 1
GROUP BY State;
T-SQL6 linesRead-only query display. It is not connected to a database.

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:

SQL query: WHERE vs HAVING (2)T-SQL
SELECT
    State,
    COUNT(*) AS CustomerCount
FROM dbo.Customers
GROUP BY State
HAVING COUNT(*) > 1;
T-SQL6 linesRead-only query display. It is not connected to a database.

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:

SQL query: WHERE vs HAVING (3)T-SQL
SELECT
    State,
    COUNT(*) AS CustomerCount
FROM dbo.Customers
WHERE CreatedDate >= '20250201'
GROUP BY State
HAVING COUNT(*) > 1;
T-SQL7 linesRead-only query display. It is not connected to a database.

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:

SQL query: GROUP BY comes before SELECTT-SQL
SELECT
    State,
    City,
    COUNT(*) AS CustomerCount
FROM dbo.Customers
GROUP BY State;
T-SQL6 linesRead-only query display. It is not connected to a database.

“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:

SQL query: The complete example, in logical orderT-SQL
SELECT
    State,
    COUNT(*) AS CustomerCount
FROM dbo.Customers
WHERE State IS NOT NULL
GROUP BY State
HAVING COUNT(*) > 1
ORDER BY CustomerCount DESC;
T-SQL8 linesRead-only query display. It is not connected to a database.
  1. FROM reads dbo.Customers: 10 rows.
  2. JOIN: there is none, so still 10 rows.
  3. WHERE keeps rows where State IS NOT NULL: every customer has a state, so 10 rows remain.
  4. 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.
  5. HAVING keeps groups where COUNT(*) > 1: 2 groups remain, FL and TX.
  6. SELECT builds the output for each group: the state, and its count, named CustomerCount.
  7. 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:

  1. FROM / JOIN: which rows are in play?
  2. WHERE: which of those rows survive?
  3. GROUP BY: how are the survivors grouped?
  4. HAVING: which groups survive?
  5. SELECT: what is calculated and named for each remaining row or group?
  6. 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

Question 1 of 2Which clause is written before FROM but logically evaluated after it?
Question 2 of 2Why does logical query order matter?

SQL Server Foundations

Lesson 5 of 25

Continue to next lesson

Filtering Data with WHERE in SQL Server

Continue to Lesson 6

Tags

  • SQL Server
  • SQL Fundamentals
  • SSMS
  • T-SQL