DataLane

23. Customers who bought every category

hardProNot started~18 minJoinsAggregationSubqueries

Asked in screens shaped like: Amazon, Instacart, Walmart

Marketing wants customers who have purchased at least one product in every product category. Count distinct categories from completed orders only. Return customer_id ascending.

Requirements

  • Columns: customer_id
  • Only completed orders
  • COUNT(DISTINCT products.category) equals the number of categories in products
  • Order by customer_id

Expected output

customer_id

Row order is graded, so ORDER BY matters here.

SQL editor

Loading SQL engine…

Ctrl/Cmd + Enter submits

Run your query to see the result set.

Pro problem

Hard problems unlock with Pro

Easy pads stay free. Medium and hard SQL and Python problems — editor, tests, hints, and solutions — open after you upgrade to Pro or coaching.

See plansPractice free easy problems
↑↓ navigate openesc close