Find Users with an Active Subscription and More Than Two Distinct Devices
Company: Apple
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: easy
Interview Round: Other
A product team wants one view that combines account, subscription and device data. You are given three tables: `Users`, `Subscriptions` and `Devices`, all linked by `user_id`.
Find users who have an active subscription (`status = 'active'`) and more than 2 distinct registered devices. Return `user_id`, `username`, `email`, and the distinct device count as `active_device_count`, ordered by `active_device_count` descending, then `user_id` ascending.
```hint Watch the row counts
A user can have several subscription rows, and the same device can be registered more than once. Think about what each of those does to the number of rows per user after the joins.
```
### Input Table(s)
`Users`
| Column | Type | Meaning |
|---|---|---|
| user_id | INT | User identifier (primary key) |
| username | VARCHAR(100) | The user's name, never NULL |
| email | VARCHAR(255) | The user's email address, never NULL |
`Subscriptions`
| Column | Type | Meaning |
|---|---|---|
| subscription_id | INT | Subscription identifier (primary key) |
| user_id | INT | Owner of the subscription, a foreign key to `Users.user_id` |
| status | VARCHAR(20) | One of `'active'`, `'expired'`, `'cancelled'`, never NULL |
`Devices`
| Column | Type | Meaning |
|---|---|---|
| device_id | INT | Device identifier, never NULL |
| user_id | INT | User the device is registered to, a foreign key to `Users.user_id` |
`Devices` has one row per registration and no primary key. If a user registers the same device twice, the pair (`user_id`, `device_id`) appears twice.
### Output Contract
Return these columns, in this order:
| Column | Meaning |
|---|---|
| user_id | The user's id |
| username | The user's name |
| email | The user's email address |
| active_device_count | Number of distinct `device_id` values registered to the user |
Return one row per qualifying user. Sort by `active_device_count` descending, then by `user_id` ascending. Because `user_id` is unique, this order is fully determined.
### Example
`Users`
| user_id | username | email |
|---|---|---|
| 1 | alice | alice@example.com |
| 2 | bob | bob@example.com |
| 3 | carol | carol@example.com |
| 4 | dave | dave@example.com |
| 5 | erin | erin@example.com |
`Subscriptions`
| subscription_id | user_id | status |
|---|---|---|
| 101 | 1 | active |
| 102 | 2 | expired |
| 103 | 2 | active |
| 104 | 3 | cancelled |
| 105 | 4 | active |
| 106 | 4 | active |
| 107 | 5 | active |
`Devices`
| device_id | user_id |
|---|---|
| 11 | 1 |
| 12 | 1 |
| 13 | 1 |
| 13 | 1 |
| 21 | 2 |
| 22 | 2 |
| 23 | 2 |
| 24 | 2 |
| 31 | 3 |
| 32 | 3 |
| 33 | 3 |
| 41 | 4 |
| 42 | 4 |
| 43 | 4 |
| 51 | 5 |
| 52 | 5 |
Expected result:
| user_id | username | email | active_device_count |
|---|---|---|---|
| 2 | bob | bob@example.com | 4 |
| 1 | alice | alice@example.com | 3 |
| 4 | dave | dave@example.com | 3 |
Bob has an active subscription next to an expired one and 4 distinct devices. Alice registered device 13 twice, so she has 4 rows but 3 distinct devices. Dave has two active subscriptions and 3 devices, which still counts as 3. Alice and Dave tie at 3, and the lower `user_id` comes first. Carol has 3 devices but only a cancelled subscription. Erin has an active subscription but only 2 devices.
### Constraints and Clarifications
- `Users` and `Subscriptions` have at most 10,000 rows each, and `Devices` has at most 50,000 rows.
- `status` is always one of `'active'`, `'expired'`, `'cancelled'`, in lowercase.
- A user has an active subscription when at least one of their `Subscriptions` rows has `status = 'active'`. A user may have several subscription rows, including more than one active row.
- Users with no subscription rows, or with no devices, do not qualify.
- "More than 2" is strict: a user with exactly 2 distinct devices is excluded.
- The same `device_id` may be registered to more than one user. Count it once for each user it is registered to.
Overview: Write a SQL query that combines users, subscriptions and device registrations to find users with an active subscription and more than two distinct devices, returning each user's device count. It tests multi-table joins, counting distinct values when joins multiply rows, and deterministic ordering.
A product team wants one view that combines account, subscription and device data. You are given three tables, `users`, `subscriptions` and `devices`, all linked by `user_id`.
Find users who have an active subscription (`status = 'active'`) and more than 2 distinct registered devices. Return `user_id`, `username`, `email`, and the distinct device count as `active_device_count`, ordered by `active_device_count` descending, then `user_id` ascending.
Tables:
- `users(user_id INTEGER PRIMARY KEY, username VARCHAR(100) NOT NULL, email VARCHAR(255) NOT NULL)`: one row per user. `username` and `email` are never NULL.
- `subscriptions(subscription_id INTEGER PRIMARY KEY, user_id INTEGER, status VARCHAR(20) NOT NULL)`: `user_id` is the owner of the subscription and refers to `users.user_id`. `status` is always one of `'active'`, `'expired'`, `'cancelled'`, in lowercase.
- `devices(device_id INTEGER NOT NULL, user_id INTEGER)`: one row per registration, with no primary key. `user_id` is the user the device is registered to and refers to `users.user_id`. If a user registers the same device twice, the pair (`user_id`, `device_id`) appears twice.
Rules:
- A user has an active subscription when at least one of their `subscriptions` rows has `status = 'active'`. A user may have several subscription rows, including more than one active row.
- Users with no subscription rows, or with no devices, do not qualify.
- "More than 2" is strict: a user with exactly 2 distinct devices is excluded.
- The same `device_id` may be registered to more than one user. Count it once for each user it is registered to.
- `users` and `subscriptions` have at most 10,000 rows each, and `devices` has at most 50,000 rows.
Output columns, in this order: `user_id`, `username`, `email`, `active_device_count` (the number of distinct `device_id` values registered to the user). Return one row per qualifying user, sorted by `active_device_count` descending, then by `user_id` ascending. Because `user_id` is unique, this order is fully determined.
Tables
users(user_id INTEGER, username VARCHAR(100), email VARCHAR(255))
subscriptions(subscription_id INTEGER, user_id INTEGER, status VARCHAR(20))
devices(device_id INTEGER, user_id INTEGER)
Hints
- A user can have several subscription rows, and the same device can be registered more than once. Think about what each of those does to the number of rows per user after the joins.
- One active row is enough for a user to have an active subscription, whatever their other rows say, and a user needs at least 3 distinct devices to pass the "more than 2" rule.
Community answers
Answer by ashokraminedi153
SELECT
u.user_id,
u.username,
u.email,
COUNT(DISTINCT d.device_id) AS active_device_count
FROM Users u
JOIN Subscriptions s
ON u.user_id = s.user_id
JOIN Devices d
ON u.user_id = d.user_id
WHERE s.status = 'active'
GROUP BY
u.user_id,
u.username,
u.email
HAVING COUNT(DISTINCT d.device_id) > 2
ORDER BY active_device_count DESC, u.user_id ASC;