Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
SQL Validation With Business Rules
00:00
5 left

SQL Validation With Business Rules

MediumSQL · PostgreSQL

Problem

Given a specific data table, calculate the expected output based on a set of conditional business rules.

Write a PostgreSQL query using data_records and conditional_rules. Apply the listed amount ranges and return the calculated decision and adjusted amount for every record, including records with no matching rule.

Output

  1. One row per record, with record_id, category, amount, decision, and adjusted_amount.
  2. Null amounts are REVIEW; negative amounts are INVALID with an adjusted amount of zero; unmatched non-null values are UNCLASSIFIED and retain their amount.
  3. Order by record_id ascending.

Schema

data_records
ColumnTypeDescription
record_idPKINTUnique identifier for the source record
categoryVARCHAR(20)Category used to select a conditional rule
amountDECIMAL(10,2)Value evaluated against the rule range
conditional_rules
ColumnTypeDescription
rule_idPKINTUnique identifier for the conditional rule
categoryVARCHAR(20)Category to which the rule applies
min_amountDECIMAL(10,2)Inclusive lower boundary for the rule
max_amountDECIMAL(10,2)Exclusive upper boundary for the rule
outcomeVARCHAR(20)Decision returned when the rule matches
multiplierDECIMAL(5,2)Factor applied to the source amount
Tablesdata_recordsconditional_rules
Interviewer

Your question is SQL Validation With Business Rules. Start with the requirements and the two 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
You need to log in / sign up to run or submit.Ln 1
Run your query to see results here.