SQL fundamentals
SELECT, WHERE, GROUP BY, HAVING, the lot. The order you write a query is not the order it executes — FROM and JOIN run first, SELECT almost last. Internalize that and half of SQL stops being surprising.
External PostgreSQL: SQL language