Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
Experiences
Practice
Mock InterviewsTimed interview simulations with feedbackSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Build my plan
SQL: First and Last Event Per User
00:00
5 left

SQL: First and Last Event Per User

MediumSQL · PostgreSQL

Problem

Business Context

NewWave Telecom & Technologies uses customer activity events to analyze engagement across its digital services. The Data Analyst team needs a per-user lifecycle summary for the NewWave customer activity dashboard.

Task

Write a PostgreSQL query that returns the first and last valid event for every user in users, including users who have no valid events.

Requirements

  1. Exclude events whose event_at is NULL.
  2. Rank events independently for each user by timestamp, using event_id as the deterministic tie-breaker.
  3. Return the first and last event timestamps and event names.
  4. Return elapsed minutes between the first and last event, or NULL when no valid event exists.
  5. Preserve users without events, and label unmatched event types as Unknown.
  6. Sort by user_id.

Schema

users
ColumnTypeDescription
user_idPKINTUnique user identifier
full_nameVARCHAR(100)User's full name
account_statusVARCHAR(20)Current account status
created_atTIMESTAMPAccount creation timestamp
user_events
ColumnTypeDescription
event_idPKINTUnique event identifier
user_idINTUser associated with the event
event_type_idINTReferenced event type
event_atTIMESTAMPTimestamp when the event occurred
event_types
ColumnTypeDescription
event_type_idPKINTUnique event type identifier
event_nameVARCHAR(50)Readable event type name
Tablesusersuser_eventsevent_types
Interviewer

Your question is SQL: First and Last Event Per User. Start with the requirements and the three tables in the Question tab.

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.
CodePostgreSQL
Sign up free to run your codeLog inLn 1
Run your query to see results here.