Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Trending in AI
Hot
Agentic AI EngineerForward-Deployed EngineerGenAI EngineerAI Architect
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
ExperiencesCoaching
New
Practice
Mock InterviewsTimed interview simulations with feedbackSuccess 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
Start free
Calculate Adjusted Amounts and Flag Mismatches
00:00
5 left

Calculate Adjusted Amounts and Flag Mismatches

MediumSQL · PostgreSQL

Problem

How would you approach a tricky SQL problem to correctly transform and validate the results?

Use the provided transaction, adjustment, account, and expected-result tables. Produce one row for each latest settled transaction, calculate its adjusted amount, and identify whether the result is valid, mismatched, incomplete, or associated with an unknown account.

Output

  1. Return transaction_id, account_name, net_amount, expected_amount, variance, and validation_status.
  2. Include only the latest settled record for each transaction and exclude canceled transactions.
  3. Sort by transaction_id ascending.

Output details

  • Choose the latest record by highest version_number, then latest loaded_at, then highest event_id, before requiring status settled.
  • Treat missing adjustments as zero, and preserve a NULL gross_amount as NULL net_amount.
  • Set variance to NULL when either net_amount or expected_amount is NULL; otherwise calculate net_amount - expected_amount without rounding.
  • Use statuses INVALID_SOURCE_AMOUNT, MISSING_EXPECTED_RESULT, UNKNOWN_ACCOUNT, VALID, and MISMATCH in that precedence order: source amount, expected result, account, equality, otherwise mismatch.

Schema

transaction_events
ColumnTypeDescription
event_idPKINTUnique source event identifier
transaction_idVARCHAR(20)Business transaction identifier
account_idVARCHAR(20)Account associated with the transaction
version_numberINTSource version for the transaction
statusVARCHAR(20)Transaction processing status
gross_amountNUMERIC(12,2)Original transaction amount
loaded_atTIMESTAMPTime the source event was loaded
transaction_adjustments
ColumnTypeDescription
adjustment_idPKINTUnique adjustment identifier
transaction_idVARCHAR(20)Transaction receiving the adjustment
adjustment_typeVARCHAR(20)Fee, refund, credit, or other adjustment type
adjustment_amountNUMERIC(12,2)Signed adjustment amount
accounts
ColumnTypeDescription
account_idPKVARCHAR(20)Unique account identifier
account_nameVARCHAR(100)Account display name
expected_transaction_results
ColumnTypeDescription
transaction_idPKVARCHAR(20)Transaction being validated
expected_amountNUMERIC(12,2)Reference amount expected after adjustments
Tablestransaction_eventstransaction_adjustmentsaccountsexpected_transaction_results
Interviewer

Whenever you're ready, walk me through your approach or go straight to the code. The requirements and the four tables are in the Question tab.

Sign up free to run or submit. Log in
CodePostgreSQL
Sign up free to run your code. Your draft is saved.Run with a free accountLog inLn 1
Run your query to see results here.