Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Trending in AI
Hot
Agentic AI EngineerForward-Deployed EngineerGenAI EngineerAI Architect
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
Start free
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.

Output details

  • Return columns in this order: customer_id, customer_name, interval_start, interval_end, active_days, distinct_activity_types, event_count.
  • When ingested_at ties during deduplication, retain the row with the greatest event_id.
  • Order by customer_id ascending, then interval_start ascending.

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

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

Sign up free to run or submit. Log in
CodePostgreSQL
Sign up free to run your code. Your draft is saved.Run with a free accountLog inLn 1
Run your query to see results here.