# GROUP BY — MySQL

Source: https://www.geekswithgeeks.com/en/mysql/sql-group-by

> Aggregate within buckets instead of the whole table.

## Grouping rows

`GROUP BY` buckets rows sharing a value, then aggregate functions run per bucket.

```sql
SELECT customer_id, COUNT(*) AS orders, SUM(amount) AS total
FROM orders
GROUP BY customer_id;
```

Output:

```
customer_id | orders | total
1           | 2      | 3200.00
2           | 1      | 1300.00
```

## Non-aggregated columns

Every column in `SELECT` that isn't inside an aggregate function must appear in `GROUP BY` — MySQL enforces this in strict mode.
