SQL Query Execution Order
We generally writes SQL in order :
SELECT → FROM → WHEREwhereas SQL actually run like in order :
FROM → WHERE → SELECTLet that sink in
So the order you write SQL is NOT the order it executes. And once you understand this — everything changes.
The Real Order :
Step 1 — FROM / JOIN / ON
Table get joins first.
Step 2 — WHERE
Get Garima Gupta’s stories in your inbox
Join Medium for free to get updates from this writer.
Rows are filtered.
Step 3 — GROUP BY
Rows are bucketed.
Step 4 — HAVING
Groups are filtered.
Step 5— SELECT DISTINCT
Finally — your columns are picked.
STEP 6 — ORDER BY
Results are sorted.
STEP 7—LIMIT
Only what’s needed is returned.
It’s the secret behind every fast query








