SQL GROUP BY One Column
The simplest, most common shape — one column decides the groups, one aggregate summarizes each one.
What & Why
Grouping by a single column is by far the most common pattern you'll write — "total X per Y" or "average X per Y" questions almost always start here. The previous lesson walked through the full mechanics; this lesson is about recognizing the pattern quickly and applying it directly.
The shape is always the same: SELECT grouping_column, aggregate(other_column) FROM table GROUP BY grouping_column. Once that structure feels automatic, reaching for it becomes second nature.
See How It Works
Growth wants one user count for each acquisition channel.
| id | created_at | country | channel | plan | activated_at | churned_at |
|---|---|---|---|---|---|---|
| 101 | 2024-01-15 09:10:00+00 | US | organic | pro | 2024-01-16 14:25:00+00 | NULL |
| 102 | 2024-02-10 11:05:00+00 | CA | paid | free | NULL | 2024-03-20 10:00:00+00 |
| 103 | 2024-03-12 16:35:00+00 | GB | referral | pro | 2024-03-13 08:15:00+00 | NULL |
| 104 | 2024-04-01 13:20:00+00 | US | organic | free | 2024-04-03 12:00:00+00 | NULL |
SELECT
channel,
COUNT(*) AS user_count
FROM growth.users
GROUP BY channel
ORDER BY user_count DESC, channel;| channel | user_count |
|---|---|
| organic | 2 |
| paid | 1 |
| referral | 1 |
The four growth.users rows form three real channel groups.
Practice this concept
Growth wants one user count for each acquisition channel.
growthPrefix tables with growth.table_name.
channeluser_countgrowth.users| Column | Type |
|---|---|
| id | integer |
| created_at | timestamp with time zone |
| country | text |
| channel | text |
| plan | text |
| activated_at | timestamp with time zone |
| churned_at | timestamp with time zone |
| legacy_user_code | text |
Sign up free to try it on a real business scenario