Case 5: cash flow forecast in SQL
Profit isn't cash in the bank. You turn a list of expected payments into a monthly forecast to December, work out the bank balance at each month end and find the month where it gets tight, before the bank calls you.
In short: inflows and outflows per month with SUM(CASE WHEN direction = 'inflow' THEN amount ELSE 0 END), net with ELSE -amount. The month-end balance is the opening balance plus SUM(net) OVER (ORDER BY month). Filtering on that balance only works in an outer query.
The situation
Some big payments are coming in the second half: a new assembly line in September, loan repayments and tax payments in September and December, and the year-end bonus in November. At the same time, customers pay less once the cycling season is over. Management wants to know whether the bank account will last until year end. The loan agreement requires a minimum balance of 50,000.
The tables
- cash_plan: one row per expected payment from July to December with expected_date, direction (inflow or outflow), category and amount. All amounts are positive; the direction column says which way the money goes.
- bank_balance: a single row with the balance per bank statement on 30 June 2026.
Here's September in the cash plan:
SELECT expected_date, direction, category, amount
FROM cash_plan
WHERE expected_date LIKE '2026-09%'
ORDER BY expected_date;
| expected_date | direction | category | amount |
|---|---|---|---|
| 2026-09-03 | outflow | Rent | 11400 |
| 2026-09-05 | inflow | Customers | 72600 |
| 2026-09-10 | outflow | Suppliers | 61200 |
| 2026-09-10 | outflow | Taxes | 14200 |
| 2026-09-15 | inflow | Customers | 64900 |
| 2026-09-15 | outflow | Capex | 86500 |
| 2026-09-20 | outflow | Energy | 4300 |
| 2026-09-25 | inflow | Customers | 41300 |
| 2026-09-28 | outflow | Payroll | 109300 |
| 2026-09-30 | outflow | Loan | 18500 |
Ten payments, three of them inflows. Whether the month ends up or down isn't visible like this, so you summarize by month.
Task 1: inflows and outflows per month
Task The cash forecast for July to December is in cash_plan, one row per expected payment. For each month show: month (as YYYY-MM), inflows, outflows and net (inflows minus outflows). Sorted by month.
cash_plan 51 rows, 5 columns
| item_id | expected_date | direction | category | amount |
|---|---|---|---|---|
| 1 | 2026-07-03 | outflow | Rent | 11400 |
| 2 | 2026-07-05 | inflow | Customers | 96400 |
| 3 | 2026-07-10 | outflow | Suppliers | 74300 |
| 4 | 2026-07-15 | inflow | Customers | 88300 |
| 5 | 2026-07-20 | outflow | Energy | 4200 |
| 6 | 2026-07-25 | inflow | Customers | 61250 |
| 7 | 2026-07-28 | outflow | Payroll | 109300 |
| 8 | 2026-08-03 | outflow | Rent | 11400 |
| 9 | 2026-08-05 | inflow | Customers | 84100 |
| 10 | 2026-08-10 | outflow | Suppliers | 69800 |
| 11 | 2026-08-15 | inflow | Customers | 79500 |
| 12 | 2026-08-18 | inflow | Other | 23500 |
| 13 | 2026-08-20 | outflow | Energy | 4100 |
| 14 | 2026-08-25 | inflow | Customers | 52800 |
| 15 | 2026-08-28 | outflow | Payroll | 109300 |
| 16 | 2026-09-03 | outflow | Rent | 11400 |
| 17 | 2026-09-05 | inflow | Customers | 72600 |
| 18 | 2026-09-10 | outflow | Suppliers | 61200 |
| 19 | 2026-09-10 | outflow | Taxes | 14200 |
| 20 | 2026-09-15 | inflow | Customers | 64900 |
| 21 | 2026-09-15 | outflow | Capex | 86500 |
| 22 | 2026-09-20 | outflow | Energy | 4300 |
| 23 | 2026-09-25 | inflow | Customers | 41300 |
| 24 | 2026-09-28 | outflow | Payroll | 109300 |
| 25 | 2026-09-30 | outflow | Loan | 18500 |
| 26 | 2026-10-03 | outflow | Rent | 11400 |
| 27 | 2026-10-05 | inflow | Customers | 68800 |
| 28 | 2026-10-10 | outflow | Suppliers | 52700 |
| 29 | 2026-10-12 | outflow | Marketing | 12800 |
| 30 | 2026-10-15 | inflow | Customers | 59400 |
| 31 | 2026-10-20 | outflow | Energy | 4600 |
| 32 | 2026-10-25 | inflow | Customers | 39900 |
| 33 | 2026-10-28 | outflow | Payroll | 109300 |
| 34 | 2026-11-03 | outflow | Rent | 11400 |
| 35 | 2026-11-05 | inflow | Customers | 69700 |
| 36 | 2026-11-10 | outflow | Suppliers | 47900 |
| 37 | 2026-11-15 | inflow | Customers | 62100 |
| 38 | 2026-11-20 | outflow | Energy | 4900 |
| 39 | 2026-11-25 | inflow | Customers | 50600 |
| 40 | 2026-11-27 | outflow | Payroll | 54600 |
| 41 | 2026-11-28 | outflow | Payroll | 109300 |
| 42 | 2026-12-03 | outflow | Rent | 11400 |
| 43 | 2026-12-05 | inflow | Customers | 88900 |
| 44 | 2026-12-10 | outflow | Suppliers | 43100 |
| 45 | 2026-12-10 | outflow | Taxes | 14200 |
| 46 | 2026-12-12 | outflow | Marketing | 7400 |
| 47 | 2026-12-15 | inflow | Customers | 77300 |
| 48 | 2026-12-20 | outflow | Energy | 5100 |
| 49 | 2026-12-25 | inflow | Customers | 64800 |
| 50 | 2026-12-28 | outflow | Payroll | 109300 |
| 51 | 2026-12-30 | outflow | Loan | 18500 |
Cut the month out of the date with substr(expected_date, 1, 7). Since every amount is positive, CASE decides the direction: the inflows column only counts inflows and every other row adds 0. For net, CASE flips the sign of outflows, so a single sum does the job.
Task 2: the running bank balance
The balance at the end of July is the 30 June balance plus July's net. At the end of August, August's net is added, and so on. That's a running total, which a window function with ORDER BY gives you. The running total lesson has the details.
SUM(net) OVER (ORDER BY month)
Put the monthly net amounts from task 1 in a CTE; the outer query adds the running total and the opening balance. You fetch the opening balance with a small subquery: (SELECT amount FROM bank_balance).
Task Now the actual forecast: for each month show month, net and closing_balance, the expected bank balance at month end. It starts from the balance on 30 June 2026 in the table bank_balance and grows by the net amounts of all months up to and including this one. Sorted by month.
cash_plan 51 rows, 5 columns
| item_id | expected_date | direction | category | amount |
|---|---|---|---|---|
| 1 | 2026-07-03 | outflow | Rent | 11400 |
| 2 | 2026-07-05 | inflow | Customers | 96400 |
| 3 | 2026-07-10 | outflow | Suppliers | 74300 |
| 4 | 2026-07-15 | inflow | Customers | 88300 |
| 5 | 2026-07-20 | outflow | Energy | 4200 |
| 6 | 2026-07-25 | inflow | Customers | 61250 |
| 7 | 2026-07-28 | outflow | Payroll | 109300 |
| 8 | 2026-08-03 | outflow | Rent | 11400 |
| 9 | 2026-08-05 | inflow | Customers | 84100 |
| 10 | 2026-08-10 | outflow | Suppliers | 69800 |
| 11 | 2026-08-15 | inflow | Customers | 79500 |
| 12 | 2026-08-18 | inflow | Other | 23500 |
| 13 | 2026-08-20 | outflow | Energy | 4100 |
| 14 | 2026-08-25 | inflow | Customers | 52800 |
| 15 | 2026-08-28 | outflow | Payroll | 109300 |
| 16 | 2026-09-03 | outflow | Rent | 11400 |
| 17 | 2026-09-05 | inflow | Customers | 72600 |
| 18 | 2026-09-10 | outflow | Suppliers | 61200 |
| 19 | 2026-09-10 | outflow | Taxes | 14200 |
| 20 | 2026-09-15 | inflow | Customers | 64900 |
| 21 | 2026-09-15 | outflow | Capex | 86500 |
| 22 | 2026-09-20 | outflow | Energy | 4300 |
| 23 | 2026-09-25 | inflow | Customers | 41300 |
| 24 | 2026-09-28 | outflow | Payroll | 109300 |
| 25 | 2026-09-30 | outflow | Loan | 18500 |
| 26 | 2026-10-03 | outflow | Rent | 11400 |
| 27 | 2026-10-05 | inflow | Customers | 68800 |
| 28 | 2026-10-10 | outflow | Suppliers | 52700 |
| 29 | 2026-10-12 | outflow | Marketing | 12800 |
| 30 | 2026-10-15 | inflow | Customers | 59400 |
| 31 | 2026-10-20 | outflow | Energy | 4600 |
| 32 | 2026-10-25 | inflow | Customers | 39900 |
| 33 | 2026-10-28 | outflow | Payroll | 109300 |
| 34 | 2026-11-03 | outflow | Rent | 11400 |
| 35 | 2026-11-05 | inflow | Customers | 69700 |
| 36 | 2026-11-10 | outflow | Suppliers | 47900 |
| 37 | 2026-11-15 | inflow | Customers | 62100 |
| 38 | 2026-11-20 | outflow | Energy | 4900 |
| 39 | 2026-11-25 | inflow | Customers | 50600 |
| 40 | 2026-11-27 | outflow | Payroll | 54600 |
| 41 | 2026-11-28 | outflow | Payroll | 109300 |
| 42 | 2026-12-03 | outflow | Rent | 11400 |
| 43 | 2026-12-05 | inflow | Customers | 88900 |
| 44 | 2026-12-10 | outflow | Suppliers | 43100 |
| 45 | 2026-12-10 | outflow | Taxes | 14200 |
| 46 | 2026-12-12 | outflow | Marketing | 7400 |
| 47 | 2026-12-15 | inflow | Customers | 77300 |
| 48 | 2026-12-20 | outflow | Energy | 5100 |
| 49 | 2026-12-25 | inflow | Customers | 64800 |
| 50 | 2026-12-28 | outflow | Payroll | 109300 |
| 51 | 2026-12-30 | outflow | Loan | 18500 |
bank_balance 1 rows, 2 columns
| as_of | amount |
|---|---|
| 2026-06-30 | 144124.59 |
This exercise is part of Premium.
Premium unlocks every exercise in every lesson, unlimited practice with fresh tasks and the final test with certificate.
See Premium Already Premium? Log in
The first exercise in this lesson is free.
Task 3: months below the minimum balance
Now you only want the months where the balance drops below 50,000. WHERE closing_balance < 50000 is the obvious idea, but it won't work in the same query: the database computes window functions only after WHERE has picked the rows. So you need one more level. The first CTE returns the monthly net, the second the balance, and only the outer query filters.
Task The bank requires the account never to drop below 50000. Show the months in which the expected month-end balance (as in task 2) is below this minimum: month, closing_balance and shortfall (50000 minus closing_balance). Sorted by month.
cash_plan 51 rows, 5 columns
| item_id | expected_date | direction | category | amount |
|---|---|---|---|---|
| 1 | 2026-07-03 | outflow | Rent | 11400 |
| 2 | 2026-07-05 | inflow | Customers | 96400 |
| 3 | 2026-07-10 | outflow | Suppliers | 74300 |
| 4 | 2026-07-15 | inflow | Customers | 88300 |
| 5 | 2026-07-20 | outflow | Energy | 4200 |
| 6 | 2026-07-25 | inflow | Customers | 61250 |
| 7 | 2026-07-28 | outflow | Payroll | 109300 |
| 8 | 2026-08-03 | outflow | Rent | 11400 |
| 9 | 2026-08-05 | inflow | Customers | 84100 |
| 10 | 2026-08-10 | outflow | Suppliers | 69800 |
| 11 | 2026-08-15 | inflow | Customers | 79500 |
| 12 | 2026-08-18 | inflow | Other | 23500 |
| 13 | 2026-08-20 | outflow | Energy | 4100 |
| 14 | 2026-08-25 | inflow | Customers | 52800 |
| 15 | 2026-08-28 | outflow | Payroll | 109300 |
| 16 | 2026-09-03 | outflow | Rent | 11400 |
| 17 | 2026-09-05 | inflow | Customers | 72600 |
| 18 | 2026-09-10 | outflow | Suppliers | 61200 |
| 19 | 2026-09-10 | outflow | Taxes | 14200 |
| 20 | 2026-09-15 | inflow | Customers | 64900 |
| 21 | 2026-09-15 | outflow | Capex | 86500 |
| 22 | 2026-09-20 | outflow | Energy | 4300 |
| 23 | 2026-09-25 | inflow | Customers | 41300 |
| 24 | 2026-09-28 | outflow | Payroll | 109300 |
| 25 | 2026-09-30 | outflow | Loan | 18500 |
| 26 | 2026-10-03 | outflow | Rent | 11400 |
| 27 | 2026-10-05 | inflow | Customers | 68800 |
| 28 | 2026-10-10 | outflow | Suppliers | 52700 |
| 29 | 2026-10-12 | outflow | Marketing | 12800 |
| 30 | 2026-10-15 | inflow | Customers | 59400 |
| 31 | 2026-10-20 | outflow | Energy | 4600 |
| 32 | 2026-10-25 | inflow | Customers | 39900 |
| 33 | 2026-10-28 | outflow | Payroll | 109300 |
| 34 | 2026-11-03 | outflow | Rent | 11400 |
| 35 | 2026-11-05 | inflow | Customers | 69700 |
| 36 | 2026-11-10 | outflow | Suppliers | 47900 |
| 37 | 2026-11-15 | inflow | Customers | 62100 |
| 38 | 2026-11-20 | outflow | Energy | 4900 |
| 39 | 2026-11-25 | inflow | Customers | 50600 |
| 40 | 2026-11-27 | outflow | Payroll | 54600 |
| 41 | 2026-11-28 | outflow | Payroll | 109300 |
| 42 | 2026-12-03 | outflow | Rent | 11400 |
| 43 | 2026-12-05 | inflow | Customers | 88900 |
| 44 | 2026-12-10 | outflow | Suppliers | 43100 |
| 45 | 2026-12-10 | outflow | Taxes | 14200 |
| 46 | 2026-12-12 | outflow | Marketing | 7400 |
| 47 | 2026-12-15 | inflow | Customers | 77300 |
| 48 | 2026-12-20 | outflow | Energy | 5100 |
| 49 | 2026-12-25 | inflow | Customers | 64800 |
| 50 | 2026-12-28 | outflow | Payroll | 109300 |
| 51 | 2026-12-30 | outflow | Loan | 18500 |
bank_balance 1 rows, 2 columns
| as_of | amount |
|---|---|
| 2026-06-30 | 144124.59 |
This exercise is part of Premium.
Premium unlocks every exercise in every lesson, unlimited practice with fresh tasks and the final test with certificate.
See Premium Already Premium? Log in
The first exercise in this lesson is free.
November is the pinch point: the year-end bonus lands on top of payroll while customers are still paying slowly. The shortfall shows how much headroom is missing. Typical answers are a talk about the credit line, pushing a supplier payment back, or offering customers an early payment discount.
Common mistakes
Net without a sign
SUM(amount) just adds inflows and outflows together. The exercise says: "Columns and row count match, but not every value."
Cutting the month wrongly
With substr(expected_date, 1, 10) you group by day. The exercise says: "Your query returns 48 rows. Wanted: 6 rows."
OVER without ORDER BY
SUM(net) OVER () is the total of all months, the same on every row. Without ORDER BY in the window there's no running total.
Opening balance missing or typed in
Without the opening balance, July's balance is just July's net. Type the bank statement figure in as a number and the result is right today but not next month. The exercise also checks with a different balance and then tells you the result only works with this data.
Running total over single payments
Put the window function straight on the cash plan and you get a balance after every payment: 51 rows instead of 6.
Window function in WHERE
If the balance sits in WHERE at the same level, the exercise explains that window functions can't go in WHERE and must be computed in a CTE first.
Check your result
- Task 1: September ends at −126,600 because capex, the loan repayment and taxes all fall due together.
- Task 2: the balance starts from 144,124.59 on 30 June and ends December at 63,174.59. Opening balance plus the sum of all six net amounts must equal the December figure.
- Task 3: exactly one month, November, with a shortfall of 8,825.41.
A 13-week cash plan is in the Excel cash flow case.
Back to the course overview.