GUIDE深度 · Deep dive
SQL JOIN and GROUP BY
JOIN combines two tables on a key, GROUP BY groups rows by a dimension, and aggregates compute per group. After a JOIN, decide what one row means before choosing the GROUP BY grain.
JOIN
INNER JOIN keeps rows matched on both sides.
LEFT JOIN keeps all left rows and fills missing right values with NULL.
GROUP BY
Non-aggregated SELECT columns belong in GROUP BY.
HAVING filters groups after aggregation; WHERE filters rows before.
FAQ
COUNT(*) vs COUNT(col)?
COUNT(*) counts rows; COUNT(col) ignores NULL.
Can I GROUP BY multiple columns?
Yes; the grain is the combination of those columns.