SQL order of execution: how the database reads your query
You type SELECT first, but the database gets to it halfway through. Once you know the real order, a lot of puzzling errors make sense: why WHERE refuses COUNT, why an alias works in ORDER BY but not in WHERE, and when HAVING is the right tool.
In short: you write SELECT … FROM … WHERE … GROUP BY … HAVING … ORDER BY … LIMIT. The logical order of evaluation is FROM (with JOIN), WHERE, GROUP BY, HAVING, SELECT, DISTINCT, ORDER BY, LIMIT. Each step only sees what the steps before it produced.
Before you start: GROUP BY, HAVING and ORDER BY. You'll only need JOIN for the third exercise.
Try it now
A typical request: who handled how many delivered orders? Before you type, decide whether the status check has to happen before or after counting.
Task Only delivered orders count toward this quarter's bonus. Show employee_id and the number of orders with status 'delivered' that each person handled.
orders 18 rows, 5 columns
| order_id | customer_id | order_date | status | employee_id |
|---|---|---|---|---|
| 1001 | 1 | 2026-01-08 | delivered | 3 |
| 1002 | 4 | 2026-01-12 | delivered | 4 |
| 1003 | 3 | 2026-01-20 | delivered | 3 |
| 1004 | 7 | 2026-02-03 | delivered | 4 |
| 1005 | 2 | 2026-02-11 | cancelled | 8 |
| 1006 | 1 | 2026-02-25 | delivered | 3 |
| 1007 | 10 | 2026-03-04 | delivered | 2 |
| 1008 | 5 | 2026-03-17 | delivered | 4 |
| 1009 | 8 | 2026-03-30 | delivered | 3 |
| 1010 | 6 | 2026-04-07 | delivered | 8 |
| 1011 | 12 | 2026-04-22 | delivered | 4 |
| 1012 | 3 | 2026-05-06 | delivered | 3 |
| 1013 | 9 | 2026-05-19 | cancelled | 8 |
| 1014 | 7 | 2026-06-02 | shipped | 4 |
| 1015 | 1 | 2026-06-10 | shipped | 3 |
| 1016 | 11 | 2026-06-12 | open | NULL |
| 1017 | 4 | 2026-06-13 | open | NULL |
| 1018 | 10 | 2026-06-14 | open | NULL |
The order the database works in
On the left is the order you write the clauses in. On the right is the order the database evaluates them in, logically speaking. Follow the lines to see where each clause ends up.
- FROM and JOIN: the database fetches the tables and joins them into one big working table.
- WHERE: tests every row on its own and throws away the ones that fail. There are no groups yet.
- GROUP BY: collapses the remaining rows into groups. From here on, COUNT, SUM and friends work per group.
- HAVING: tests whole groups, for example
COUNT(*) >= 2. - SELECT: computes the columns you asked for and applies the AS names.
- DISTINCT: removes duplicate result rows (more on DISTINCT).
- ORDER BY: sorts the finished result. The SELECT names are known by now.
- LIMIT: trims the list at the very end.
This is the logical order. Internally the optimizer may take shortcuts, as long as the result is the same. Both Microsoft's and SQLite's documentation point this out. For reading results and error messages, though, the logical order is the one that matters.
One query, step by step
Purchasing asks: which categories have at least two products under 100, and what is the most expensive of them?
SELECT category, COUNT(*) AS products, MAX(price) AS top_price
FROM products
WHERE price < 100
GROUP BY category
HAVING COUNT(*) >= 2
ORDER BY top_price DESC;
| category | products | top_price |
|---|---|---|
| Printer supplies | 2 | 89.9 |
| Writing | 2 | 6.99 |
| Paper | 2 | 5.49 |
FROM starts with all 12 products. WHERE keeps the 7 that cost less than 100, so Furniture and Electronics are gone before any grouping happens. GROUP BY turns those 7 rows into 4 groups: Filing, Paper, Printer supplies and Writing. HAVING drops Filing, which has only one product left. Only now does SELECT work out products and top_price, and ORDER BY can sort by that name.
Why an alias works in ORDER BY but not in WHERE
An alias is born in step 5, in SELECT. ORDER BY runs later and can use it. WHERE runs in step 2, before the alias exists. This version works in MySQL, PostgreSQL and SQL Server alike:
SELECT name, price * stock AS stock_value
FROM products
WHERE price * stock > 2000
ORDER BY stock_value DESC;
| name | stock_value |
|---|---|
| 27-inch monitor | 4185 |
| Comfort office chair | 2268 |
| Standing desk | 2245 |
| Copy paper, 500 sheets | 2095.8 |
WHERE repeats the calculation; ORDER BY just uses the name. The exercises on this site run on SQLite, which happens to accept WHERE stock_value > 2000 as well. Don't get used to it: PostgreSQL, SQL Server and MySQL all reject that exact query.
More practice: filtering before and after grouping
This one needs both kinds of filter. One tests single products, the other tests whole categories.
Task Purchasing wants to know which categories have at least two products in stock (stock greater than 0). Show category and the number of those products, only for categories with at least two of them.
products 12 rows, 5 columns
| product_id | name | category | price | stock |
|---|---|---|---|---|
| 1 | Copy paper, 500 sheets | Paper | 4.99 | 420 |
| 2 | Blue ballpoint pens, 10-pack | Writing | 3.49 | 310 |
| 3 | Lever arch file, gray | Filing | 2.79 | 185 |
| 4 | Comfort office chair | Furniture | 189 | 12 |
| 5 | Standing desk | Furniture | 449 | 5 |
| 6 | Black ink cartridge | Printer supplies | 24.9 | 64 |
| 7 | Yellow sticky notes, 12 pads | Writing | 6.99 | 0 |
| 8 | Mono laser printer L30 | Electronics | 219 | 8 |
| 9 | 27-inch monitor | Electronics | 279 | 15 |
| 10 | Notebook A5, ruled | Paper | 5.49 | 140 |
| 11 | Black toner XL | Printer supplies | 89.9 | 22 |
| 12 | Cross-cut shredder | Electronics | 129 | 0 |
This exercise is part of Premium.
Premium unlocks every exercise in every lesson, unlimited practice with fresh tasks and the final test with certificate.
See Premium Already Premium? Log in
The first exercise in this lesson is free.
To finish, the whole pipeline in one query: join, filter, group, compute, sort, cut.
Task Management wants to see the three customers with the highest revenue, counting delivered orders only (status 'delivered'). Show name and revenue (the sum of quantity times unit_price), highest revenue first.
customers 12 rows, 7 columns
| customer_id | name | city | country | customer_since | segment | phone |
|---|---|---|---|---|---|---|
| 1 | Harbor Fremont Partners | Boston | USA | 2021-03-15 | Business | 617-555-0142 |
| 2 | Emily Carter | Chicago | USA | 2022-07-01 | Consumer | NULL |
| 3 | Riverside County Office | Riverside | USA | 2020-11-20 | Government | 951-555-0110 |
| 4 | Raj Patel | Leeds | UK | 2023-01-09 | Consumer | 0113 496 0123 |
| 5 | Lakeview Hotel Ltd | Manchester | UK | 2022-04-18 | Business | 0161 496 0456 |
| 6 | Olivia Brooks | Austin | USA | 2024-02-27 | Consumer | NULL |
| 7 | Greene & Shaw LLP | London | UK | 2021-09-05 | Business | 020 7946 0321 |
| 8 | Maple Grove High School | Austin | USA | 2023-08-14 | School | 512-555-0187 |
| 9 | Chloe Evans | Boston | USA | 2025-05-30 | Consumer | 617-555-0199 |
| 10 | Northline Printing Co | Toronto | Canada | 2020-06-12 | Business | 416-555-0133 |
| 11 | Daniel Kim | Seattle | USA | 2024-10-03 | Consumer | NULL |
| 12 | Westbrook Dental Clinic | Chicago | USA | 2022-12-01 | Business | 312-555-0164 |
orders 18 rows, 5 columns
| order_id | customer_id | order_date | status | employee_id |
|---|---|---|---|---|
| 1001 | 1 | 2026-01-08 | delivered | 3 |
| 1002 | 4 | 2026-01-12 | delivered | 4 |
| 1003 | 3 | 2026-01-20 | delivered | 3 |
| 1004 | 7 | 2026-02-03 | delivered | 4 |
| 1005 | 2 | 2026-02-11 | cancelled | 8 |
| 1006 | 1 | 2026-02-25 | delivered | 3 |
| 1007 | 10 | 2026-03-04 | delivered | 2 |
| 1008 | 5 | 2026-03-17 | delivered | 4 |
| 1009 | 8 | 2026-03-30 | delivered | 3 |
| 1010 | 6 | 2026-04-07 | delivered | 8 |
| 1011 | 12 | 2026-04-22 | delivered | 4 |
| 1012 | 3 | 2026-05-06 | delivered | 3 |
| 1013 | 9 | 2026-05-19 | cancelled | 8 |
| 1014 | 7 | 2026-06-02 | shipped | 4 |
| 1015 | 1 | 2026-06-10 | shipped | 3 |
| 1016 | 11 | 2026-06-12 | open | NULL |
| 1017 | 4 | 2026-06-13 | open | NULL |
| 1018 | 10 | 2026-06-14 | open | NULL |
order_items 38 rows, 4 columns
| order_id | product_id | quantity | unit_price |
|---|---|---|---|
| 1001 | 1 | 40 | 4.79 |
| 1001 | 3 | 25 | 2.79 |
| 1001 | 6 | 4 | 24.9 |
| 1002 | 2 | 2 | 3.49 |
| 1002 | 10 | 3 | 5.49 |
| 1003 | 1 | 60 | 4.69 |
| 1003 | 11 | 6 | 84.9 |
| 1003 | 3 | 50 | 2.59 |
| 1004 | 4 | 3 | 179 |
| 1004 | 9 | 3 | 269 |
| 1005 | 5 | 1 | 449 |
| 1006 | 1 | 30 | 4.79 |
| 1006 | 7 | 10 | 6.99 |
| 1007 | 8 | 2 | 219 |
| 1007 | 11 | 4 | 89.9 |
| 1007 | 1 | 20 | 4.99 |
| 1008 | 4 | 6 | 175 |
| 1008 | 2 | 5 | 3.49 |
| 1009 | 10 | 120 | 4.99 |
| 1009 | 2 | 40 | 3.19 |
| 1009 | 9 | 2 | 279 |
| 1010 | 7 | 2 | 6.99 |
| 1010 | 2 | 1 | 3.49 |
| 1011 | 12 | 1 | 129 |
| 1011 | 1 | 10 | 4.99 |
| 1011 | 3 | 12 | 2.79 |
| 1012 | 6 | 8 | 23.9 |
| 1012 | 1 | 40 | 4.69 |
| 1013 | 9 | 1 | 279 |
| 1014 | 5 | 2 | 439 |
| 1014 | 4 | 2 | 179 |
| 1015 | 1 | 50 | 4.79 |
| 1015 | 11 | 2 | 89.9 |
| 1016 | 10 | 4 | 5.49 |
| 1017 | 2 | 3 | 3.49 |
| 1017 | 7 | 2 | 6.99 |
| 1018 | 1 | 80 | 4.79 |
| 1018 | 6 | 10 | 24.9 |
This exercise is part of Premium.
Premium unlocks every exercise in every lesson, unlimited practice with fresh tasks and the final test with certificate.
See Premium Already Premium? Log in
The first exercise in this lesson is free.
Common mistakes
A row condition in HAVING instead of WHERE
GROUP BY employee_id HAVING status = 'delivered' looks innocent but gives wrong counts. COUNT(*) has already counted every order, and SQLite checks the status against just one row of each group. In the first exercise the checker answers "Your query returns only 3 rows. Wanted: 4 rows. Is a column missing from GROUP BY, or is a condition in WHERE or HAVING too strict?" MySQL with its default ONLY_FULL_GROUP_BY setting and PostgreSQL won't run the query at all; the SQLite documentation describes these "bare columns" as an SQLite-specific extension.
COUNT or SUM in WHERE
WHERE COUNT(*) >= 2 can't work, because there are no groups yet when WHERE runs. The exercise tells you: "Functions like COUNT or SUM do not work in WHERE, because WHERE checks single rows. Conditions on groups go in HAVING."
Writing the clauses in execution order
The execution order is a way of thinking, not a way of writing. … GROUP BY category WHERE price < 100 is a syntax error near "WHERE", and the exercise reminds you of the written order: SELECT, FROM, WHERE, GROUP BY, HAVING, ORDER BY, LIMIT.
Expecting LIMIT to trim the raw data
LIMIT 3 does not mean "take three orders and then add them up". It applies after GROUP BY and ORDER BY. Leave out ORDER BY and you get three arbitrary groups; the checker then says that not every value matches.
Order and aliases in MySQL, PostgreSQL, SQL Server and SQLite
The logical order is the same everywhere. Where the products differ is how early a SELECT alias may be used:
| Database | Alias in WHERE | in GROUP BY | in HAVING | in ORDER BY |
|---|---|---|---|---|
| MySQL | no | yes | yes | yes |
| PostgreSQL | no | yes | no | yes |
| SQL Server | no | no | no | yes |
| SQLite (these exercises) | runs | runs | runs | yes |
The MySQL manual explains the WHERE ban with exactly this order: when WHERE is evaluated, the column value may not be known yet. Microsoft lists the SQL Server steps as FROM, ON, JOIN, WHERE, GROUP BY, WITH CUBE or WITH ROLLUP, HAVING, SELECT, DISTINCT, ORDER BY, TOP, and states that a SELECT alias is only visible to later clauses such as ORDER BY. In SQL Server the last step is TOP rather than LIMIT. For SQLite, only the ORDER BY case is documented; that WHERE, GROUP BY and HAVING accept an alias too is something we tested in the exercises. If a query has to run anywhere, spell the expression out in WHERE and HAVING.
Sources: Microsoft Learn (SELECT, logical processing order), PostgreSQL documentation (SELECT), MySQL Reference Manual (Problems with Column Aliases), sqlite.org (SELECT).
FAQ
Does the database really run the steps one after another?
Not necessarily. The optimizer may use an index or test a condition earlier. The result, however, has to look as if the steps ran in the logical order, which is why that order is the right tool for explaining results and errors.
How do I choose between WHERE and HAVING?
If the condition is about a single row (status, price, date), use WHERE. If it is about a group (a count, a total, an average), use HAVING. WHERE also keeps unneeded rows out of the groups in the first place.
Where does WITH fit in?
Right at the start. According to the PostgreSQL documentation, the queries in the WITH list are computed first and can then be used in FROM like tables.
Can I sort by a column that is not in SELECT?
Yes. ORDER BY can use any column of the tables in FROM, even one you don't display.