Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Consecutive-Month Customer Orders

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

Your question is Consecutive-Month Customer Orders. Start with the requirements and the four 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

Business Context

Eaton's business analysts need to identify customers showing sustained purchasing activity across consecutive calendar months. The analysis should use completed orders for Eaton electrical products and ignore duplicate orders within the same month.

Task

Write a PostgreSQL query that identifies customers who placed qualifying orders in at least one pair of consecutive calendar months.

Requirements

  1. Consider only orders with order_status = 'Completed', non-null order dates, and products in the Electrical business unit.
  2. Treat multiple qualifying orders by the same customer in one month as one active month.
  3. Use a window function to detect consecutive calendar months, including December to January transitions.
  4. Return each qualifying customer once with the number of consecutive month pairs, the first pair, and the last pair.
  5. Sort the output by customer_id ascending.

Representative Data

customers includes customers without qualifying orders, while orders includes canceled, undated, and non-qualifying examples. Multiple rows can belong to the same customer or order.

Schema

customers
ColumnTypeDescription
customer_idPKINTUnique customer identifier
customer_nameVARCHAR(100)Customer business name
orders
ColumnTypeDescription
order_idPKINTUnique order identifier
customer_idINTCustomer who placed the order
order_dateDATEDate the order was placed
order_statusVARCHAR(20)Order status
order_items
ColumnTypeDescription
order_idINTOrder containing the item
product_idINTProduct included in the order
quantityINTQuantity ordered
products
ColumnTypeDescription
product_idPKINTUnique product identifier
product_nameVARCHAR(100)Eaton product name
business_unitVARCHAR(50)Eaton product business unit
Tablescustomersordersorder_itemsproducts
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results