Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

First and Last User Events

MediumSQL · PostgreSQL00:00
I
Practice interviewer
Your interviewer
In session
I
Interviewer

Welcome to the SQL screen.

The question is on your right: First and Last User Events. Read through the requirements and the three tables first.

Run and submit your code as often as you need. You also have five interviewer messages this session - want to talk through your approach, or are you ready to start coding?

You need to log in / sign up to run or submit.

Problem

StreamWave tracks user activity across web and mobile sessions. Write a PostgreSQL query to return each active user's first and last event based on event timestamp.

Requirements

  1. Include only users whose status is 'active'.
  2. Return one row per active user, even if the user has no events.
  3. For each user, show the first event type and timestamp, and the last event type and timestamp.
  4. If multiple events share the same timestamp for a user, break ties using the smaller event_id for the first event and the larger event_id for the last event.
  5. Order the final result by user_id.

Schema

users
ColumnTypeDescription
user_idPKINTPrimary key for the user
full_nameVARCHAR(100)User full name
statusVARCHAR(20)Account status such as active, inactive, or banned
signup_dateDATEDate the user signed up
platforms
ColumnTypeDescription
platform_idPKINTPrimary key for the platform
platform_nameVARCHAR(50)Platform name such as web or ios
events
ColumnTypeDescription
event_idPKINTPrimary key for the event
user_idINTUser who generated the event
platform_idINTPlatform where the event occurred
event_typeVARCHAR(50)Type of event
event_timeTIMESTAMPTimestamp when the event occurred
Tablesusersplatformsevents
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results