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
ExperiencesCoaching
New
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
Get started
User Action Span on Facebook
00:00
5 left

User Action Span on Facebook

EasySQL · PostgreSQL

Problem

On Facebook, analysts often need to measure how long each user has been active within an event log. Write a SQL query to find the time between each user's first and last recorded action.

Requirements

  1. Return one row per user_id.
  2. Find each user's earliest and latest action_time.
  3. Calculate the elapsed time between those two timestamps as time_between_actions.
  4. Include users with only one action; their elapsed time should be INTERVAL '0 seconds'.
  5. Order the result by user_id ascending.

Schema

facebook_actions
ColumnTypeDescription
action_idPKINTUnique action event ID
user_idINTFacebook user identifier
action_typeVARCHAR(50)Type of action performed
action_timeTIMESTAMPTimestamp of the action
deviceVARCHAR(20)Device used for the action
Tablesfacebook_actions
Interviewer

Whenever you're ready, walk me through your approach or go straight to the code. The requirements and the one table are in the Question tab.

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.