Also SELECT should be the last clause (at least for row-oriented DBMS, for columnar databases it might come before WHERE, GROUP BY, ORDER BY to hint which columns we want to select to query on). Row-oriented: FROM table_name WHERE condition GROUP BY ... HAVING ... ORDER BY ... SELECT column1, column2, ...; Columnar: FROM table_name SELECT column1, column2, ... ORDER BY ... WHERE condition GROUP BY ... HAVING ...;
The lexical ordering is:
SELECT
FROM
WHERE
GROUP BY
HAVING
UNION
ORDER BY
while the logical order is: FROM
WHERE
GROUP BY
HAVING
SELECT
UNION
ORDER BY
[1] https://blog.jooq.org/10-easy-steps-to-a-complete-understand...