GROUP BY
Six rows in orders, two from each of three buyers, and the whole-table total came back as one number: 188.50. Ask for the total per buyer instead, and three numbers come back, one each.
- ▸Six
ordersrows, totalling 188.50, split by buyer into 59.50, 99.00, and 30.00, one row each. - ▸List a bare column beside an aggregate and the query is rejected: one slot, several rival values.
- ▸Split also by status and the three buyer piles become five, one row per buyer-and-status pair.
Six rows sit in orders. Add every amount together and you get one number for the whole table: 188.50. But the table also records who placed each order, in the user_id column, and the six rows belong to three buyers: two rows for buyer 1, two for buyer 2, two for buyer 3.
Ask a sharper question. Not "what did everyone spend?" but "what did each buyer spend?". Now one number is not enough. Buyer 1's two amounts add to 59.50. Buyer 2's add to 99.00 (one of their rows is a blank that SUM skips). Buyer 3's add to 30.00 (same blank story). The single whole-table total has split into three totals, one per buyer.
The engine gets there by splitting the six rows into three piles by user_id, then running the same SUM once inside each pile. The vertical collapse from the previous topic still happens, many values folding to one, but now it happens three times over, once per pile, instead of once for the entire table. Three piles in, three result rows out, and the three totals add back to the original 188.50.