SQL-Engineering-Handbook

05 · Finance — Window Functions in Budgeting & Profitability Analysis

Year-to-date profit meets budget variance

Introduction

If Sales Analytics (Chapter 02) taught you to track revenue over time, Finance closes the loop by tracking profit, expense, and budget variance over the same time dimension - but with a critical difference: finance teams almost always need to compare an actual number to a planned number, not just to a prior period. This chapter combines every pattern built so far - running totals, period-over-period comparison, and leaderboard ranking - into the reporting structure finance and FP&A (Financial Planning & Analysis) teams present to leadership every month.


Business Background

A typical finance reporting schema centers on a ledger of actuals, often paired with a budget table:

FP&A teams consume this data primarily through monthly rollups, running (year-to-date) totals, and variance against budget.


Typical KPIs


Typical Dashboards


Business Problems

  1. “Rank our departments by profit contribution this quarter.”
  2. “Rank departments by expense - which teams are the largest cost centers?”
  3. “Show running (year-to-date) profit, department by department, and company-wide.”
  4. “How does actual spend compare to budget, month by month, for every department?”
  5. “Which departments had the largest swing in expense from one month to the next?”
  6. “Build a financial leaderboard that’s reusable for any month or quarter.”

Why Window Functions Are Needed

Every pattern in this chapter has already been introduced - RANK()/DENSE_RANK() for leaderboards (Chapters 01-03), running totals for year-to-date profit (Chapter 02’s running revenue, Chapter 04’s running balance), and LAG() for period-over-period comparison (Chapter 02’s MoM/YoY growth). Finance simply layers a budget comparison on top - typically via a join to a budgets table rather than a new window function - which is why this chapter also serves as a review of everything covered so far, applied to the domain where leadership scrutiny is highest.


Functions Used in This Chapter

Function Business Explanation
RANK() / DENSE_RANK() Department leaderboards by profit and by expense.
SUM() OVER (... ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) Running (year-to-date) profit, per department and company-wide.
LAG() Month-over-month expense comparison and volatility flagging.
Window function + join to budgets Actual-vs-budget variance reporting.

SQL Concepts Reinforced


Performance Notes


Common Mistakes


Interview Questions

  1. “How would you calculate year-to-date profit per department, ensuring it resets correctly at each fiscal year boundary?” — Expect SUM(profit) OVER (PARTITION BY department_id, fiscal_year ORDER BY transaction_month ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW).
  2. “How would you compute budget variance using SQL, and what does a positive vs. negative variance mean for expenses versus revenue?” — Expect a join between actuals and budgets at a common grain, actual - budget, and a clear explanation that a positive expense variance is unfavorable while a positive revenue variance is favorable.
  3. “How would you flag departments with unusually volatile month-over-month expenses?” — Expect LAG(expense) OVER (PARTITION BY department_id ORDER BY month) followed by a percentage-change calculation compared against a materiality threshold.
  4. “Why might RANK() produce a misleading department leaderboard for an executive presentation?” — Expect a discussion of tie-skipping behavior and when DENSE_RANK() or ROW_NUMBER() (with a documented tiebreaker) is more appropriate.
  5. “How is this chapter’s running-profit pattern the same as, or different from, the running revenue pattern in Sales Analytics?” — Expect recognition that the SQL pattern (a framed SUM() OVER (...)) is identical; only the underlying business metric (profit vs. revenue) and partition grain (department vs. salesperson) differ.

Summary

Finance is the capstone domain of this module because nearly every pattern introduced in Chapters 01-04 reappears here in service of the metric leadership scrutinizes most closely: profit. Running totals, leaderboards, and period-over-period comparisons combine with budget joins to produce the actual-vs-budget variance reports that drive monthly business reviews - and by this point, you should recognize the underlying SQL pattern before you even finish reading the business question.


Further Practice


Next: 05_FINANCE.sql — the fully engineered SQL chapter for this domain, and the final chapter of Module 08.


Previous: 04_BANKING.md · Module: README · Next module: 09_Date_Functions