SQL order of writing & order of execution

The order in which you write SQL statements isn't the order in which they are executed.

Use this as a guide for writing your statements. Sometimes in SQL it's easy to forget whether a WHERE statement fomes before or after a JOIN, for example.

Order of Writing (Syntax) Logical Order of Execution
1. SELECT 8. SELECT
2. FROM 1. FROM
3. JOIN 2. JOIN
4. WHERE 3. WHERE
5. GROUP BY 4. GROUP BY
6. HAVING 5. HAVING
7. ORDER BY 7. ORDER BY
8. LIMIT / TOP / FETCH 9. LIMIT / TOP / FETCH