Fill in the blanks for the following SQL query to retrieve the total number of orders placed by each customer:
SELECT Customer_ID, ______(OrderID) AS TotalOrders FROM Orders GROUP BY _______.
Fill in the blanks for the following SQL query to retrieve the total number of orders placed by each customer:
SELECT Customer_ID, ______(OrderID) AS TotalOrders FROM Orders GROUP BY _______.
Solution
COUNT() returns the number of rows in each group, so gives the number of orders per customer. GROUP BY Customer_ID forms one group for each customer, and COUNT is applied to each group separately.
The query becomes: SELECT Customer_ID, COUNT(OrderID) AS TotalOrders FROM Orders GROUP BY Customer_ID.
SUM and AVG: these work on the numeric values of OrderID (adding or averaging the ID numbers), not on how many orders there are.
TOTAL: this is not a standard SQL aggregate function. SQLite's total() adds values like SUM and does not count rows.
Related questions:
2026: 21 May Shift 1
2025: 27 May Shift 2
2025: 13 May Shift 2
2026: 26 May Shift 1