This content originally appeared on DEV Community and was authored by Abhinav
While reviewing a PR today, it struck me how SQL queries are written in a logical order that differs from how the database actually executes them.
SELECT name
FROM employees
WHERE age > 30
ORDER BY name;
Simple, right? But did you know SQL doesn't execute the query in this order?
Letβs dive into the actual execution order of SQL queries and why understanding this can save you from common mistakes and boost your database skills.
π§© The Logical Execution Order of SQL
When SQL runs your query, it doesn't start with SELECT
. Instead, it follows this logical order:
Step | Clause | Purpose |
---|---|---|
1οΈβ£ | FROM |
Chooses and joins tables |
2οΈβ£ | WHERE |
Filters rows based on conditions |
3οΈβ£ | GROUP BY |
Groups rows into buckets |
4οΈβ£ | HAVING |
Filters groups |
5οΈβ£ | SELECT |
Chooses which columns or expressions to show |
6οΈβ£ | ORDER BY |
Sorts the result |
7οΈβ£ | LIMIT |
Limits the number of rows returned |
π§ Remember: Even though we write SELECT
first, SQL runs FROM
first!
π Letβs Understand With an Example
SELECT department, COUNT(*) as num_employees
FROM employees
WHERE age > 25
GROUP BY department
HAVING COUNT(*) > 5
ORDER BY num_employees DESC
LIMIT 3;
Step-by-Step Breakdown:
FROM
employees
β Loads theemployees
table.WHERE
age > 25
β Filters employees older than 25.GROUP BY
department
β Groups remaining employees by department.HAVING
COUNT(*) > 5
β Filters groups (departments) with more than 5 employees.SELECT
department, COUNT(*)
β Projects the result columns.ORDER BY
num_employees DESC
β Sorts departments by employee count, descending.LIMIT
3
β Picks only top 3 departments.
π« Common Gotchas
- β Using column aliases in WHERE: You can't use a
SELECT
alias inWHERE
becauseSELECT
comes afterWHERE
.
SELECT salary * 12 AS annual_salary
FROM employees
WHERE annual_salary > 50000; -- β This will fail
β
Instead, use the expression directly:
SELECT salary * 12 AS annual_salary
FROM employees
WHERE salary * 12 > 50000;
- β Using aggregate functions in WHERE: You canβt use
COUNT()
,SUM()
, etc. inWHERE
β useHAVING
instead.
π§ Why This Matters
- π Debugging complex queries becomes easier
- π― You write more efficient and correct queries
- π You understand why some queries fail or return unexpected results
π Quick Recap: Execution Order
1. FROM
2. WHERE
3. GROUP BY
4. HAVING
5. SELECT
6. ORDER BY
7. LIMIT
Write SQL like a poet, but think like a compiler. π»β€οΈ
πΌοΈ Bonus:
Here's a simple visual of the execution order (great for your wall π):
ββββββββββββββ
β FROM β
ββββββ¬ββββββββ
βΌ
ββββββββββββββ
β WHERE β
ββββββ¬ββββββββ
βΌ
ββββββββββββββ
β GROUP BY β
ββββββ¬ββββββββ
βΌ
ββββββββββββββ
β HAVING β
ββββββ¬ββββββββ
βΌ
ββββββββββββββ
β SELECT β
ββββββ¬ββββββββ
βΌ
ββββββββββββββ
β ORDER BY β
ββββββ¬ββββββββ
βΌ
ββββββββββββββ
β LIMIT β
ββββββββββββββ
π§ Final Thought
If you're just starting out with SQL, internalizing the execution order will make you a more thoughtful and powerful query writer. Next time your query breaks or returns weird results, look back at the steps and see where itβs going off-track!
This content originally appeared on DEV Community and was authored by Abhinav

Abhinav | Sciencx (2025-06-25T16:30:24+00:00) π§ Understanding SQL Query Execution Order: Not As Straightforward As You Think. Retrieved from https://www.scien.cx/2025/06/25/%f0%9f%a7%a0-understanding-sql-query-execution-order-not-as-straightforward-as-you-think/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.