Advanced SQL Topics
GROUP BY
Note that the query below will not work
SELECT ST, 2022 estimate
FROM database_table
WHERE ST IN (‘NY’, ‘CA’, ‘TX’)
GROUP BY ST
While the following will indeed work:
SELECT ST, SUM(2022 estimate)
FROM database_table
WHERE ST IN (‘NY’, ‘CA’, ‘TX’)
GROUP BY ST
A GROUP BY needs an aggregation within it since it groups each distinct value in one column, and will perform an aggregation on it. So the first query will not run successfully since there is nothing to aggregate with.
Note that we can use any aggregation in SQL (AVG, SUM, COUNT, MIN, MAX) in a group by clause to return the aggregated data’s calculation.