Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Clean Duplicate Orders for Reporting

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

Your question is Clean Duplicate Orders for Reporting. 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

BrightCart needs a client-facing order report, but the source data contains duplicate order records and some rows have missing product_id values. Write a PostgreSQL query to produce a cleaned report for January 2024.

Requirements

  1. Deduplicate orders_raw by keeping only the latest row per order_id based on updated_at.
  2. Join the cleaned orders to products using product_id.
  3. If product_id is missing or does not match a product, label the product as 'Unknown Product' and the category as 'Unknown'.
  4. Return one row per cleaned order with: order_id, client_name, order_date, product_name, category, quantity, and line_amount (quantity * unit_price).
  5. Include only orders where order_date is between 2024-01-01 and 2024-01-31, ordered by order_date, then order_id.

Schema

clients
ColumnTypeDescription
client_idPKINTPrimary key for the client
client_nameVARCHAR(100)Client company name
products
ColumnTypeDescription
product_idPKINTPrimary key for the product
product_nameVARCHAR(100)Display name of the product
categoryVARCHAR(50)Product category
unit_priceDECIMAL(10,2)Unit price used to calculate line amount
orders_raw
ColumnTypeDescription
row_idPKINTPrimary key for the raw order row
order_idINTBusiness order identifier that may appear multiple times
client_idINTReferences the client placing the order
product_idINTReferences the ordered product; may be NULL or invalid
order_dateDATEDate the order was placed
quantityINTNumber of units ordered
updated_atTIMESTAMPTimestamp of the latest raw row update
Tablesclientsproductsorders_raw
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results