Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Top Percent Users by Activity

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

Your question is Top Percent Users by Activity. Start with the requirements and the three 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

Google Workspace analytics needs to identify the most active users during January 2025. Write a PostgreSQL query that calculates weighted activity, ranks every registered user, and returns the top 5%.

Requirements

  1. Include every user, including users with no qualifying activity.
  2. Count activity points from events joined to activity_types; events without a matching user must not contribute to any user.
  3. Restrict events to January 1 through January 31, 2025.
  4. Use a ranking function that preserves ties and gaps. Include every user tied at the top-5% boundary.
  5. Return user_id, display_name, activity_score, and activity_rank, ordered by score descending and user ID ascending.

Schema

users
ColumnTypeDescription
user_idPKINTEGERUnique registered user identifier
display_nameVARCHAR(100)User display name
created_atDATEDate the account was created
activity_types
ColumnTypeDescription
activity_type_idPKINTEGERUnique activity type identifier
event_nameVARCHAR(50)Name of the tracked activity
activity_pointsINTEGERWeighted activity points
activity_events
ColumnTypeDescription
event_idPKINTEGERUnique activity event identifier
user_idINTEGERAssociated user identifier
activity_type_idINTEGERAssociated activity type
occurred_atTIMESTAMPTime when the activity occurred
Tablesusersactivity_typesactivity_events
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results