Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Clean Duplicate Transactions and Null Costs

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

Your question is Clean Duplicate Transactions and Null Costs. Start with the requirements and the one table 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

You are given transaction-level cost data that may contain duplicate records and missing cost values. Write a PostgreSQL query that returns one reporting row per customer and month, using the latest version of each transaction, removing duplicate transaction records, and replacing null cost values with the customer’s average non-null cost for that month. If a customer-month has no non-null cost at all, use 0.

Schema

transactions
ColumnTypeDescription
transaction_idVARCHAR(20)Business transaction identifier
customer_nameVARCHAR(100)Customer name
transaction_dateDATEDate the transaction occurred
costDECIMAL(10,2)Transaction cost, may be missing
updated_atTIMESTAMPTimestamp of the latest record version
Tablestransactions
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results