Find power users in the top decile of search counts with NTILE(10)

Quick Overview

Write a SQL query that splits all users into ten buckets with NTILE by total search count and returns the most active bucket. It tests left joins that keep zero-activity users, counting with NULLs, deterministic tie-breaking inside a window function, and filtering on a window result.

Find power users in the top decile of search counts with NTILE(10)

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

Divide all users into 10 groups with `NTILE(10)`, ordered by each user's total search count descending, and return only bucket 1, the most active group. Return `user_id`, `search_count` (the user's total number of searches) and `user_rank` (the bucket number), ordered by `search_count` descending, then `user_id` ascending. ```hint Who counts as a user Every row in `users` takes part in the bucketing, whether or not it has any searches. Check that each of them gets a row and the right count before you apply `NTILE`. ``` ### Input Table(s) `users` | Column | Type | Meaning | |---|---|---| | user_id | INT | Unique id of the user | | username | VARCHAR(100) | Username, NOT NULL | | signup_date | DATE | Sign-up date, NOT NULL | Primary key: `user_id`. `searches` | Column | Type | Meaning | |---|---|---| | search_id | INT | Unique id of the search | | user_id | INT | User who searched, NOT NULL, references `users.user_id` | | search_query | VARCHAR(255) | Text of the search, NOT NULL | | search_date | DATETIME (TIMESTAMP in PostgreSQL) | When the search ran, NOT NULL | Primary key: `search_id`. Foreign key: `user_id` references `users(user_id)`. ### Output Contract - Columns, in this order: `user_id`, `search_count`, `user_rank`. - `search_count` is the integer number of rows in `searches` for the user, 0 for a user with none. - `user_rank` is the `NTILE(10)` bucket, so it is 1 on every returned row. - Buckets are assigned in order of `search_count` descending, with ties broken by `user_id` ascending. - Rows ordered by `search_count` descending, then `user_id` ascending. `user_id` is unique, so the order is fully determined. ### Example `users` | user_id | username | signup_date | |---|---|---| | 1 | ana | 2024-01-01 | | 2 | ben | 2024-01-02 | | 3 | chloe | 2024-01-03 | | 4 | dev | 2024-01-04 | | 5 | eli | 2024-01-05 | | 6 | fay | 2024-01-06 | | 7 | gus | 2024-01-07 | | 8 | hana | 2024-01-08 | | 9 | ivan | 2024-01-09 | | 10 | jo | 2024-01-10 | | 11 | kai | 2024-01-11 | `searches` | search_id | user_id | search_query | search_date | |---|---|---|---| | 1 | 3 | laptop | 2024-02-01 09:15:00 | | 2 | 3 | usb c hub | 2024-02-01 09:20:00 | | 3 | 3 | monitor arm | 2024-02-02 18:05:00 | | 4 | 5 | running shoes | 2024-02-01 12:00:00 | | 5 | 5 | trail shoes | 2024-02-03 07:45:00 | | 6 | 8 | coffee grinder | 2024-02-02 08:30:00 | | 7 | 8 | pour over kettle | 2024-02-02 08:31:00 | | 8 | 1 | desk lamp | 2024-02-04 21:10:00 | Expected result: | user_id | search_count | user_rank | |---|---|---| | 3 | 3 | 1 | | 5 | 2 | 1 | There are 11 users, so `NTILE(10)` puts 2 users in bucket 1 and 1 user in each of buckets 2 to 10. In bucketing order, user 3 (3 searches) comes first, then users 5 and 8 (2 searches each, so user 5 goes first by id), then user 1, then the seven users with no searches. Bucket 1 is users 3 and 5. User 8 ties user 5 on count but lands in bucket 2. ### Constraints and Clarifications - `users` has at most 10,000 rows and `searches` at most 100,000 rows. - Every `user_id` in `searches` exists in `users`. - Users with no searches have `search_count` 0 and are part of the bucketing. They change how many users fall into each bucket. - With N users, `NTILE(10)` gives the first N mod 10 buckets one extra user, so bucket 1 holds N/10 rounded up (one user when N is below 10). - Ties in `search_count` are broken by `user_id` ascending when assigning buckets, so a user tied with someone in bucket 1 can still land in bucket 2. - Each `searches` row is one search. `search_query` and `search_date` do not affect the count.

Overview: Write a SQL query that splits all users into ten buckets with NTILE by total search count and returns the most active bucket. It tests left joins that keep zero-activity users, counting with NULLs, deterministic tie-breaking inside a window function, and filtering on a window result.

Nov 25, 2026
mediumData ScientistOtherData Manipulation (SQL/Python)
0
0

Divide all users into 10 groups with NTILE(10), ordered by each user's total search count descending, and return only bucket 1, the most active group. Return user_id, search_count (the user's total number of searches) and user_rank (the bucket number), ordered by search_count descending, then user_id ascending.

Input Table(s)

users

ColumnTypeMeaning
user_idINTUnique id of the user
usernameVARCHAR(100)Username, NOT NULL
signup_dateDATESign-up date, NOT NULL

Primary key: user_id.

searches

ColumnTypeMeaning
search_idINTUnique id of the search
user_idINTUser who searched, NOT NULL, references users.user_id
search_queryVARCHAR(255)Text of the search, NOT NULL
search_dateDATETIME (TIMESTAMP in PostgreSQL)When the search ran, NOT NULL

Primary key: search_id. Foreign key: user_id references users(user_id).

Output Contract

  • Columns, in this order: user_id , search_count , user_rank .
  • search_count is the integer number of rows in searches for the user, 0 for a user with none.
  • user_rank is the NTILE(10) bucket, so it is 1 on every returned row.
  • Buckets are assigned in order of search_count descending, with ties broken by user_id ascending.
  • Rows ordered by search_count descending, then user_id ascending. user_id is unique, so the order is fully determined.

Example

users

user_idusernamesignup_date
1ana2024-01-01
2ben2024-01-02
3chloe2024-01-03
4dev2024-01-04
5eli2024-01-05
6fay2024-01-06
7gus2024-01-07
8hana2024-01-08
9ivan2024-01-09
10jo2024-01-10
11kai2024-01-11

searches

search_iduser_idsearch_querysearch_date
13laptop2024-02-01 09:15:00
23usb c hub2024-02-01 09:20:00
33monitor arm2024-02-02 18:05:00
45running shoes2024-02-01 12:00:00
55trail shoes2024-02-03 07:45:00
68coffee grinder2024-02-02 08:30:00
78pour over kettle2024-02-02 08:31:00
81desk lamp2024-02-04 21:10:00

Expected result:

user_idsearch_countuser_rank
331
521

There are 11 users, so NTILE(10) puts 2 users in bucket 1 and 1 user in each of buckets 2 to 10. In bucketing order, user 3 (3 searches) comes first, then users 5 and 8 (2 searches each, so user 5 goes first by id), then user 1, then the seven users with no searches. Bucket 1 is users 3 and 5. User 8 ties user 5 on count but lands in bucket 2.

Constraints and Clarifications

  • users has at most 10,000 rows and searches at most 100,000 rows.
  • Every user_id in searches exists in users .
  • Users with no searches have search_count 0 and are part of the bucketing. They change how many users fall into each bucket.
  • With N users, NTILE(10) gives the first N mod 10 buckets one extra user, so bucket 1 holds N/10 rounded up (one user when N is below 10).
  • Ties in search_count are broken by user_id ascending when assigning buckets, so a user tied with someone in bucket 1 can still land in bucket 2.
  • Each searches row is one search. search_query and search_date do not affect the count.