Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

SQL Consecutive Anomalous Days

MediumSQL · PostgreSQL00:00
Practice interviewer
In session
5 left
00:00

Your question is SQL Consecutive Anomalous Days. Start with the requirements and the one table on the right.

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.

Problem

Write a SQL query using SQL window functions to identify consecutive days of anomalous email sending behavior for a given user.

Use the daily email activity records for user_id = 101. Treat rows where is_anomalous is true as anomalous, and return only streaks containing at least two consecutive calendar days.

Output

  1. One row per anomalous streak
  2. Columns: user_id, streak_start, streak_end, and anomalous_days
  3. Order by streak_start ascending

Schema

email_sending_daily
ColumnTypeDescription
record_idPKINTUnique daily activity record identifier
user_idINTIdentifier of the email-sending user
activity_dateDATECalendar date of the user's email activity
sent_countINTNumber of emails sent on the date
is_anomalousBOOLEANWhether the daily sending behavior was classified as anomalous
Tablesemail_sending_daily
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results