Skip to content
Hands-on Mentor
ENTR
Back to blog

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

SQL does not run a query in the order you write it. Learn the real order of execution, why aliases fail in WHERE, and how to avoid the common errors.

4 min read

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.

StepClauseWhat happens
1FROM and JOINPick the tables and combine rows
2WHERERemove rows
3GROUP BYForm groups
4HAVINGRemove groups
5SELECTCompute columns and aliases
6DISTINCTRemove duplicate rows
7ORDER BYSort the result
8LIMITKeep 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 exist

The 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_namescore
Noor100.00
Omar100.00
Anika97.00
Camila97.00
Zara96.25
Kenji95.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 WHERE

Use 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_idstudents
424
723

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.

yearstudents
210
38
49

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_namelast_namegpa
NoorGarcía4.00
OmarKim4.00
CamilaOsei3.88

What should you remember?

  • Aliases from SELECT work in ORDER BY, but not in WHERE or HAVING in most databases.
  • Aggregates like COUNT and AVG cannot be used in WHERE. Use HAVING.
  • WHERE filters rows. HAVING filters groups.
  • LIMIT goes last, after ORDER 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
All posts