Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Top 5% Users by Engagement

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

Your question is Top 5% Users by Engagement. 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 needs a quarterly engagement report identifying its most active users. For this exercise, treat the last quarter as 2025-04-01 through 2025-06-30, inclusive. Write a PostgreSQL query that calculates engagement scores and returns the top 5% of users.

Requirements

  1. Include every user, including users with no qualifying activity.
  2. Calculate each user's score from events during the quarter using these weights: login = 1, view = 2, like = 5, comment = 8, share = 10, and publish = 15. Treat unknown or NULL event types as zero points.
  3. Rank users by score descending, breaking ties with user_id ascending, and return CEIL(5% of all users) rows.
  4. Return the user ID, email, engagement score, and rank.

Schema

users
ColumnTypeDescription
user_idPKINTUnique user identifier
emailVARCHAR(255)User email address
engagement_events
ColumnTypeDescription
event_idPKINTUnique engagement event identifier
user_idINTReferences users.user_id
event_typeVARCHAR(30)Type of engagement event
occurred_atDATEDate when the event occurred
Tablesusersengagement_events
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results