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
Clean Duplicate Orders for Reporting
00:00
5 left

Clean Duplicate Orders for Reporting

MediumSQL · PostgreSQL

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
Interviewer

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