Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Top N Users Per Day

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

Your question is Top N Users Per Day. 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

Databricks administrators want a daily view of the users generating the most activity in Databricks SQL. Write a PostgreSQL query that counts valid activity events per user and returns the top three activity ranks for each calendar day.

Requirements

  1. Join activity_logs to users so the output includes each user's name.
  2. Count only events with a non-null activity_type, grouped by user and activity date.
  3. Use a ranking window function so ties at the third rank are included.
  4. Return activity_date, user_id, user_name, and activity_count, ordered by date, rank, and user ID.

Schema

users
ColumnTypeDescription
user_idPKINTEGERUnique Databricks user identifier
user_nameVARCHAR(100)User display name
emailVARCHAR(255)User email address
activity_logs
ColumnTypeDescription
activity_idPKINTEGERUnique activity event identifier
user_idINTEGERUser associated with the activity event
activity_atTIMESTAMPTimestamp when the activity occurred
activity_typeVARCHAR(50)Type of activity, nullable when event metadata is incomplete
Tablesusersactivity_logs
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results