Back to Practice
#0356

Prevent Join Duplicate Explosion

MediumSQL24 min20 XP

Problem

Return user_id, orders_count, and tickets_count without multiplying rows between orders and tickets.

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(order_id INTEGER, user_id INTEGER); tickets(ticket_id INTEGER, user_id INTEGER)

Examples

Example 1
Inputuser 1 has 2 orders and 3 tickets
Output2 and 3, not 6

Pre-aggregation avoids order-ticket row multiplication.

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 has 2 orders and 3 tickets
Expected[[1,2,3],[2,1,1]]

Pre-aggregation avoids order-ticket row multiplication.

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