Department Budget Variance
Beginner Mode

Scenario

You work as a data analyst for a government agency that tracks annual budget allocations and actual spending across departments.

Task

Write a Snowflake SQL query that:

  1. Joins {{ ref("budgets") }} with {{ ref("spending") }} on both department and fiscal_year
  2. Calculates budget_variance as CAST(VARIANCE(amount_budgeted) AS INTEGER) grouped by department
  3. Calculates spending_variance as CAST(VARIANCE(amount_spent) AS INTEGER) grouped by department
  4. Returns department, budget_variance, and spending_variance

Schema

budgets

Column Type Description
department String Name of the government department
fiscal_year Integer Fiscal year of the allocation
amount_budgeted Float Total budget allocated (in millions)

spending

Column Type Description
department String Name of the government department
fiscal_year Integer Fiscal year of the expenditure
amount_spent Float Total amount spent (in millions)

Example

budgets:

department fiscal_year amount_budgeted
Engineering 2022 400.0
Engineering 2023 460.0
Marketing 2022 300.0
Marketing 2023 350.0

spending:

department fiscal_year amount_spent
Engineering 2022 380.0
Engineering 2023 440.0
Marketing 2022 280.0
Marketing 2023 320.0

Expected Output:

department budget_variance spending_variance
Engineering 1800 1800
Marketing 1250 800

Note: Variance here uses sample variance (divides by N-1, not N). The result is a whole number with no decimals. Results are ordered alphabetically by department.

Quick Solution

Code Environment

Sign in or try as guest to run your code.

Sign In

Track

Question Difficulty Company Access
Need more practice in this area? Explore more questions →