Dataford
Interview QuestionsInterview GuidesExperiencesCoaching
New
Pricing
Get started
Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
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
Experiences
Practice
Mock InterviewsTimed interview simulations with feedbackCoaching1:1 coaching with people at your target companiesSuccess 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
Build my plan
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
Sign up free to run your codeLog inLn 1
Run your query to see results here.