A classic interview question about retention: "Of the customers who ordered in April, how many ordered again in May, and what share is that?" Each month gives a set of customers. The April set is the whole, and the April customers also found in May are the part.
Build each set in a WITH from the orders whose ordered date is LIKE '2026-04%' (or '2026-05%'), with SELECT DISTINCT customer_id so a customer who ordered twice is one row. Then read FROM the April set and LEFT JOIN the May set: every April customer stays, and one with no May order gets NULL on the May side. COUNT(*) counts every row, while COUNT of a column skips the NULLs. The same idea counts the products sold at least once:
query = (
"WITH sold AS (SELECT DISTINCT product_id FROM order_items) "
"SELECT COUNT(*) AS products, COUNT(sold.product_id) AS sold_once "
"FROM products "
"LEFT JOIN sold ON sold.product_id = products.id "
)
For the share, multiply by 100.0, not 100: in SQL two whole numbers divide to a whole number, so 2 * 100 / 3 is 66. ROUND(…, 1) keeps one decimal. And keep the LEFT: a plain JOIN drops the customers who didn't come back, so the share would always be 100.
For this lesson the hidden setup adds three orders to the shop:
id | customer_id | ordered | status 8 | 3 | 2026-05-12 | paid 9 | 1 | 2026-04-28 | paid 10 | 4 | 2026-04-11 | paid
With them, your query must print:
april_customers | ordered_again | percent 3 | 2 | 66.7 (1 row)
Your task: in query, build the sets april and may in a WITH, then select COUNT(*) as april_customers, COUNT(may.customer_id) as ordered_again and the share in percent, rounded to 1 decimal, as percent, FROM april with may LEFT JOINed on the customer id.
Assignment
Follow the Coding Tutorial to write the query one clause per line: the two sets, the two counts, the percent, FROM april and the LEFT JOIN.
Hint
Only the April set ends with a comma after its bracket: the main query follows the May set. COUNT(may.customer_id) counts only the April customers that found a May match.
Free plan: 30 tutor messages. PRO is unlimited.
