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 for User Engagement Metrics
00:00
5 left

SQL for User Engagement Metrics

MediumSQL · PostgreSQL

Problem

Write a SQL query to extract specific user engagement metrics across multiple joined tables.

Use the provided user, session, and engagement event data. Return metrics for users whose marketing_opt_in is true, including users with no sessions or events.

Output

  1. One row per opted-in user with user_id, email, total_sessions, engaged_sessions, engagement_event_count, and last_engaged_at.
  2. Count each session once, count sessions containing at least one event as engaged, and exclude events not associated with a returned user's session.
  3. Order by user_id ascending.

Schema

users
ColumnTypeDescription
user_idPKINTUnique user identifier
emailVARCHAR(255)User email address
marketing_opt_inBOOLEANWhether the user has opted into marketing
sessions
ColumnTypeDescription
session_idPKINTUnique session identifier
user_idINTUser who started the session
started_atTIMESTAMPTZSession start timestamp
platformVARCHAR(30)Platform used for the session
engagement_events
ColumnTypeDescription
event_idPKINTUnique engagement event identifier
session_idINTSession associated with the event
event_typeVARCHAR(50)Type of engagement event
occurred_atTIMESTAMPTZEvent occurrence timestamp
Tablesuserssessionsengagement_events
Interviewer

Your question is SQL for User Engagement Metrics. 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.