Aggregation and Grouping · Problem 28 of 118

Orders and Buyers

EasyAggregation and GroupingShop data

Return one row with two numbers: orders (how many orders exist) and buyers (how many different customers have placed at least one order).

Hint 1

COUNT(DISTINCT column) counts each value once.

Reference solution

SELECT COUNT(*) AS orders,
       COUNT(DISTINCT customer_id) AS buyers
FROM orders;

COUNT(*) counts rows. COUNT(column) counts the rows where the column is not NULL. COUNT(DISTINCT column) counts the different non-NULL values. Three similar-looking expressions, three different questions: "how many orders", "how many orders have a customer", "how many customers".