Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Top 5 Users Per Category

MediumSQL · PostgreSQL00:00
Practice interviewer
In session
5 left
00:00

Your question is Top 5 Users Per Category. Start with the requirements and the two tables on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

Given a table of user activity, how would you identify the top 5 users per category?

Use the provided users and user_activity tables. Treat the number of activity records as each user's activity total. Exclude rows with a NULL category. Break ties by ascending user_id.

Output

  1. One row per selected user and category, with columns category, user_id, user_name, activity_count, and category_rank.
  2. Include at most five users per category, ordered by category, category_rank, and user_id.

Schema

users
ColumnTypeDescription
user_idPKINTUnique user identifier
user_nameVARCHAR(100)Display name of the user
user_activity
ColumnTypeDescription
activity_idPKINTUnique activity record identifier
user_idINTUser who generated the activity
categoryVARCHAR(50)Activity category
activity_atTIMESTAMPTimestamp when the activity occurred
Tablesusersuser_activity
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results