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
| name | months |
|---|---|
| Tofu | 132 |
| Gizmo | 120 |
| Bruno | 108 |
Try it: PostgreSQL playground · SQLite playground
How it works
- 1
FROM … JOIN …Pick the tables and combine their rows. Everything later works on this set of rows.
- 2
WHEREKeep the rows that pass. No aggregates, no window functions and (in standard SQL) no SELECT aliases: none exist yet.
- 3
GROUP BYCollapse the remaining rows into groups.
- 4
HAVINGKeep the groups that pass. Aggregates are available now.
- 5
SELECTCompute the output columns. Aliases are born here, and window functions run here, over the rows that are left.
- 6
DISTINCTDrop duplicate output rows.
- 7
ORDER BYSort. Aliases, aggregates and window results can all be used.
- 8
LIMIT / OFFSETKeep 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
| name | months |
|---|---|
| Bruno | 108 |
| Tofu | 132 |
| Gizmo | 120 |
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
| species | pets |
|---|---|
| dog | 8 |
| cat | 7 |
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
| name | age | rn |
|---|---|---|
| Pepper | 1 | 1 |
| Mochi | 2 | 2 |
| Pumpkin | 3 | 3 |
| Olive | 4 | 4 |
| Luna | 5 | 5 |
| Socks | 8 | 6 |
| Tofu | 11 | 7 |
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
| kind | pets |
|---|---|
| bird | 2 |
| cat | 7 |
| dog | 8 |
| rabbit | 3 |
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
| species | total |
|---|---|
| cat | 7 |
| dog | 8 |
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
- Guide: The SQL SELECT statement
- Practice: SQL SELECT practice: 7 exercises with answers
- Lesson: Filter with WHERE (free)
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.