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_datedirectioncategoryamount
2026-09-03outflowRent11400
2026-09-05inflowCustomers72600
2026-09-10outflowSuppliers61200
2026-09-10outflowTaxes14200
2026-09-15inflowCustomers64900
2026-09-15outflowCapex86500
2026-09-20outflowEnergy4300
2026-09-25inflowCustomers41300
2026-09-28outflowPayroll109300
2026-09-30outflowLoan18500

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_idexpected_datedirectioncategoryamount
12026-07-03outflowRent11400
22026-07-05inflowCustomers96400
32026-07-10outflowSuppliers74300
42026-07-15inflowCustomers88300
52026-07-20outflowEnergy4200
62026-07-25inflowCustomers61250
72026-07-28outflowPayroll109300
82026-08-03outflowRent11400
92026-08-05inflowCustomers84100
102026-08-10outflowSuppliers69800
112026-08-15inflowCustomers79500
122026-08-18inflowOther23500
132026-08-20outflowEnergy4100
142026-08-25inflowCustomers52800
152026-08-28outflowPayroll109300
162026-09-03outflowRent11400
172026-09-05inflowCustomers72600
182026-09-10outflowSuppliers61200
192026-09-10outflowTaxes14200
202026-09-15inflowCustomers64900
212026-09-15outflowCapex86500
222026-09-20outflowEnergy4300
232026-09-25inflowCustomers41300
242026-09-28outflowPayroll109300
252026-09-30outflowLoan18500
262026-10-03outflowRent11400
272026-10-05inflowCustomers68800
282026-10-10outflowSuppliers52700
292026-10-12outflowMarketing12800
302026-10-15inflowCustomers59400
312026-10-20outflowEnergy4600
322026-10-25inflowCustomers39900
332026-10-28outflowPayroll109300
342026-11-03outflowRent11400
352026-11-05inflowCustomers69700
362026-11-10outflowSuppliers47900
372026-11-15inflowCustomers62100
382026-11-20outflowEnergy4900
392026-11-25inflowCustomers50600
402026-11-27outflowPayroll54600
412026-11-28outflowPayroll109300
422026-12-03outflowRent11400
432026-12-05inflowCustomers88900
442026-12-10outflowSuppliers43100
452026-12-10outflowTaxes14200
462026-12-12outflowMarketing7400
472026-12-15inflowCustomers77300
482026-12-20outflowEnergy5100
492026-12-25inflowCustomers64800
502026-12-28outflowPayroll109300
512026-12-30outflowLoan18500

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_idexpected_datedirectioncategoryamount
12026-07-03outflowRent11400
22026-07-05inflowCustomers96400
32026-07-10outflowSuppliers74300
42026-07-15inflowCustomers88300
52026-07-20outflowEnergy4200
62026-07-25inflowCustomers61250
72026-07-28outflowPayroll109300
82026-08-03outflowRent11400
92026-08-05inflowCustomers84100
102026-08-10outflowSuppliers69800
112026-08-15inflowCustomers79500
122026-08-18inflowOther23500
132026-08-20outflowEnergy4100
142026-08-25inflowCustomers52800
152026-08-28outflowPayroll109300
162026-09-03outflowRent11400
172026-09-05inflowCustomers72600
182026-09-10outflowSuppliers61200
192026-09-10outflowTaxes14200
202026-09-15inflowCustomers64900
212026-09-15outflowCapex86500
222026-09-20outflowEnergy4300
232026-09-25inflowCustomers41300
242026-09-28outflowPayroll109300
252026-09-30outflowLoan18500
262026-10-03outflowRent11400
272026-10-05inflowCustomers68800
282026-10-10outflowSuppliers52700
292026-10-12outflowMarketing12800
302026-10-15inflowCustomers59400
312026-10-20outflowEnergy4600
322026-10-25inflowCustomers39900
332026-10-28outflowPayroll109300
342026-11-03outflowRent11400
352026-11-05inflowCustomers69700
362026-11-10outflowSuppliers47900
372026-11-15inflowCustomers62100
382026-11-20outflowEnergy4900
392026-11-25inflowCustomers50600
402026-11-27outflowPayroll54600
412026-11-28outflowPayroll109300
422026-12-03outflowRent11400
432026-12-05inflowCustomers88900
442026-12-10outflowSuppliers43100
452026-12-10outflowTaxes14200
462026-12-12outflowMarketing7400
472026-12-15inflowCustomers77300
482026-12-20outflowEnergy5100
492026-12-25inflowCustomers64800
502026-12-28outflowPayroll109300
512026-12-30outflowLoan18500
bank_balance 1 rows, 2 columns
as_ofamount
2026-06-30144124.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_idexpected_datedirectioncategoryamount
12026-07-03outflowRent11400
22026-07-05inflowCustomers96400
32026-07-10outflowSuppliers74300
42026-07-15inflowCustomers88300
52026-07-20outflowEnergy4200
62026-07-25inflowCustomers61250
72026-07-28outflowPayroll109300
82026-08-03outflowRent11400
92026-08-05inflowCustomers84100
102026-08-10outflowSuppliers69800
112026-08-15inflowCustomers79500
122026-08-18inflowOther23500
132026-08-20outflowEnergy4100
142026-08-25inflowCustomers52800
152026-08-28outflowPayroll109300
162026-09-03outflowRent11400
172026-09-05inflowCustomers72600
182026-09-10outflowSuppliers61200
192026-09-10outflowTaxes14200
202026-09-15inflowCustomers64900
212026-09-15outflowCapex86500
222026-09-20outflowEnergy4300
232026-09-25inflowCustomers41300
242026-09-28outflowPayroll109300
252026-09-30outflowLoan18500
262026-10-03outflowRent11400
272026-10-05inflowCustomers68800
282026-10-10outflowSuppliers52700
292026-10-12outflowMarketing12800
302026-10-15inflowCustomers59400
312026-10-20outflowEnergy4600
322026-10-25inflowCustomers39900
332026-10-28outflowPayroll109300
342026-11-03outflowRent11400
352026-11-05inflowCustomers69700
362026-11-10outflowSuppliers47900
372026-11-15inflowCustomers62100
382026-11-20outflowEnergy4900
392026-11-25inflowCustomers50600
402026-11-27outflowPayroll54600
412026-11-28outflowPayroll109300
422026-12-03outflowRent11400
432026-12-05inflowCustomers88900
442026-12-10outflowSuppliers43100
452026-12-10outflowTaxes14200
462026-12-12outflowMarketing7400
472026-12-15inflowCustomers77300
482026-12-20outflowEnergy5100
492026-12-25inflowCustomers64800
502026-12-28outflowPayroll109300
512026-12-30outflowLoan18500
bank_balance 1 rows, 2 columns
as_ofamount
2026-06-30144124.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.

Next: case 6, month-end close

Do debits equal credits on every document? Are cost centers missing? Do invoices and the ledger agree? Three checks that make every close safer.

Go to case 6

Back to the course overview.

Write to us

Choose language

Select your preferred language