KatInterview

Auditing a Credit-Card Ledger: Validation Rules and Failing-Row Queries

SQL & DataModel answer
MediumModel answerSQL & Datadata-qualitysqlvalidation

Last updated

Design a layered set of data quality checks over transactions and vendors tables, with SQL for the failing rows.

You are responsible for the health of a credit-card ledger stored in two relational tables.

transactions

columntypedescription
transaction_idintegerUnique ID for a transaction.
user_idintegerUnique ID for a customer.
vendor_idintegerUnique ID for a vendor.
transaction_timetimestampTime when the transaction was recorded.
transaction_dollarsnumericDollar amount of the transaction.
transaction_typetextTransaction type, such as PURCHASE or REFUND.
refund_transaction_idintegerFor a REFUND, the transaction_id of the original PURCHASE.

vendors

columntypedescription
vendor_idintegerUnique ID for a vendor.
citytextCity where the vendor is located.
state_provincetextState or province where the vendor is located.
countrytextTwo-letter country code.

Propose at least five data quality checks or validations for these tables. Consider integrity constraints, referential integrity, refund logic, invalid values, missing data, and anomaly detection. For at least two checks, provide SQL that returns the failing rows or failing keys.

Hints

Hint 1

Write each check as a query that returns the bad rows, so an empty result is a pass — and separate hard rules (fail the pipeline) from statistical rules (warn only).

Hint 2

Refund checks are multi-part: the link must be present, point to a real PURCHASE, share the same user and vendor, come after the purchase, and never exceed the purchase amount in total.

Hint 3

For anomaly detection, compare today's volume against a trailing window using a window function; state explicitly any sign or cents-rounding assumptions.