STOCK has five records with (Category, Qty): (Pen, 12), (Paper, 40), (Pen, 8), (Ink, 5), (Paper, 10). What does this query return?
SELECT Category, SUM(Qty) FROM STOCK GROUP BY Category;
- GROUP BY Category puts the records into three groups: Pen, Paper and Ink.
- SUM(Qty) is worked out for each group. Pen: 12 + 8 = 20. Paper: 40 + 10 = 50. Ink: 5.
- The query returns one row for each group, not one row for each record.
Answer: Three rows: Pen 20, Paper 50, Ink 5.