Sub topic 5
Grouping by More Than One Column
This chapter assumes you know basic SELECT, FROM, WHERE, and ORDER BY syntax. You should also understand that NULL represents a missing value and that a table contains rows and columns.
One group for each combination
GROUP BY can contain several columns. SQL creates one group for each distinct combination of values in those columns.
Suppose orders has these columns:
| order_id | customer_id | status | amount |
|---|---|---|---|
| 101 | 7 | paid | 40 |
| 102 | 7 | pending | 25 |
| 103 | 7 | paid | 60 |
| 104 | 9 | paid | 80 |
Grouping by customer_id alone creates one group for customer 7 and one for customer 9. Grouping by both customer_id and status creates these combinations:
(7, paid)(7, pending)(9, paid)
The order of the grouping columns does not change which combinations exist. It can affect the output order or the order of subtotal levels when you use ROLLUP.
SELECT customer_id,
status,
COUNT(*) AS order_count,
SUM(amount) AS total_amount
FROM orders
GROUP BY customer_id, status
ORDER BY customer_id, status;The result has one row per customer-status combination. For customer 7, the two paid orders form one group, and the pending order forms another group.
Choosing the grouping level
Add a column when the report needs a more detailed breakdown. For example:
GROUP BY customer_idreports one summary per customer.GROUP BY statusreports one summary per status.GROUP BY customer_id, statusreports one summary per customer and status pair.
Every non-aggregated column in the SELECT list must appear in GROUP BY. Aggregate functions such as COUNT(*) and SUM(amount) may appear without being grouped because they produce one value for each group.
SELECT customer_id,
status,
COUNT(*) AS order_count
FROM orders
GROUP BY customer_id, status;A query such as the following is invalid in most SQL systems:
SELECT customer_id,
status,
COUNT(*) AS order_count
FROM orders
GROUP BY customer_id;The database knows which customer each group represents, but it does not know which single status to display when a customer has several statuses. Grouping by status removes that ambiguity.
Some databases allow non-grouped columns through special extensions or configuration settings. Avoid relying on that behavior because the selected value may be arbitrary and the query may fail after moving to another database system.
`NULL` in multiple-column groups
Rows with NULL in a grouping column are grouped together. SQL treats all missing values as one grouping value for this purpose.
For example, rows with (customer_id = 7, status = NULL) belong to the same group. This does not mean that NULL equals another NULL in ordinary comparisons. Grouping uses special grouping rules.
A missing value in any grouping column changes the combination:
| customer_id | status | Group |
|---|---|---|
| 7 | paid | `(7, paid)` |
| 7 | NULL | `(7, NULL)` |
| NULL | paid | `(NULL, paid)` |
Checking grouped results
Before trusting a multi-column summary, check the grouping level and compare the row count with the number of distinct combinations. Also check whether filters belong in WHERE before grouping or in HAVING after grouping.
Exercise
What group contains an order whose customerid is 12 and whose status is NULL when the query uses GROUP BY customerid, status?
Show answer
It belongs to the single combination group (12, NULL). Other rows with customer_id 12 and status NULL join that same group.