Stop 05 · Aggregation & GroupingTopics · 9/9

Logical Order of Query Execution

Part of the SQL Roadmap

In short

You write SELECT first, but the database runs it fifth. Once you know the real order, errors like 'column revenue does not exist' stop being mysterious and start being obvious.

SQL is written SELECT first, but it is logically processed as FROM and JOIN, WHERE, GROUP BY, HAVING, SELECT, DISTINCT, ORDER BY, then LIMIT. Knowing this explains most errors about aliases and aggregates.

The jaadu works along a row of brass pots on a stove, moving from boiling water to crushing ginger to straining steaming chai into a line of clay kulhads.

Think of it like this

The masala chai recipe card

A recipe card says "Serve hot masala chai in kulhads" at the top, because that's the goal. But in the kitchen you boil water first, then add tea and spices, then milk, then strain, and only then pour into kulhads. SQL is written goal-first like the card, but cooked in kitchen order.

Watch it happen

The order the database really follows

FROM / JOIN
WHERE
GROUP BY
HAVING
SELECT
DISTINCT
ORDER BY
LIMIT

1/8FROM and JOIN build the full set of rows to work with.

The big ideas

What you need to know

Written order is not run order

You write SELECT, FROM, WHERE. The database works FROM, WHERE, GROUP BY, HAVING, SELECT, ORDER BY, LIMIT.

Aliases are born in SELECT

Anything that runs before SELECT (WHERE, GROUP BY, HAVING) can't see aliases in standard SQL. ORDER BY can.

Aggregates need groups

SUM and COUNT only exist from GROUP BY onwards, so they can't appear in WHERE.

Logical, not physical

The optimiser may physically do things differently, but the result always matches this logical order.

How it works

Debug a query in run order

  1. 1

    FROM

    Run SELECT * FROM orders LIMIT 20 and check the rows look right.

  2. 2

    WHERE

    Add the filters and check the row count drops the way you expect.

  3. 3

    GROUP BY

    Group and add one aggregate, then check a group by hand.

  4. 4

    HAVING

    Add the group filter and see which groups disappear.

  5. 5

    SELECT and ORDER BY

    Add the remaining columns, aliases and sorting last.

See it in code

Walk through the code

sql
1SELECT city, SUM(amount) AS revenue   -- 52FROM orders                             -- 13WHERE status = 'delivered'              -- 24GROUP BY city                           -- 35HAVING SUM(amount) > 100000             -- 46ORDER BY revenue DESC                   -- 67LIMIT 5;                                -- 7

1/4Steps 1 and 2: open the table, drop rows that aren't delivered.

Top five cities by delivered revenue, with the logical step number on each line. SQL Server uses SELECT TOP 5 instead of LIMIT.

Save yourself the pain

Common mistakes

Oops

SELECT amount * 1.18 AS with_gst FROM orders WHERE with_gst > 1000

Do this instead

WHERE runs before SELECT. Repeat the expression: WHERE amount * 1.18 > 1000.

Oops

Thinking LIMIT 10 runs before ORDER BY and grabs 10 random rows to sort.

Do this instead

ORDER BY runs first, then LIMIT keeps the top 10 of the sorted result.

Oops

Writing a whole query at once and staring at the error.

Do this instead

Build it in run order, FROM first, checking the result after each clause.

Your mission

Order from memory

~15 min

Quick check

Why does ORDER BY revenue work when WHERE revenue > 5000 fails?

Go deeper

  1. 1Write the execution order from memory and check it.
  2. 2Explain why a SELECT alias can be used in ORDER BY but not in WHERE in standard SQL.
  3. 3Use the order to debug a query: build it up clause by clause, checking the result each time.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge