11. Find Repeat Customers
Difficulty: Easy · Topics: Aggregations, Filtering & Selection
orders has one row per order line: order_id, customer_id, product and amount. An order can have several lines.
For every customer with at least 2 different orders, return customer_id, orders (distinct order ids), products (distinct products) and total (sum of amount).
Row order does not matter; column names must match. Your code is graded on 3 test cases, including hidden edge cases.
Sample data
orders
| order_id | customer_id | product | amount |
|---|---|---|---|
| 1 | c1 | pen | 10 |
| 1 | c1 | ink | 5 |
| 2 | c1 | pen | 10 |
| 3 | c2 | book | 300 |
| 4 | c3 | pen | 10 |
| 5 | c3 | pen | 10 |
Expected output
| customer_id | orders | products | total |
|---|---|---|---|
| c1 | 2 | 2 | 25 |
| c3 | 2 | 1 | 20 |
Hints
Hint 1
F.countDistinct("order_id") counts orders, not lines.Hint 2
Aggregate first, then filter on the aggregated column (SQL's HAVING).Hint 3
Several aggregates can go in oneagg call.Learn the concepts
- groupBy + agg · 3 min read. Aggregate rows per group: counts, sums, averages, and how count(*) differs from count(col).
- filter / where · 4 min read. Keep the rows that match a condition, and the null and operator traps that silently drop data.
PySpark functions you'll practise
- groupBy
- agg
- sum
- countDistinct
- filter
Related problems
- Customers Who Bought Every Product · Hard · Joins
- Departments with Large Teams · Easy · Aggregations
- Find Partitions That Need Compaction · Easy · Aggregations
- Rebuild a Table from Its Transaction Log · Medium · Joins
- Files VACUUM Can Delete · Medium · Joins
Browse
Topics: Window Functions · Joins · Aggregations · Pivot, Unpivot & Rollup · Arrays · Null Handling · Conditional Logic · Dates · Filtering & Selection · Strings
Difficulty: Easy · Medium · Hard · PySpark interview roadmap · Learn · All problems