Skip to main contentSkip to solution

For the following table 'PRODUCT':

IDNameCategoryPriceQuantitySupplierDate
301IphoneElectronics8000025Apple Inc.2022-06-10
302SamsungElectronics6000040Samsung Elec.2022-05-15
303NikeFootwear1200050Nike2022-07-01
304DellElectronics9000010Dell2022-03-20
305PumaFootwear800060Puma2022-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:

CategoryTotalProducts
Electronics3
Footwear2

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.

Keyboard Shortcuts

  • Left arrow: Previous question
  • Right arrow: Next question
  • S key: Jump to solution
  • Q key: Jump to question