Find users with more than two devices and over 50 GB of iCloud storage
Company: Apple
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: easy
Interview Round: Other
Find users who own more than two devices and use more than 50 GB of iCloud storage. Return `user_id`, `user_name`, `icloud_storage_used_gb`, and the user's total number of devices as `device_count`, ordered by `icloud_storage_used_gb` descending, then `user_id` ascending.
```hint Where each filter belongs
One condition is about a single column of the user row and one is about a count over many device rows. Decide which can be checked before grouping and which has to wait for the aggregate.
```
### Input Table(s)
`users` (primary key `user_id`)
| Column | Type | Meaning |
|---|---|---|
| user_id | INT | User identifier |
| user_name | VARCHAR(100) | User's name, not null |
| email | VARCHAR(150) | User's email, not null |
| icloud_storage_used_gb | DECIMAL(10, 2) | iCloud storage in use, in GB, not null |
`devices` (primary key `device_id`; `user_id` references `users`)
| Column | Type | Meaning |
|---|---|---|
| device_id | INT | Device identifier |
| user_id | INT | Owner of the device |
| device_name | VARCHAR(100) | Device name, not null |
| device_type | VARCHAR(50) | Device type, not null |
### Output Contract
- Columns, in this order: `user_id`, `user_name`, `icloud_storage_used_gb`, `device_count`.
- One row per qualifying user.
- Sort by `icloud_storage_used_gb` descending, then `user_id` ascending. `user_id` is unique, so the order is fully determined.
- Return `icloud_storage_used_gb` as stored, with no rounding. `device_count` is an integer.
### Example
`users`
| user_id | user_name | email | icloud_storage_used_gb |
|---|---|---|---|
| 1 | Ana | ana@example.com | 75.00 |
| 2 | Ben | ben@example.com | 50.00 |
| 3 | Chloe | chloe@example.com | 210.25 |
| 4 | Dev | dev@example.com | 75.00 |
| 5 | Eli | eli@example.com | 120.50 |
`devices`
| device_id | user_id | device_name | device_type |
|---|---|---|---|
| 101 | 1 | iPhone 15 | phone |
| 102 | 1 | MacBook Air | laptop |
| 103 | 1 | iPad mini | tablet |
| 104 | 2 | iPhone 14 | phone |
| 105 | 2 | iPad Air | tablet |
| 106 | 2 | Apple Watch | watch |
| 107 | 3 | iPhone 15 Pro | phone |
| 108 | 3 | iMac | desktop |
| 109 | 4 | iPhone 13 | phone |
| 110 | 4 | iPad Pro | tablet |
| 111 | 4 | MacBook Pro | laptop |
| 112 | 4 | Apple Watch | watch |
| 113 | 5 | iPhone 15 | phone |
| 114 | 5 | MacBook Air | laptop |
| 115 | 5 | Apple TV | tv |
Expected result:
| user_id | user_name | icloud_storage_used_gb | device_count |
|---|---|---|---|
| 5 | Eli | 120.50 | 3 |
| 1 | Ana | 75.00 | 3 |
| 4 | Dev | 75.00 | 4 |
Ben has three devices but uses exactly 50.00 GB, which is not more than 50. Chloe uses the most storage but owns only two devices. Ana and Dev tie on storage, so the lower `user_id` comes first.
### Constraints and Clarifications
- Both thresholds are strict: a user with exactly two devices or exactly 50.00 GB does not qualify.
- `device_count` is the number of rows in `devices` for the user. `device_id` is unique, so each device is counted once.
- A user with no devices has a count of zero and never qualifies.
- No column used by the query is NULL.
- Each device belongs to exactly one user.
- `users` has at most 10,000 rows and `devices` at most 50,000 rows.
- `icloud_storage_used_gb` is a non-negative decimal.
Overview: Write a SQL query that finds users who own more than two devices and use more than 50 GB of cloud storage, returning each user's device count sorted by storage. It tests joins, GROUP BY with HAVING, strict threshold filters, and deterministic tie-breaking in ORDER BY.