Microsoft-style Company ChallengeHardVerified answerSQLite live

Top Active Users by Usage

For the top 10 users by total usage, show their country, number of distinct products subscribed to, and total usage minutes.

  • Joins
  • Subqueries
  • Aggregation
  • Sorting
  • Top-N

Challenge brief

Understand the request

Enterprise Analytics is building an executive leaderboard of the most active Microsoft customers ranked by total product usage.

Join users, subscriptions, and usage_logs to build a per-user activity summary, ordered by usage descending.

Return

  • user_id
  • country
  • products (distinct subscribed product count)
  • total_usage

Constraints

  • Count distinct subscribed products independently from usage totals
  • Do not multiply usage when a user has multiple subscription records
  • Return at most 10 users with deterministic tie handling

Data you will use

Review the relevant tables before deciding how to join, filter, or aggregate them.

subscriptions

  • subscription_idINTEGER
  • user_idINTEGER
  • product_idINTEGER
  • start_dateDATE
  • end_dateDATE
  • priceINTEGER

usage_logs

  • log_idINTEGER
  • user_idINTEGER
  • product_idINTEGER
  • usage_dateDATE
  • usage_minutesINTEGER

users

  • user_idINTEGER
  • countryTEXT
  • signup_dateDATE

Hints, when you need them

Open one clue at a time so you still do the reasoning.

Hint 1

This requires three tables: users (for country), subscriptions (for product count), usage_logs (for usage minutes). JOIN all three on user_id. COUNT(DISTINCT product_id) from subscriptions, SUM(usage_minutes) from usage_logs.

Hint 2

FROM users u JOIN subscriptions s ON u.user_id = s.user_id JOIN usage_logs ul ON u.user_id = ul.user_id. GROUP BY u.user_id, u.country. COUNT(DISTINCT s.product_id), SUM(ul.usage_minutes). ORDER BY total_usage DESC LIMIT 10.

Hint 3

Build question 21 from the required result grain: choose the driving table, add only the joins and filters needed for that grain, then apply aggregation and deterministic ordering.

Verified SQL answer

Attempt the problem first, then compare structure and reasoning—not just syntax.

Reveal solution and explanation
WITH subscribed_products AS (SELECT user_id, COUNT(DISTINCT product_id) AS products FROM subscriptions GROUP BY user_id), usage_totals AS (SELECT user_id, SUM(usage_minutes) AS total_usage FROM usage_logs GROUP BY user_id) SELECT u.user_id, u.country, sp.products, ut.total_usage FROM users u INNER JOIN subscribed_products sp ON u.user_id = sp.user_id INNER JOIN usage_totals ut ON u.user_id = ut.user_id ORDER BY ut.total_usage DESC, u.user_id ASC LIMIT 10;

Why this works

Aggregate subscription breadth and usage independently at user grain before joining them. This prevents multiple subscription records from multiplying usage minutes.

Success check

10 users — user 8 (US, 1 product, 580 min) leads; user 9 (DE, 1 product, 100 min) is 10th

Expected result

Use this output to verify values, aliases, ordering, and row count.

user_idcountryproductstotal_usage
8US1580
12CA1500
1US2210
7IN1210
3US1200
5CA1180
4UK1150
11US1130
6US1110
9DE1100

Learn the concepts behind this answer

Strengthen your understanding with these targeted learning topics:

Continue practicing

SQL Interview Practice

Return to the complete interview preparation experience.