Quick 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.

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.

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. Tables: - `users` (primary key `user_id`): `user_id` INTEGER, the user identifier; `user_name` VARCHAR(100), the user's name, not null; `email` VARCHAR(150), the user's email, not null; `icloud_storage_used_gb` DECIMAL(10,2), the iCloud storage in use in GB, not null. - `devices` (primary key `device_id`; `user_id` references `users`): `device_id` INTEGER, the device identifier; `user_id` INTEGER, the owner of the device; `device_name` VARCHAR(100), the device name, not null; `device_type` VARCHAR(50), the device type, not null. Output columns, in this order: `user_id`, `user_name`, `icloud_storage_used_gb`, `device_count`. Return one row per qualifying user, sorted 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. 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, and `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, and `icloud_storage_used_gb` is a non-negative decimal. `users` has at most 10,000 rows and `devices` at most 50,000 rows.

Tables

users(user_id INTEGER, user_name VARCHAR(100), email VARCHAR(150), icloud_storage_used_gb DECIMAL(10,2))

devices(device_id INTEGER, user_id INTEGER, device_name VARCHAR(100), device_type VARCHAR(50))

Hints

  1. 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.
  2. Both thresholds are strict, so a user with exactly 50.00 GB or exactly two devices does not qualify.

Community answers

Answer by ashokraminedi153

SELECT u.user_id, u.user_name, u.icloud_storage_used_gb, COUNT(d.device_id) AS device_count FROM users u JOIN devices d ON u.user_id = d.user_id WHERE u.icloud_storage_used_gb > 50 GROUP BY u.user_id, u.user_name, u.icloud_storage_used_gb HAVING COUNT(d.device_id) > 2 ORDER BY u.icloud_storage_used_gb DESC, u.user_id ASC;
AppleData ScientisteasyData Manipulation (SQL/Python)

Users With More Than Two Devices and Over 50 GB of iCloud Storage

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.

Tables:

  • users (primary key user_id): user_id INTEGER, the user identifier; user_name VARCHAR(100), the user's name, not null; email VARCHAR(150), the user's email, not null; icloud_storage_used_gb DECIMAL(10,2), the iCloud storage in use in GB, not null.
  • devices (primary key device_id; user_id references users): device_id INTEGER, the device identifier; user_id INTEGER, the owner of the device; device_name VARCHAR(100), the device name, not null; device_type VARCHAR(50), the device type, not null.

Output columns, in this order: user_id, user_name, icloud_storage_used_gb, device_count. Return one row per qualifying user, sorted 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.

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, and 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, and icloud_storage_used_gb is a non-negative decimal. users has at most 10,000 rows and devices at most 50,000 rows.

Expected Output

Tables

usersdevices
users
ColumnType
user_idINTEGER
user_nameVARCHAR(100)
emailVARCHAR(150)
icloud_storage_used_gbDECIMAL(10,2)
devices
ColumnType
device_idINTEGER
user_idINTEGER
device_nameVARCHAR(100)
device_typeVARCHAR(50)

Comments (1)

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Language
Run code to see results here.

Browse More Questions