Dataford
Interview QuestionsInterview GuidesExperiencesCoaching
New
Pricing
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 feedbackCoaching1:1 coaching with people at your target companiesSuccess 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 Joins, CTEs, and Intervals
00:00
5 left

SQL Joins, CTEs, and Intervals

HardSQL · PostgreSQL

Problem

Sberbank wants to identify continuous customer activity periods in Sberbank Online. Activity records can be duplicated during ingestion, and separate events belong to the same interval when the next event starts no later than one day after the running end of the current interval.

Write a PostgreSQL query that joins all three tables and returns only intervals lasting at least two calendar days and containing at least two distinct Sberbank Online activity types.

Requirements

  1. Deduplicate events by customer, activity type, start date, and end date, keeping the row with the latest ingested_at.
  2. Join customers, activity events, and activity types, retaining only channel = 'Sberbank Online'.
  3. Use CTEs and window functions to merge overlapping or adjacent intervals. The chaining logic must compare each event with the running maximum end date, not only the immediately preceding event.
  4. Return the customer, merged interval boundaries, inclusive active days, distinct activity type count, and deduplicated event count.
  5. Keep intervals meeting both thresholds and order the result by customer and interval start.

Schema

customers
ColumnTypeDescription
customer_idPKINTUnique Sberbank customer identifier
customer_nameVARCHAR(100)Customer display name
activity_events
ColumnTypeDescription
event_idPKINTUnique ingested event record
customer_idINTReferences customers.customer_id
activity_type_idINTReferences activity_types.activity_type_id
start_dateDATEActivity interval start date
end_dateDATEActivity interval end date
ingested_atTIMESTAMPTimestamp when the record was ingested
activity_types
ColumnTypeDescription
activity_type_idPKINTUnique activity type identifier
activity_nameVARCHAR(80)Readable activity name
channelVARCHAR(40)Sberbank product or channel where the activity occurred
Tablescustomersactivity_eventsactivity_types
Interviewer

Your question is SQL Joins, CTEs, and Intervals. 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.