FROM, JOIN, WHERE, SELECT, GROUP BY, HAVING, DISTINCT, UNION, ORDER BY, LIMIT.
The author has SELECT after the GROUP BY and HAVING, which doesn't make sense because GROUP BY is aware of expressions in the SELECT statement. Grouping and filtering happens after selection.
SELECT is evaluated after GROUP BY, at least in most databases that implement SQL correctly. SQL Server's documentation nicely explains this:
http://technet.microsoft.com/en-us/library/ms189499.aspx
Skip to "Logical Processing Order of the SELECT statement".
MySQL, PostgreSQL, and SQLite behave differently, though!
WITH orders(created_at) AS (
SELECT date '2012-01-01' UNION ALL
SELECT date '2012-01-02' UNION ALL
SELECT date '2012-01-03' UNION ALL
SELECT date '2012-01-04' UNION ALL
SELECT date '2012-01-05' UNION ALL
SELECT date '2012-01-06' UNION ALL
SELECT date '2012-01-07' UNION ALL
SELECT date '2012-01-08'
)
SELECT EXTRACT(DOW FROM created_at) wkday, COUNT(1)
FROM orders
GROUP BY wkday
That's really disturbing. I fixed the linked article accordingly.Edit: it seems more complicated that I initially assumed. MySQL doesn't allow it. SQLite does.
Rule number 2, for instance, does not apply exactly in the above way to MySQL, PostgreSQL, and SQLite.