Sitemap
Garima Gupta

Garima Gupta

Performance Architect , SRE , Full stack AI Implementation

SQL Query Execution Order

May 29, 2026
Press enter or click to view image in full size

We generally writes SQL in order :

SELECT → FROM → WHERE

whereas SQL actually run like in order :

FROM → WHERE → SELECT

Let 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

--

--

Garima Gupta
Garima Gupta

Written by Garima Gupta

Performance Architect , SRE , Full stack AI Implementation