Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
SQL for Expense Variance
00:00
5 left

SQL for Expense Variance

MediumSQL · PostgreSQL

Problem

Write a SQL query to analyze monthly property operating expenses by building, compare them to budget, and flag properties with more than 10% variance.

Use the properties, operating_expenses, and expense_budgets tables. Include months represented by either actual expenses or budgets, and treat a budget of zero or less as having no positive budget for percentage comparison.

Output

  1. One row per month and building, ordered by month ascending and building ID ascending.
  2. Columns: month, property_id, building_id, building_name, actual_expenses, budget_expenses, variance_amount, variance_percent, and variance_flag.
  3. Include unmatched actual or budget records. variance_percent is NULL when the budget is not positive; use OVER_10_PERCENT only when the absolute variance exceeds 10%.

Schema

properties
ColumnTypeDescription
building_idPKINTEGERUnique building identifier
property_idVARCHAR(20)Property reference code
building_nameVARCHAR(100)Building display name
operating_expenses
ColumnTypeDescription
expense_idPKINTEGERUnique operating expense identifier
building_idINTEGERBuilding associated with the expense
expense_dateDATEDate the expense was recorded
amountNUMERIC(14,2)Operating expense amount
expense_budgets
ColumnTypeDescription
budget_idPKINTEGERUnique budget record identifier
building_idINTEGERBuilding associated with the budget
budget_monthDATEMonth represented by the budget
budget_amountNUMERIC(14,2)Budgeted operating expense amount
Tablespropertiesoperating_expensesexpense_budgets
Interviewer

Your question is SQL for Expense Variance. 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
You need to log in / sign up to run or submit.Ln 1
Run your query to see results here.