Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Rolling 30-Day Active Users SQL

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

Your question is Rolling 30-Day Active Users SQL. 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

Publicis Groupe analysts are evaluating regional engagement across a digital audience platform. Write a PostgreSQL query that measures distinct active users during the trailing 30-day window ending on 2024-02-01, assigns each region an engagement tier, and ranks regions within their tier.

Requirements

  1. Join users to user_activity while retaining regions whose users had no qualifying activity.
  2. Count distinct users with activity from 2024-01-03 through 2024-02-01, inclusive.
  3. Assign tiers using High for at least 3 active users, Medium for 1 or 2, and Low for 0.
  4. Use a window function to rank regions within each tier by active-user count descending, with region name as a deterministic tie-breaker in the final output.
  5. Exclude users whose region is NULL.

Schema

users
ColumnTypeDescription
user_idPKINTUnique user identifier
regionVARCHAR(50)Geographic region assigned to the user
user_activity
ColumnTypeDescription
activity_idPKINTUnique activity event identifier
user_idINTUser associated with the activity event
activity_dateDATECalendar date on which the activity occurred
Tablesusersuser_activity
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results