Top 3 Pin Categories by Engagement and the Top 3 Users in Each

Quick Overview

A SQL question that ranks pin categories by total clicks plus saves and then ranks the users inside each top category by the engagement on their own pins. It tests chained CTEs, RANK() with PARTITION BY, filtering on a window result, and tie rules that cap each list at three.

Top 3 Pin Categories by Engagement and the Top 3 Users in Each

Company: Pinterest

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

Pins belong to categories, and each pin collects clicks and saves. Engagement is `clicks + saves`. Find the top 3 pin categories by total engagement and, within each of those categories, the top 3 users by their engagement in that category. A user's engagement in a category is the total engagement on the pins that user created in that category. Return `Category`, `Total_Engagement`, `Category_Rank`, `Top_User` (the username), `User_Engagement` and `User_Rank`. Rank with `RANK()` window functions, keep at most 3 categories and at most 3 users per category, and order by `Category_Rank`, then `User_Rank`, ascending. ```hint Two levels of totals The category total and the per-user total are separate aggregations over the same joined rows. A window function's result can only be filtered one query level above the place where it is computed. ``` ### Input Table(s) Table `users`. Primary key: `user_id`. | Column | Type | Meaning | |---|---|---| | user_id | INT | Unique user id | | username | VARCHAR(100) | Display name | Table `pins`, one row per pin. Primary key: `pin_id`. `user_id` references `users.user_id`. | Column | Type | Meaning | |---|---|---| | pin_id | INT | Unique pin id | | user_id | INT | User who created the pin | | category | VARCHAR(100) | Category of the pin | Table `engagements`. No primary key. `pin_id` references `pins.pin_id`, and a pin can have zero, one or many rows. | Column | Type | Meaning | |---|---|---| | pin_id | INT | Pin the engagement was recorded on | | clicks | INT | Number of clicks, non-negative | | saves | INT | Number of saves, non-negative | ### Output Contract - Columns, in order and with this exact casing: `Category`, `Total_Engagement`, `Category_Rank`, `Top_User`, `User_Engagement`, `User_Rank`. In PostgreSQL the aliases need double quotes to keep their case. - `Total_Engagement` is `SUM(clicks + saves)` over every engagement row of every pin in the category. - `Category_Rank` is `RANK()` over `Total_Engagement` descending, with ties broken by `category` ascending. Keep ranks 1 to 3. - `User_Engagement` is `SUM(clicks + saves)` over the engagement rows of the pins the user created in that category. - `User_Rank` is `RANK()` within the category over `User_Engagement` descending, with ties broken by `user_id` ascending. Keep ranks 1 to 3. - One row per (category, user) pair, ordered by `Category_Rank` ascending, then `User_Rank` ascending. - All values are integers. No rounding. ### Example Input `users`: | user_id | username | |---|---| | 1 | ana | | 2 | ben | | 3 | cara | | 4 | dev | | 5 | eli | Input `pins`: | pin_id | user_id | category | |---|---|---| | 10 | 1 | Food | | 11 | 2 | Food | | 12 | 3 | Food | | 13 | 4 | Food | | 14 | 1 | Travel | | 15 | 5 | Travel | | 16 | 2 | Decor | | 17 | 3 | Art | | 18 | 4 | Art | | 19 | 5 | Food | Input `engagements`: | pin_id | clicks | saves | |---|---|---| | 10 | 30 | 10 | | 11 | 20 | 5 | | 12 | 15 | 10 | | 13 | 5 | 5 | | 14 | 40 | 20 | | 15 | 10 | 0 | | 15 | 0 | 5 | | 16 | 30 | 20 | | 17 | 20 | 10 | | 18 | 10 | 10 | Expected output: | Category | Total_Engagement | Category_Rank | Top_User | User_Engagement | User_Rank | |---|---|---|---|---|---| | Food | 100 | 1 | ana | 40 | 1 | | Food | 100 | 1 | ben | 25 | 2 | | Food | 100 | 1 | cara | 25 | 3 | | Travel | 75 | 2 | ana | 60 | 1 | | Travel | 75 | 2 | eli | 15 | 2 | | Art | 50 | 3 | cara | 30 | 1 | | Art | 50 | 3 | dev | 20 | 2 | The category totals are Food 100, Travel 75, Art 50 and Decor 50. Art and Decor tie, and Art takes rank 3 on the name tiebreaker, so Decor is left out. In Food, ben and cara tie at 25 and ben ranks higher on `user_id`. Dev is fourth in Food with 10, and eli's pin 19 has no engagement rows at all. Pin 15 has two engagement rows, so eli's Travel engagement is 10 + 5 = 15. ### Constraints and Clarifications - `users` has at most 10,000 rows. `pins` and `engagements` have at most 100,000 rows each. - `clicks` and `saves` are non-negative. No column is NULL, every `pins.user_id` exists in `users`, and every `engagements.pin_id` exists in `pins`. - The tiebreakers make both rankings strict, so the output never has more than 3 categories or more than 3 users per category. - A category with no engagement rows has no total and is not ranked. If fewer than 3 categories have engagement, return those. If a category has fewer than 3 users with engagement rows, return those users. - A pin whose engagement rows sum to 0 still counts, so a user can appear with `User_Engagement` equal to 0. - Two users can share a username. Users are told apart by `user_id`.

Overview: A SQL question that ranks pin categories by total clicks plus saves and then ranks the users inside each top category by the engagement on their own pins. It tests chained CTEs, RANK() with PARTITION BY, filtering on a window result, and tie rules that cap each list at three.

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

Pins belong to categories, and each pin collects clicks and saves. Engagement is clicks + saves. Find the top 3 pin categories by total engagement and, within each of those categories, the top 3 users by their engagement in that category. A user's engagement in a category is the total engagement on the pins that user created in that category.

Return Category, Total_Engagement, Category_Rank, Top_User (the username), User_Engagement and User_Rank. Rank with RANK() window functions, keep at most 3 categories and at most 3 users per category, and order by Category_Rank, then User_Rank, ascending.

Input Table(s)

Table users. Primary key: user_id.

ColumnTypeMeaning
user_idINTUnique user id
usernameVARCHAR(100)Display name

Table pins, one row per pin. Primary key: pin_id. user_id references users.user_id.

ColumnTypeMeaning
pin_idINTUnique pin id
user_idINTUser who created the pin
categoryVARCHAR(100)Category of the pin

Table engagements. No primary key. pin_id references pins.pin_id, and a pin can have zero, one or many rows.

ColumnTypeMeaning
pin_idINTPin the engagement was recorded on
clicksINTNumber of clicks, non-negative
savesINTNumber of saves, non-negative

Output Contract

  • Columns, in order and with this exact casing: Category , Total_Engagement , Category_Rank , Top_User , User_Engagement , User_Rank . In PostgreSQL the aliases need double quotes to keep their case.
  • Total_Engagement is SUM(clicks + saves) over every engagement row of every pin in the category.
  • Category_Rank is RANK() over Total_Engagement descending, with ties broken by category ascending. Keep ranks 1 to 3.
  • User_Engagement is SUM(clicks + saves) over the engagement rows of the pins the user created in that category.
  • User_Rank is RANK() within the category over User_Engagement descending, with ties broken by user_id ascending. Keep ranks 1 to 3.
  • One row per (category, user) pair, ordered by Category_Rank ascending, then User_Rank ascending.
  • All values are integers. No rounding.

Example

Input users:

user_idusername
1ana
2ben
3cara
4dev
5eli

Input pins:

pin_iduser_idcategory
101Food
112Food
123Food
134Food
141Travel
155Travel
162Decor
173Art
184Art
195Food

Input engagements:

pin_idclickssaves
103010
11205
121510
1355
144020
15100
1505
163020
172010
181010

Expected output:

CategoryTotal_EngagementCategory_RankTop_UserUser_EngagementUser_Rank
Food1001ana401
Food1001ben252
Food1001cara253
Travel752ana601
Travel752eli152
Art503cara301
Art503dev202

The category totals are Food 100, Travel 75, Art 50 and Decor 50. Art and Decor tie, and Art takes rank 3 on the name tiebreaker, so Decor is left out. In Food, ben and cara tie at 25 and ben ranks higher on user_id. Dev is fourth in Food with 10, and eli's pin 19 has no engagement rows at all. Pin 15 has two engagement rows, so eli's Travel engagement is 10 + 5 = 15.

Constraints and Clarifications

  • users has at most 10,000 rows. pins and engagements have at most 100,000 rows each.
  • clicks and saves are non-negative. No column is NULL, every pins.user_id exists in users , and every engagements.pin_id exists in pins .
  • The tiebreakers make both rankings strict, so the output never has more than 3 categories or more than 3 users per category.
  • A category with no engagement rows has no total and is not ranked. If fewer than 3 categories have engagement, return those. If a category has fewer than 3 users with engagement rows, return those users.
  • A pin whose engagement rows sum to 0 still counts, so a user can appear with User_Engagement equal to 0.
  • Two users can share a username. Users are told apart by user_id .