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
- FROM
- JOIN
- WHERE
- GROUP BY
- HAVING
- SELECT
- ORDER BY
- LIMIT
Key Insight
Even though we write:
sqlSELECT nameFROM employeesWHERE salary > 50000;
SQL actually processes:
FROM → get table
WHERE → filter rows
SELECT → choose columns
Example Dataset
Table: employees
Step-by-Step Execution
-
FROM
FROM employees
Loads the table. -
JOIN
JOIN departments ON ...
Combines tables. -
WHERE
WHERE salary > 50000
Filters rows before grouping. -
GROUP BY
GROUP BY department
Groups rows. -
HAVING
HAVING COUNT(*) > 1
Filters grouped data. -
SELECT
SELECT department, COUNT(*)
Chooses columns. -
ORDER BY
ORDER BY COUNT(*) DESC
Sorts results. -
LIMIT
LIMIT 2
Restricts output rows.
Full Example
sqlSELECT department, COUNT(*) AS totalFROM employeesWHERE salary > 40000GROUP BY departmentHAVING COUNT(*) > 1ORDER BY total DESCLIMIT 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
- Using alias in WHERE
sqlSELECT salary * 2 AS new_salaryFROM employeesWHERE new_salary > 100000; -- wrong
Fix:
sqlSELECT salary * 2 AS new_salaryFROM employeesWHERE salary * 2 > 100000;
- 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.
Execution order identify
Identify execution order of: SELECT name FROM employees WHERE salary > 50000;
Solve MissionWhy SELECT not first
Explain why SELECT cannot be used before WHERE logically.
Fix alias in WHERE
Fix the query using alias in WHERE.
When HAVING applied
Explain when HAVING is applied.
Filter and group
Write query using correct execution order: filter salary > 40000, group by department.
Which runs first
Which runs first: WHERE or GROUP BY?
Which runs last
Which runs last: ORDER BY or LIMIT?
Identify error
Identify error: SELECT COUNT(*) FROM employees WHERE COUNT(*) > 1;
Correct HAVING query
Correct query using HAVING for above.
Explain execution order
Explain execution order for query with JOIN, WHERE, GROUP BY, HAVING.