Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Reporting Query With Joins and Aggregates

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

Your question is Reporting Query With Joins and Aggregates. Start with the requirements and the three 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

Caterpillar's parts distribution team needs a quarterly report for Midwest dealers. Write a PostgreSQL query that summarizes completed parts orders from January 1 through March 31, 2025, while retaining Midwest dealers with no qualifying activity.

Requirements

  1. Use LEFT JOIN to preserve dealers without completed orders or order items.
  2. Include the number of completed orders, total units, total revenue, and average order value for each Midwest dealer.
  3. Treat missing quantities, items, and revenue as zero, and sort by total revenue descending, then dealer name ascending.

Schema

dealers
ColumnTypeDescription
dealer_idPKINTUnique Caterpillar dealer identifier
dealer_nameVARCHAR(100)Dealer business name
regionVARCHAR(50)Dealer sales region
parts_orders
ColumnTypeDescription
order_idPKINTUnique parts order identifier
dealer_idINTDealer associated with the order
order_dateDATEOrder placement date
statusVARCHAR(20)Current order status
order_items
ColumnTypeDescription
item_idPKINTUnique order line identifier
order_idINTRelated parts order
part_numberVARCHAR(30)Caterpillar part number
quantityINTNumber of units ordered
unit_priceNUMERIC(10,2)Price per unit
Tablesdealersparts_ordersorder_items
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results