Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Find Second Most Recent Subscription

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

Your question is Find Second Most Recent Subscription. Start with the requirements and the one table 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

Adobe wants to review the second most recent subscription event for each account in Adobe Experience Cloud. Write a SQL query to return that event per user.

Requirements

  1. Use a window function to rank subscription events per user_id by event_timestamp in descending order.
  2. Return exactly one row per user for the second most recent event.
  3. Include users with tied timestamps correctly, using a deterministic tie-breaker if needed.
  4. Exclude users who do not have at least two subscription events.

Schema

subscription_events
ColumnTypeDescription
event_idPKBIGINTPrimary key for the event
user_idBIGINTAdobe account identifier
subscription_idBIGINTSubscription record identifier
event_typeVARCHAR(50)Event type, such as start, renewal, or cancel
event_timestampTIMESTAMPWhen the subscription event occurred
channelVARCHAR(50)Source channel, such as web, mobile, or partner
Tablessubscription_events
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results