Solve “Deduplicate events with ROW_NUMBER”

Free. Runs in your browser, no install.

SQL · Basics & Joins

Order of SQL clause execution

Easyexecution orderquery processing

Interview question

What is the logical order of SQL clause execution?

How to answer

The short answer

A simplified logical order is:

Output
FROM / JOIN
WHERE
GROUP BY
HAVING
SELECT
DISTINCT
ORDER BY
LIMIT

So although we write:

SQL
SELECT ...
FROM ...
WHERE ...
GROUP BY ...
HAVING ...
ORDER BY ...

SQL logically processes FROM first.

Interview gotcha: the order you write clauses is not the order SQL runs them.

Lock it in with a real drill

You remember an answer longer once you have used it. Try a hands-on problem on this concept.

Open related drill