User Roster

An easy SQL interview practice problem on DataDriven. Write the query and run it against a real database, with instant feedback.

Domain
SQL
Difficulty
easy
Seniority
L3
Asked in
Tata Consultancy Services

Problem

The growth team is building a retention dashboard and needs to understand the distribution of account states. For each account status, show the number of users and what share of the total user base that status represents. Round the share to one decimal place and list from the largest group to the smallest, with equally sized groups ordered alphabetically by status name.

Which account states are bleeding users?

Tables

users

Columns: user_id INTEGER, username TEXT, email TEXT, signup_date TEXT, account_status TEXT, age_bucket TEXT

users: 10 of 200 rows
user_idusernameemailsignup_dateaccount_statusage_bucket
100alicealice@example.com2025-02-02inactive25-34
197aaron42aaron42@example.com2026-03-03suspended35-44
294 ameliaamelia@example.com2024-04-04pending_verification45-54
391arjun arjun@example.com2025-05-05active55-64
488ava99ava99@example.com2026-06-06inactive65+
585andrew_kandrew_k@example.com2024-07-07suspendedNULL
682anikaanika@example.com2025-08-08 16:34:00pending_verification18-24
779aidenaiden@example.com2026-09-09 14:53:00active25-34
876aria_baria_b@example.com2024-10-10inactive35-44
973brianbrian@example.com2025-11-11suspended45-54

Expected output

First 4 of 4 rows
account_statususer_countshare_pct
active5025
inactive5025
pending_verification5025
suspended5025

Worked solution and explanation

Why this problem exists in real interviews

This tests GROUP BY with a window function for computing both a count and a percentage in a single query. Interviewers check whether you can compute a group's share of the total without a subquery.


Break down the requirements

Step 1: Count users per status

GROUP BY account_status with COUNT(*) produces the count per group.

Step 2: Compute share of total

Divide each group's count by the total user count (via a subquery or window function), multiply by 100, and round to 1 decimal place.

Step 3: Order by count descending

ORDER BY user_count DESC lists from largest group to smallest.


The solution

Group counts with percentage of total
SELECT
    account_status,
    COUNT(*) AS user_count,
    ROUND(COUNT(*) * 100.0 / (SELECT COUNT(*) FROM users), 1) AS share_pct
FROM users
GROUP BY account_status
ORDER BY user_count DESC, account_status

Common follow-up questions