Do you know what actually happens when you run an SQL query?
It’s not as simple as: “SQL just fetches rows from a table.”
Behind the scenes, your query goes through a step-by-step execution order and knowing this is often an interview question for beginners.
Execution Order:
FROM (pick the table)
WHERE (filter rows)
GROUP BY (make buckets)
HAVING (filter buckets)
SELECT (choose what to show)
ORDER BY (sort the results)
LIMIT (control how much to show)
Notice how WHERE comes before grouping, while HAVING comes after grouping.
Notice how ORDER BY is one of the last steps, even though we often write it after SELECT.
Common Confusions:
a) WHERE vs HAVING
WHERE filters rows before grouping.
HAVING filters groups after aggregation.
Example:
WHERE city = 'Delhi' → filter students from Delhi first.
HAVING COUNT(*) > 10 → only keep cities with more than 10 students.
b) GROUP BY vs ORDER BY
GROUP BY → bundles rows into categories (e.g., students per city).
ORDER BY → decides the sequence in which results are displayed (e.g., sort cities by student count).
c) LIMIT with ORDER BY
Important tasks like “find top 3 highest salaries” or “top 5 cheapest products.”
If you found this useful, drop a like or share it with someone preparing for SQL interviews.
Follow for more such insightful posts , I’m breaking SQL down into small, clear lessons you’ll actually remember.