Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

SQL for Top Activity Users

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

Your question is SQL for Top Activity Users. 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

The WWT Advanced Technology Center analytics team wants to identify the most active users of its internal technology portal. Write a PostgreSQL query that returns the top 5% of users based on their number of qualifying activity events during January 2025.

Requirements

  1. Include every user when calculating the population, including users with zero activity.
  2. Count only events from 2025-01-01 through 2025-01-31, inclusive.
  3. Return the smallest whole-number group representing at least 5% of users. For this dataset, that is one user.
  4. Return user_id, user_name, activity_count, and the user's activity rank, ordered by rank ascending. Break ties by user_id ascending.

Schema

users
ColumnTypeDescription
user_idPKINTUnique user identifier
user_nameVARCHAR(100)User display name
emailVARCHAR(255)User email address
user_activity
ColumnTypeDescription
activity_idPKINTUnique activity event identifier
user_idINTUser associated with the activity event
event_typeVARCHAR(50)Type of portal activity
occurred_atTIMESTAMPActivity event timestamp
Tablesusersuser_activity
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results