sqlstreak

SQL reference

SQL order of execution: the order a query really runs in

Updated · Tested on PostgreSQL 18.6 and SQLite 3.54.1 (results generated , re-checked on every release)

Short answer

SQL runs a query in this order: FROM (with its JOINs), WHERE, GROUP BY, HAVING, SELECT (window functions included), DISTINCT, ORDER BY, LIMIT. That's why WHERE can't use a SELECT alias, an aggregate or a window function, while ORDER BY can use all three.

The task: Know which clause SQL evaluates first, and so what each clause can and can't see.

ORDER BY can use an alias

ORDER BY runs after SELECT, so the alias already exists.

SELECT name, age * 12 AS months
FROM pets
ORDER BY months DESC
LIMIT 3;

Result (PostgreSQL and SQLite, identical): 3 rows

namemonths
Tofu132
Gizmo120
Bruno108

Try it: PostgreSQL playground · SQLite playground

How it works

  1. 1
    FROM … JOIN …

    Pick the tables and combine their rows. Everything later works on this set of rows.

  2. 2
    WHERE

    Keep the rows that pass. No aggregates, no window functions and (in standard SQL) no SELECT aliases: none exist yet.

  3. 3
    GROUP BY

    Collapse the remaining rows into groups.

  4. 4
    HAVING

    Keep the groups that pass. Aggregates are available now.

  5. 5
    SELECT

    Compute the output columns. Aliases are born here, and window functions run here, over the rows that are left.

  6. 6
    DISTINCT

    Drop duplicate output rows.

  7. 7
    ORDER BY

    Sort. Aliases, aggregates and window results can all be used.

  8. 8
    LIMIT / OFFSET

    Keep a slice of the sorted rows: that's why “top 3” needs ORDER BY first.

More examples

WHERE can't (in PostgreSQL)

WHERE runs before SELECT. PostgreSQL follows the standard and doesn't know the alias yet; SQLite bends the rule and accepts it.

SELECT name, age * 12 AS months
FROM pets
WHERE months > 100;

PostgreSQLERROR: column "months" does not exist

SQLite: 3 rows

namemonths
Bruno108
Tofu132
Gizmo120

Try it: PostgreSQL playground · SQLite playground

Aggregates don't exist yet in WHERE

Groups are made after WHERE, so there is nothing to count. Both engines refuse.

SELECT species, COUNT(*) AS pets
FROM pets
WHERE COUNT(*) > 3
GROUP BY species;

PostgreSQLERROR: aggregate functions are not allowed in WHERE

SQLitemisuse of aggregate: COUNT()

Try it: PostgreSQL playground · SQLite playground

HAVING runs after GROUP BY

SELECT species, COUNT(*) AS pets
FROM pets
GROUP BY species
HAVING COUNT(*) > 3
ORDER BY pets DESC;

Result (PostgreSQL and SQLite, identical): 2 rows

speciespets
dog8
cat7

Try it: PostgreSQL playground · SQLite playground

Window functions see only the rows WHERE kept

The numbering covers the cats alone, because WHERE removed everything else first.

SELECT name, age,
       ROW_NUMBER() OVER (ORDER BY age, name) AS rn
FROM pets
WHERE species = 'cat'
ORDER BY rn;

Result (PostgreSQL and SQLite, identical): 7 rows

nameagern
Pepper11
Mochi22
Pumpkin33
Olive44
Luna55
Socks86
Tofu117

Try it: PostgreSQL playground · SQLite playground

…so they can't be filtered in WHERE

SELECT name FROM pets
WHERE ROW_NUMBER() OVER (ORDER BY age) <= 3;

PostgreSQLERROR: window functions are not allowed in WHERE

SQLitemisuse of window function ROW_NUMBER()

Try it: PostgreSQL playground · SQLite playground

Variations

Aliases in GROUP BY

Both engines also accept a SELECT alias in GROUP BY, an extension to the standard order.

SELECT species AS kind, COUNT(*) AS pets
FROM pets
GROUP BY kind
ORDER BY kind;

Result (PostgreSQL and SQLite, identical): 4 rows

kindpets
bird2
cat7
dog8
rabbit3

Try it: PostgreSQL playground · SQLite playground

Aliases in HAVING

SQLite accepts an alias in HAVING too; PostgreSQL doesn't. Repeat the expression to be safe.

SELECT species, COUNT(*) AS total
FROM pets
GROUP BY species
HAVING total > 3;

PostgreSQLERROR: column "total" does not exist

SQLite: 2 rows

speciestotal
cat7
dog8

Try it: PostgreSQL playground · SQLite playground

Logical order, not physical

This is the order SQL defines the meaning by. The planner may physically do things differently (use an index, push a filter into a join), but the result is always as if it ran in this order.

Common mistakes

  • Filtering on a SELECT alias in WHERE: repeat the expression, or compute it in a subquery or CTE first.

  • Filtering on an aggregate in WHERE: that's what HAVING is for.

  • Using LIMIT without ORDER BY for “the top N”: without a sort, which rows you get isn't defined.

PostgreSQL and SQLite

The order is the same in both. SQLite is more lenient about aliases (it accepts a SELECT alias in WHERE and HAVING); PostgreSQL follows the standard and rejects both. Both accept an alias in GROUP BY and ORDER BY. The build ran every example on both engines; the errors on this page are the real messages.

Official documentation

Go deeper

Make it stick in five minutes a day

“Filter with WHERE” teaches this step by step on a real database, with instant feedback. Free, no account needed.

Start the lesson