Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Top Users SQL Query

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

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

Write an SQL query to find the top 3 users based on their interactions on a given day.

Use 2025-02-14 as the given day. Count only countable interaction types and interactions that are not deleted. Rows with a NULL interaction timestamp are ignored.

Output

  1. Return up to three rows, one per user.
  2. Include user_id, username, interaction_count, and rank.
  3. Order by rank ascending. Break equal interaction counts by user_id ascending.

Schema

users
ColumnTypeDescription
user_idPKINTEGERUnique user identifier
usernameVARCHAR(100)User display name
interaction_types
ColumnTypeDescription
interaction_type_idPKINTEGERUnique interaction type identifier
interaction_type_nameVARCHAR(80)Name of the interaction type
is_countableBOOLEANWhether this interaction type contributes to user counts
interactions
ColumnTypeDescription
interaction_idPKINTEGERUnique interaction identifier
user_idINTEGERUser associated with the interaction
interaction_type_idINTEGERType associated with the interaction
occurred_atTIMESTAMPTimestamp when the interaction occurred
is_deletedBOOLEANWhether the interaction has been deleted
Tablesusersinteractionsinteraction_types
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results