Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Top 5 Users by Activity

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

Your question is Top 5 Users by Activity. 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

Tech(x) wants to identify its most active users in the Tech(x) product during January 2025. Write a PostgreSQL query that includes users with no qualifying activity, counts their activity events, and returns the top five users.

Requirements

  1. Consider only activity recorded from 2025-01-01 00:00:00+00 through 2025-01-31 23:59:59+00.
  2. Use a LEFT JOIN so users without qualifying activity remain eligible with an activity count of zero.
  3. Return the five highest activity counts, breaking ties by active days descending and then user_id ascending.

Schema

users
ColumnTypeDescription
user_idPKINTEGERUnique Tech(x) user identifier
usernameVARCHAR(100)Tech(x) account name
user_activity
ColumnTypeDescription
activity_idPKINTEGERUnique activity event identifier
user_idINTEGERUser associated with the activity event
event_typeVARCHAR(50)Type of Tech(x) activity
occurred_atTIMESTAMPTZTimestamp 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