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
Segment Active Customers by Completed Order Spend
00:00
5 left

Segment Active Customers by Completed Order Spend

MediumSQL · PostgreSQL

Problem

Describe a complex SQL query you developed to optimize data retrieval.

Use the provided customer, order, and order-item tables to produce a customer-level retrieval result that includes active customers and completed-order metrics.

Output

  1. One row per active customer, including customers without completed orders.
  2. Return customer_id, customer_name, completed_orders, total_spend, latest_order_date, and customer_segment.
  3. Sort by total_spend descending, then customer_id ascending.

Output details

  • Treat customers with is_active = TRUE as active.
  • Count only orders whose status is completed, counting each completed order once per customer.
  • Calculate each order's spend as the sum of quantity * unit_price across its items.
  • Return total_spend with exactly two decimal places, using 0.00 when the calculated total is null or absent.
  • Return latest_order_date as NULL when a customer has no completed orders.
  • Classify customers with no completed orders as No orders; otherwise use High for spend at least 500, and Standard below 500.

Schema

customers
ColumnTypeDescription
customer_idPKINTUnique customer identifier
customer_nameVARCHAR(100)Customer display name
is_activeBOOLEANWhether the customer is active
orders
ColumnTypeDescription
order_idPKINTUnique order identifier
customer_idINTCustomer who placed the order
order_dateDATEDate the order was placed
statusVARCHAR(20)Order lifecycle status
order_items
ColumnTypeDescription
order_item_idPKINTUnique order-item identifier
order_idINTOrder containing the item
quantityINTNumber of units ordered
unit_priceNUMERIC(10, 2)Price per unit
Tablescustomersordersorder_items
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.