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
Consecutive-Month Customer Orders
00:00
5 left

Consecutive-Month Customer Orders

MediumSQL · PostgreSQL

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
Interviewer

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