🧠 Understanding SQL Query Execution Order: Not As Straightforward As You Think
Abhinav

Abhinav @abhivyaktii

About: Learning In Public

Location:
Bengaluru, India
Joined:
Nov 10, 2021

🧠 Understanding SQL Query Execution Order: Not As Straightforward As You Think

Publish Date: Jun 25
1 0

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;
Enter fullscreen mode Exit fullscreen mode

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;
Enter fullscreen mode Exit fullscreen mode

Step-by-Step Breakdown:

  1. FROM employees
    β†’ Loads the employees table.

  2. WHERE age > 25
    β†’ Filters employees older than 25.

  3. GROUP BY department
    β†’ Groups remaining employees by department.

  4. HAVING COUNT(*) > 5
    β†’ Filters groups (departments) with more than 5 employees.

  5. SELECT department, COUNT(*)
    β†’ Projects the result columns.

  6. ORDER BY num_employees DESC
    β†’ Sorts departments by employee count, descending.

  7. LIMIT 3
    β†’ Picks only top 3 departments.


🚫 Common Gotchas

  • ❌ Using column aliases in WHERE: You can't use a SELECT alias in WHERE because SELECT comes after WHERE.
  SELECT salary * 12 AS annual_salary
  FROM employees
  WHERE annual_salary > 50000; -- ❌ This will fail
Enter fullscreen mode Exit fullscreen mode

βœ… Instead, use the expression directly:

  SELECT salary * 12 AS annual_salary
  FROM employees
  WHERE salary * 12 > 50000;
Enter fullscreen mode Exit fullscreen mode
  • ❌ Using aggregate functions in WHERE: You can’t use COUNT(), SUM(), etc. in WHERE β€” use HAVING 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
Enter fullscreen mode Exit fullscreen mode

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     β”‚
      β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
Enter fullscreen mode Exit fullscreen mode

🧠 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!

Comments 0 total

    Add comment