The SQL order of execution is different from the order you write. You write SELECT first, but the database starts with FROM, then WHERE, GROUP BY, HAVING, SELECT, ORDER BY, and LIMIT. Once you know this order, many confusing errors make sense.
The examples use the student table of the fictional Northbridge University in Port Alder, which has 120 students.
What is the order of execution in SQL?
This is the logical order. It describes the result you get, even if the database optimizes the steps behind the scenes.
| Step | Clause | What happens |
|---|---|---|
| 1 | FROM and JOIN | Pick the tables and combine rows |
| 2 | WHERE | Remove rows |
| 3 | GROUP BY | Form groups |
| 4 | HAVING | Remove groups |
| 5 | SELECT | Compute columns and aliases |
| 6 | DISTINCT | Remove duplicate rows |
| 7 | ORDER BY | Sort the result |
| 8 | LIMIT | Keep the first rows |
A short memory aid: the database first finds the data, then filters, then groups, then filters groups, and only then picks and names the columns.
Why can't you use a column alias in WHERE?
An alias is created in step 5, SELECT. WHERE runs in step 2, so the alias does not exist yet. This query fails:
SELECT first_name, gpa * 25 AS score FROM student WHERE score > 95;
Outputcolumn "score" does not existThe fix is to repeat the expression in WHERE:
SELECT first_name, gpa * 25 AS score FROM student WHERE gpa * 25 > 95 ORDER BY score DESC, first_name;
| first_name | score |
|---|---|
| Noor | 100.00 |
| Omar | 100.00 |
| Anika | 97.00 |
| Camila | 97.00 |
| Zara | 96.25 |
| Kenji | 95.75 |
Notice that ORDER BY score works. Sorting is step 7, after the alias exists.
Why can't you use COUNT in WHERE?
A group does not exist at step 2, so a group total cannot exist either. This fails:
SELECT department_id, COUNT(*) AS students FROM student WHERE COUNT(*) > 20 GROUP BY department_id;
Outputaggregate functions are not allowed in WHEREUse HAVING, which runs after the groups are formed:
SELECT department_id, COUNT(*) AS students FROM student GROUP BY department_id HAVING COUNT(*) > 20 ORDER BY students DESC;
| department_id | students |
|---|---|
| 4 | 24 |
| 7 | 23 |
For more on this pair, read [WHERE vs HAVING in SQL](/blog/sql-where-vs-having).
How do all the steps work together?
Take this query and follow it step by step:
SELECT year, COUNT(*) AS students FROM student WHERE gpa >= 3 GROUP BY year HAVING COUNT(*) >= 5 ORDER BY year;
1. FROM student starts with all 120 rows.
2. WHERE gpa >= 3 keeps only students with a GPA of at least 3. Rows with no GPA are removed too.
3. GROUP BY year makes one group for each year.
4. HAVING COUNT(*) >= 5 drops groups with fewer than 5 students.
5. SELECT creates the students column.
6. ORDER BY year sorts the groups.
| year | students |
|---|---|
| 2 | 10 |
| 3 | 8 |
| 4 | 9 |
Year 1 is missing. After the WHERE filter, it had fewer than 5 students, so HAVING removed it.
Does LIMIT come before or after ORDER BY?
LIMIT runs last, so it cuts the sorted result. You must also write it last. This order is a syntax error:
OutputSELECT first_name, gpa FROM student WHERE gpa > 3.9 LIMIT 2 ORDER BY gpa;
syntax error at or near "ORDER"Write ORDER BY first and LIMIT after it. Here are the top three GPAs, with missing values removed:
SELECT first_name, last_name, gpa FROM student WHERE gpa IS NOT NULL ORDER BY gpa DESC, last_name LIMIT 3;
| first_name | last_name | gpa |
|---|---|---|
| Noor | García | 4.00 |
| Omar | Kim | 4.00 |
| Camila | Osei | 3.88 |
What should you remember?
- Aliases from
SELECTwork inORDER BY, but not inWHEREorHAVINGin most databases. - Aggregates like
COUNTandAVGcannot be used inWHERE. UseHAVING. WHEREfilters rows.HAVINGfilters groups.LIMITgoes last, afterORDER BY.
Some databases allow an alias in GROUP BY. Others do not, so repeating the expression is the safe choice.
What to read next
- [Lesson: execution order and common errors](/learn/sql/execution-order-errors)
- [SQL course: 41 short lessons, free](/courses/sql)
- [SQL GROUP BY explained](/blog/sql-group-by)
- [SQL practice for beginners: 10 exercises](/blog/sql-practice-for-beginners)
- #sql
- #order of execution
- #where
- #having
- #beginners