# GROUP BY

> Split rows into groups and compute one aggregate per group, with the one rule PostgreSQL insists on for the SELECT list.

Subject: SQL | Lesson 20 of 44 | 6 min read
Interactive lesson: https://codewithanimation.in/sql/group-by
Previous: [Aggregate Functions](https://codewithanimation.in/sql/aggregate-functions.md) | Next: [HAVING vs WHERE](https://codewithanimation.in/sql/having-vs-where.md)

## One result row per group

`GROUP BY` puts all rows that share the same value in a column into one group. Each aggregate in the `SELECT` list is then computed separately inside each group, and the query returns one row per group.

Without `GROUP BY`, all rows form one single group. That is why `SELECT count(*) FROM players` returns exactly one row.

## Totals per category

Real questions are rarely about the whole table: "how many players per team?", "total sales per month?", "orders per customer?". `GROUP BY` answers all of them with one query.

## Count players per team

```sql
SELECT team, count(*) AS n, max(points) AS best
FROM players
GROUP BY team;
```

The 8 players fall into 3 groups: Red has 3 rows, Blue has 3 and Green has 2. The result has 3 rows, one for each team, and `count(*)` and `max(points)` are worked out inside each team.

When you group by a column that has `NULL`, all the NULLs go into one group. Chen and Eli both have NULL points, so `GROUP BY points` gives them one shared NULL group of 2 rows.

## Common mistake: The SELECT list rule

Every plain column in the `SELECT` list must be in `GROUP BY` or be inside an aggregate. This query fails: `SELECT team, name, count(*) FROM players GROUP BY team`. A team has several names, and the database will not guess which one to show.

PostgreSQL says: `column "players.name" must appear in the GROUP BY clause or be used in an aggregate function`. The fix is either to add `name` to `GROUP BY` (which makes smaller groups), or to wrap it in an aggregate such as `max(name)`.

Without an `ORDER BY`, the order of the group rows is not promised. Add one when the order matters.

## Tip: Ask what one row of the result stands for

Before you write `GROUP BY`, finish the sentence "one row per ...". Whatever fills the gap, such as team, goes in `GROUP BY` and in `SELECT`. Everything else must be an aggregate.

## Try it: Group by team, by points, or by nothing

Watch the last column of the source table: it shows the group every row falls into. Then press Also SELECT name to see the real error.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/sql/group-by

## Check yourself

### Question 1

`players` has 8 rows from 3 teams. How many rows does `SELECT team, count(*) FROM players GROUP BY team` return?

- A. 3
- B. 8
- C. 1

Correct answer: A

GROUP BY returns one row for each different team, and there are 3 teams.

- Why not B: 8 is the number of input rows. GROUP BY returns one row per group.
- Why not C: One row would come from a query with no GROUP BY. Here there are 3 teams.

### Question 2

What happens with `SELECT team, name, count(*) FROM players GROUP BY team`?

- A. An error: name must appear in GROUP BY or be inside an aggregate
- B. It shows the first name in each team
- C. It works, and name is NULL for every group

Correct answer: A

A group holds several names, so name has no single value per group. Group by it, or aggregate it.

- Why not B: PostgreSQL does not pick a value for you. It refuses the query.
- Why not C: No value is made up. The query is rejected before it runs.

### Question 3

Two players have NULL `points`. How many groups do they make in `GROUP BY points`?

- A. One group of two rows
- B. Two groups of one row
- C. They are left out of the result

Correct answer: A

For grouping, all NULLs count as equal, so they land in one group.

- Why not B: NULL = NULL is not true in a comparison, but GROUP BY treats NULLs as the same value.
- Why not C: NULL rows are not dropped. They form a group of their own.

## Remember this

- GROUP BY makes one group per different value, and the query returns one row per group.
- Aggregates are computed separately inside each group.
- Every plain column in SELECT must be in GROUP BY or inside an aggregate.
- All NULLs fall into one group, and the order of groups is not promised without ORDER BY.
