Back to Practice
#0325

Distinct Products Per User

EasySQL15 min10 XP

Problem

Return user_id and distinct_products purchased by each user.

Why This Matters

SQL interviews usually test whether you can turn a business question into the right rows, grouping level, join type, and edge-case behavior.

Schema

orders(user_id INTEGER, product TEXT)

Examples

Example 1
Inputuser 1 bought Book twice and Pen once
Outputuser 1 -> 2

COUNT(DISTINCT product) counts Book once for user 1.

Constraints

  • Return only the requested columns.
  • Use deterministic ORDER BY when multiple rows are returned.
  • Assume SQLite-compatible SQL unless the prompt states otherwise.
CodeSQL
Visible browser tests run here when available.
Testcases1 visible / 3 hidden categories
Core SQL case
Inputuser 1 bought Book twice and Pen once
Expected[[1,2],[2,1]]

COUNT(DISTINCT product) counts Book once for user 1.

Hidden Test Categories
Empty or missing matchesDuplicate rows or tiesNULL values where the pattern commonly breaks