For the following table 'PRODUCT':
ID Name Category Price Quantity Supplier Date 301 Iphone Electronics 80000 25 Apple Inc. 2022-06-10 302 Samsung Electronics 60000 40 Samsung Elec. 2022-05-15 303 Nike Footwear 12000 50 Nike 2022-07-01 304 Dell Electronics 90000 10 Dell 2022-03-20 305 Puma Footwear 8000 60 Puma 2022-04-05
Which query retrieves the total number of products in each category?
For the following table 'PRODUCT':
| ID | Name | Category | Price | Quantity | Supplier | Date |
|---|---|---|---|---|---|---|
| 301 | Iphone | Electronics | 80000 | 25 | Apple Inc. | 2022-06-10 |
| 302 | Samsung | Electronics | 60000 | 40 | Samsung Elec. | 2022-05-15 |
| 303 | Nike | Footwear | 12000 | 50 | Nike | 2022-07-01 |
| 304 | Dell | Electronics | 90000 | 10 | Dell | 2022-03-20 |
| 305 | Puma | Footwear | 8000 | 60 | Puma | 2022-04-05 |
Which query retrieves the total number of products in each category?
Solution
✅ Correct Option: 3
Option 3 groups the rows by Category with GROUP BY, and COUNT(*) counts the rows in each group. It also selects Category, so each count is labelled with its category.
For the given table, it returns:
| Category | TotalProducts |
|---|---|
| Electronics | 3 |
| Footwear | 2 |
Option 1: counts all rows in the table (5), with no grouping by category.
Option 2: groups correctly, but does not select Category, so the counts have no labels. COUNT(Quantity) would also skip rows where Quantity is NULL.
Option 4: mixes a non-aggregated column (Category) with COUNT(ID) without a GROUP BY, which is invalid in standard SQL.
Related questions:
2026: 29 May Shift 2
2026: 11 May Shift 2