SQL FOUNDATIONS:Query Execution Order

Understand how SQL executes clauses under the hood.

SQL Query Execution Order Explained with Examples + Practice Questions

Why This Matters

SQL queries are written in one order, but executed in another.

Understanding this helps you:

  • Avoid logical mistakes
  • Debug queries
  • Solve interview questions correctly

Actual Execution Order

  1. FROM
  2. JOIN
  3. WHERE
  4. GROUP BY
  5. HAVING
  6. SELECT
  7. ORDER BY
  8. LIMIT

Key Insight

Even though we write:

sql
SELECT name
FROM employees
WHERE salary > 50000;

SQL actually processes:

FROM → get table
WHERE → filter rows
SELECT → choose columns

Example Dataset

Table: employees

idnamedepartmentsalary
1JohnHR40000
2AliceIT60000
3BobIT55000
4EmmaFinance70000
5DavidHR45000

Step-by-Step Execution

  1. FROM
    FROM employees
    Loads the table.

  2. JOIN
    JOIN departments ON ...
    Combines tables.

  3. WHERE
    WHERE salary > 50000
    Filters rows before grouping.

  4. GROUP BY
    GROUP BY department
    Groups rows.

  5. HAVING
    HAVING COUNT(*) > 1
    Filters grouped data.

  6. SELECT
    SELECT department, COUNT(*)
    Chooses columns.

  7. ORDER BY
    ORDER BY COUNT(*) DESC
    Sorts results.

  8. LIMIT
    LIMIT 2
    Restricts output rows.

Full Example

sql
SELECT department, COUNT(*) AS total
FROM employees
WHERE salary > 40000
GROUP BY department
HAVING COUNT(*) > 1
ORDER BY total DESC
LIMIT 1;

Execution Breakdown

FROM → employees
WHERE → salary > 40000
GROUP BY → group by department
HAVING → filter groups
SELECT → department, count
ORDER BY → sort
LIMIT → return top result

Common Mistakes

  1. Using alias in WHERE
sql
SELECT salary * 2 AS new_salary
FROM employees
WHERE new_salary > 100000; -- wrong

Fix:

sql
SELECT salary * 2 AS new_salary
FROM employees
WHERE salary * 2 > 100000;
  1. Using HAVING without GROUP BY incorrectly

HAVING should be used with aggregated results.

Key Concepts

  • SQL executes FROM first, not SELECT
  • WHERE filters before grouping
  • HAVING filters after grouping
  • SELECT happens late in execution
  • ORDER BY happens at the end

Query Execution Flow Summary

FROM → WHERE → GROUP BY → HAVING → SELECT → ORDER BY → LIMIT

Common Questions (FAQ)

What is SQL execution order

The sequence in which SQL processes a query internally.

Why is it important

Helps avoid logical errors and improves understanding.

Does SQL run SELECT first

No, it runs FROM first.

When is HAVING used

After GROUP BY to filter aggregated results.

Internal Learning Links

  • Learn WHERE filtering
  • Learn GROUP BY
  • Learn JOINs

Practice CTA

Practice SQL queries on Schoolabe to understand execution flow deeply.

Query Execution Order Missions

Solve exercises in sequence to unlock the next mission.

1

Execution order identify

Identify execution order of: SELECT name FROM employees WHERE salary > 50000;

Solve Mission
2

Why SELECT not first

Explain why SELECT cannot be used before WHERE logically.

Locked
3

Fix alias in WHERE

Fix the query using alias in WHERE.

Locked
4

When HAVING applied

Explain when HAVING is applied.

Locked
5

Filter and group

Write query using correct execution order: filter salary > 40000, group by department.

Locked
6

Which runs first

Which runs first: WHERE or GROUP BY?

Locked
7

Which runs last

Which runs last: ORDER BY or LIMIT?

Locked
8

Identify error

Identify error: SELECT COUNT(*) FROM employees WHERE COUNT(*) > 1;

Locked
9

Correct HAVING query

Correct query using HAVING for above.

Locked
10

Explain execution order

Explain execution order for query with JOIN, WHERE, GROUP BY, HAVING.

Locked