Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

SQL: Consecutive Rejections Detection

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

Your question is SQL: Consecutive Rejections Detection. Start with the requirements and the two tables 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

Instacart monitors shopper order decisions to identify repeated rejections that may indicate delivery friction or account issues. Write a PostgreSQL query that identifies every rejection immediately following another rejection by the same shopper.

Requirements

  1. Join order decision events to shopper account data and retain events even when a shopper profile is unavailable.
  2. Use ROW_NUMBER() to assign each shopper's events a chronological sequence, breaking timestamp ties with event_id.
  3. Use LAG() to retrieve the previous decision for each shopper.
  4. Return only rejected events whose immediately preceding decision by that shopper was also rejected. Include the shopper, event details, sequence number, and previous decision.

Schema

shopper_accounts
ColumnTypeDescription
shopper_idPKINTUnique shopper identifier
shopper_nameVARCHAR(100)Shopper display name
marketVARCHAR(50)Instacart market associated with the shopper
shopper_order_events
ColumnTypeDescription
event_idPKINTUnique order decision event identifier
shopper_idINTShopper associated with the event
order_idINTInstacart order identifier
event_timeTIMESTAMPTimestamp when the decision was recorded
decisionVARCHAR(20)Shopper decision, accepted, rejected, or not recorded
Tablesshopper_accountsshopper_order_events
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results