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
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
Get started
SQL Query Over Multi-Join Schema
00:00
5 left

SQL Query Over Multi-Join Schema

MediumSQL · PostgreSQL

Problem

Write a SQL query to extract specific metrics from a complex, multi-join schema.

Use the provided schema and sample data. Calculate metrics for each customer from completed orders placed during 2025, while retaining customers with no qualifying orders.

Output

  1. One row per customer with customer_id, customer_name, completed_order_count, total_units, and total_revenue.
  2. Treat missing qualifying orders or line items as zero metrics.
  3. Order by total_revenue descending, then completed_order_count descending, then customer_id ascending.

Output details

  • Interpret during 2025 as order dates from 2025-01-01 inclusive through 2026-01-01 exclusive.
  • Format total_revenue as numeric values with exactly two decimal places and total_units and completed_order_count as integers.
  • Calculate total_units by summing line-item quantity and total_revenue by summing quantity * unit_price per qualifying order.
  • Count every qualifying completed order, including orders with no line items, while assigning such orders zero units and revenue.

Schema

customers
ColumnTypeDescription
customer_idPKINTUnique customer identifier
customer_nameVARCHAR(100)Customer display name
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 line item identifier
order_idINTOrder containing the line item
quantityINTNumber of units in the line item
unit_priceNUMERIC(10,2)Price per unit
Tablescustomersordersorder_items
Interviewer

Your question is SQL Query Over Multi-Join Schema. Start with the requirements and the three 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.