Case 3: accounts receivable aging in SQL

How much do customers owe, and how old is it? You build the open items list as of the cut-off date, a dunning list of overdue invoices and the aging summary that belongs in every month-end pack.

In short: the open balance is i.amount - COALESCE(SUM(p.amount), 0) per invoice, with a LEFT JOIN to payments so unpaid invoices don't vanish. SQLite works out days past due with julianday('2026-06-30') - julianday(i.due_date). The aging buckets come from CASE, working up from the lowest limit.

The situation

It's the end of June. Before the close, accounts receivable has to deliver three things: a list of every open item as of 30 June, a dunning list of invoices more than 30 days past due, and the aging of the receivables balance for the management report. The bank will ask for the aging too, because old receivables are a credit risk.

The tables

  • invoices: 60 customer invoices from January to June with customer, invoice_date, due_date and amount. Payment terms vary by customer: mostly 30 days, 60 for one online retailer and 14 for another customer.
  • payments: customer receipts up to 30 June with invoice_id, payment_date and amount. An invoice can have no payment, one or several.

What "several payments" means for a query shows up in a LEFT JOIN for three invoices:

SELECT i.invoice_id, i.amount, p.payment_date, p.amount
FROM invoices i
LEFT JOIN payments p ON p.invoice_id = i.invoice_id
WHERE i.invoice_id IN (26018, 26039, 26049);
invoice_idamountpayment_dateamount
2601814692.65NULLNULL
2603950138.682026-05-2625000
2603950138.682026-06-2525138.68
2604946653.82026-06-2523300

Invoice 26018 has no payment, so the payment columns are NULL. Invoice 26039 shows up twice because it was paid in two parts, and 26049 is only half paid. That leads to the two key rules of this case: LEFT JOIN so 26018 stays, and never sum the invoice amount, or 26039 counts twice.

Task 1: open items at the cut-off date

Task Accounts receivable needs the open items list as of 30 June 2026. Show every invoice that isn't fully paid: invoice_id, customer, due_date and open_amount (invoice amount minus all payments against it). Invoices with no payment at all belong on the list too, of course. The payments in the table run to 30 June.

invoices 60 rows, 5 columns
invoice_idcustomerinvoice_datedue_dateamount
26001Northgate Cycles Ltd2026-01-032026-02-0226959.83
26002Riverside Bike Co.2026-01-062026-02-0514721.94
26003Hillcrest Cycle Shop2026-01-082026-02-0716992.65
26004Pedal Point Inc.2026-01-122026-02-1123417.22
26005Urban Wheels Online2026-01-152026-03-1628124.62
26006Summit Outdoor Supply2026-01-172026-02-1620273.54
26007Coastline Cycles2026-01-212026-02-2012687.37
26008Greenway Bikes2026-01-232026-02-2211730.56
26009Two Wheel Depot2026-01-262026-02-2519622.5
26010Maple Street Cycles2026-01-292026-02-1215772.49
26011Northgate Cycles Ltd2026-02-032026-03-0534472.71
26012Riverside Bike Co.2026-02-052026-03-0721466.08
26013Hillcrest Cycle Shop2026-02-092026-03-1120975.08
26014Pedal Point Inc.2026-02-122026-03-1430082.56
26015Urban Wheels Online2026-02-142026-04-1540785.91
26016Summit Outdoor Supply2026-02-182026-03-2024787.09
26017Coastline Cycles2026-02-212026-03-2316231.53
26018Greenway Bikes2026-02-242026-03-2614692.65
26019Two Wheel Depot2026-02-262026-03-2826205.7
26020Maple Street Cycles2026-02-262026-03-1219640.44
26021Northgate Cycles Ltd2026-03-032026-04-0254429.19
26022Riverside Bike Co.2026-03-062026-04-0532466.62
26023Hillcrest Cycle Shop2026-03-082026-04-0734701.33
26024Pedal Point Inc.2026-03-112026-04-1052082.83
26025Urban Wheels Online2026-03-142026-05-1359991.26
26026Summit Outdoor Supply2026-03-172026-04-1637796.33
26027Coastline Cycles2026-03-202026-04-1924397.37
26028Greenway Bikes2026-03-232026-04-2221785.83
26029Two Wheel Depot2026-03-272026-04-2639812.27
26030Maple Street Cycles2026-03-292026-04-1218452.9
26031Northgate Cycles Ltd2026-04-022026-05-0259320
26032Riverside Bike Co.2026-04-062026-05-0637402.19
26033Hillcrest Cycle Shop2026-04-082026-05-0842329.35
26034Pedal Point Inc.2026-04-112026-05-1161184.01
26035Urban Wheels Online2026-04-142026-06-1375122.1
26036Summit Outdoor Supply2026-04-182026-05-1842952.05
26037Coastline Cycles2026-04-202026-05-2029968.46
26038Greenway Bikes2026-04-242026-05-2425966.97
26039Two Wheel Depot2026-04-262026-05-2650138.68
26040Maple Street Cycles2026-04-282026-05-1227754.11
26041Northgate Cycles Ltd2026-05-032026-06-0265530.8
26042Riverside Bike Co.2026-05-062026-06-0537631.02
26043Hillcrest Cycle Shop2026-05-082026-06-0741637.92
26044Pedal Point Inc.2026-05-112026-06-1057840.47
26045Urban Wheels Online2026-05-142026-07-1368239.27
26046Summit Outdoor Supply2026-05-172026-06-1644576.3
26047Coastline Cycles2026-05-212026-06-2028255.84
26048Greenway Bikes2026-05-242026-06-2325800.16
26049Two Wheel Depot2026-05-262026-06-2546653.8
26050Maple Street Cycles2026-05-292026-06-1221711
26051Northgate Cycles Ltd2026-06-032026-07-0348415.21
26052Riverside Bike Co.2026-06-062026-07-0627191.07
26053Hillcrest Cycle Shop2026-06-092026-07-0932156.21
26054Pedal Point Inc.2026-06-122026-07-1246139.99
26055Urban Wheels Online2026-06-302026-08-2955652
26056Summit Outdoor Supply2026-06-172026-07-1737295.37
26057Coastline Cycles2026-06-202026-07-2026031.62
26058Greenway Bikes2026-06-232026-07-2321187.55
26059Two Wheel Depot2026-06-272026-07-2741200.86
26060Maple Street Cycles2026-06-282026-07-1214292.64
payments 48 rows, 4 columns
payment_idinvoice_idpayment_dateamount
1260012026-01-3126959.83
2260022026-01-3114721.94
3260032026-02-0216992.65
4260042026-02-0823417.22
5260102026-02-0915772.49
6260082026-02-1911730.56
7260092026-02-259800
8260112026-03-0334472.71
9260122026-03-0721466.08
10260132026-03-0920975.08
11260202026-03-0919640.44
12260142026-03-1130082.56
13260072026-03-1312687.37
14260052026-03-1628124.62
15260062026-03-1620273.54
16260092026-03-279822.5
17260192026-03-2813100
18260212026-03-2854429.19
19260222026-03-3032466.62
20260232026-04-0534701.33
21260242026-04-0552082.83
22260302026-04-0918452.9
23260152026-04-1040785.91
24260172026-04-1710000
25260282026-04-2221785.83
26260162026-04-2524787.09
27260292026-04-2619900
28260192026-04-2713105.7
29260312026-05-0159320
30260322026-05-0537402.19
31260332026-05-0542329.35
32260342026-05-0661184.01
33260402026-05-1027754.11
34260252026-05-1159991.26
35260262026-05-1137796.33
36260382026-05-2425966.97
37260292026-05-2619912.27
38260392026-05-2625000
39260412026-06-0165530.8
40260432026-06-0241637.92
41260422026-06-0537631.02
42260502026-06-0821711
43260442026-06-0957840.47
44260352026-06-1375122.1
45260482026-06-2225800.16
46260362026-06-2342952.05
47260392026-06-2525138.68
48260492026-06-2523300

Group by invoice and take the sum of payments off the amount. For an invoice with no payment, SUM(p.amount) returns NULL, and amount minus NULL is NULL again. COALESCE(SUM(p.amount), 0) turns it into 0; the NULL lesson has more. Because open_amount only exists after grouping, you filter with HAVING, not WHERE.

Task 2: the dunning list

For dunning, what counts isn't when the invoice was raised but how long the payment terms have been up. SQLite stores these dates as text. julianday turns a date into a running day number, and the difference between two of them is the gap in days. More date functions are in the dates in SQL lesson.

SELECT julianday('2026-06-30') - julianday('2026-05-20') AS days;
days
41.0

Task For dunning, what counts is how long an item has been overdue. Show all open items whose due date is more than 30 days before the cut-off date of 30 June 2026: customer, invoice_id, open_amount and days_overdue (days from the due date to the cut-off). Sorted by days_overdue, oldest first.

invoices 60 rows, 5 columns
invoice_idcustomerinvoice_datedue_dateamount
26001Northgate Cycles Ltd2026-01-032026-02-0226959.83
26002Riverside Bike Co.2026-01-062026-02-0514721.94
26003Hillcrest Cycle Shop2026-01-082026-02-0716992.65
26004Pedal Point Inc.2026-01-122026-02-1123417.22
26005Urban Wheels Online2026-01-152026-03-1628124.62
26006Summit Outdoor Supply2026-01-172026-02-1620273.54
26007Coastline Cycles2026-01-212026-02-2012687.37
26008Greenway Bikes2026-01-232026-02-2211730.56
26009Two Wheel Depot2026-01-262026-02-2519622.5
26010Maple Street Cycles2026-01-292026-02-1215772.49
26011Northgate Cycles Ltd2026-02-032026-03-0534472.71
26012Riverside Bike Co.2026-02-052026-03-0721466.08
26013Hillcrest Cycle Shop2026-02-092026-03-1120975.08
26014Pedal Point Inc.2026-02-122026-03-1430082.56
26015Urban Wheels Online2026-02-142026-04-1540785.91
26016Summit Outdoor Supply2026-02-182026-03-2024787.09
26017Coastline Cycles2026-02-212026-03-2316231.53
26018Greenway Bikes2026-02-242026-03-2614692.65
26019Two Wheel Depot2026-02-262026-03-2826205.7
26020Maple Street Cycles2026-02-262026-03-1219640.44
26021Northgate Cycles Ltd2026-03-032026-04-0254429.19
26022Riverside Bike Co.2026-03-062026-04-0532466.62
26023Hillcrest Cycle Shop2026-03-082026-04-0734701.33
26024Pedal Point Inc.2026-03-112026-04-1052082.83
26025Urban Wheels Online2026-03-142026-05-1359991.26
26026Summit Outdoor Supply2026-03-172026-04-1637796.33
26027Coastline Cycles2026-03-202026-04-1924397.37
26028Greenway Bikes2026-03-232026-04-2221785.83
26029Two Wheel Depot2026-03-272026-04-2639812.27
26030Maple Street Cycles2026-03-292026-04-1218452.9
26031Northgate Cycles Ltd2026-04-022026-05-0259320
26032Riverside Bike Co.2026-04-062026-05-0637402.19
26033Hillcrest Cycle Shop2026-04-082026-05-0842329.35
26034Pedal Point Inc.2026-04-112026-05-1161184.01
26035Urban Wheels Online2026-04-142026-06-1375122.1
26036Summit Outdoor Supply2026-04-182026-05-1842952.05
26037Coastline Cycles2026-04-202026-05-2029968.46
26038Greenway Bikes2026-04-242026-05-2425966.97
26039Two Wheel Depot2026-04-262026-05-2650138.68
26040Maple Street Cycles2026-04-282026-05-1227754.11
26041Northgate Cycles Ltd2026-05-032026-06-0265530.8
26042Riverside Bike Co.2026-05-062026-06-0537631.02
26043Hillcrest Cycle Shop2026-05-082026-06-0741637.92
26044Pedal Point Inc.2026-05-112026-06-1057840.47
26045Urban Wheels Online2026-05-142026-07-1368239.27
26046Summit Outdoor Supply2026-05-172026-06-1644576.3
26047Coastline Cycles2026-05-212026-06-2028255.84
26048Greenway Bikes2026-05-242026-06-2325800.16
26049Two Wheel Depot2026-05-262026-06-2546653.8
26050Maple Street Cycles2026-05-292026-06-1221711
26051Northgate Cycles Ltd2026-06-032026-07-0348415.21
26052Riverside Bike Co.2026-06-062026-07-0627191.07
26053Hillcrest Cycle Shop2026-06-092026-07-0932156.21
26054Pedal Point Inc.2026-06-122026-07-1246139.99
26055Urban Wheels Online2026-06-302026-08-2955652
26056Summit Outdoor Supply2026-06-172026-07-1737295.37
26057Coastline Cycles2026-06-202026-07-2026031.62
26058Greenway Bikes2026-06-232026-07-2321187.55
26059Two Wheel Depot2026-06-272026-07-2741200.86
26060Maple Street Cycles2026-06-282026-07-1214292.64
payments 48 rows, 4 columns
payment_idinvoice_idpayment_dateamount
1260012026-01-3126959.83
2260022026-01-3114721.94
3260032026-02-0216992.65
4260042026-02-0823417.22
5260102026-02-0915772.49
6260082026-02-1911730.56
7260092026-02-259800
8260112026-03-0334472.71
9260122026-03-0721466.08
10260132026-03-0920975.08
11260202026-03-0919640.44
12260142026-03-1130082.56
13260072026-03-1312687.37
14260052026-03-1628124.62
15260062026-03-1620273.54
16260092026-03-279822.5
17260192026-03-2813100
18260212026-03-2854429.19
19260222026-03-3032466.62
20260232026-04-0534701.33
21260242026-04-0552082.83
22260302026-04-0918452.9
23260152026-04-1040785.91
24260172026-04-1710000
25260282026-04-2221785.83
26260162026-04-2524787.09
27260292026-04-2619900
28260192026-04-2713105.7
29260312026-05-0159320
30260322026-05-0537402.19
31260332026-05-0542329.35
32260342026-05-0661184.01
33260402026-05-1027754.11
34260252026-05-1159991.26
35260262026-05-1137796.33
36260382026-05-2425966.97
37260292026-05-2619912.27
38260392026-05-2625000
39260412026-06-0165530.8
40260432026-06-0241637.92
41260422026-06-0537631.02
42260502026-06-0821711
43260442026-06-0957840.47
44260352026-06-1375122.1
45260482026-06-2225800.16
46260362026-06-2342952.05
47260392026-06-2525138.68
48260492026-06-2523300

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.

The cut-off date is fixed in the query, and that's right here: the report is for 30 June. With date('now') you'd get a different answer every day, and nobody could reproduce the report.

Task 3: the aging summary

The aging summary spreads open items over buckets by days past due. The usual ones are current, 1 to 30, 31 to 60, 61 to 90 and over 90 days. That takes two levels: first open_amount and days_overdue per invoice as in task 2, then count and total per bucket. Write the first level as a CTE; the second groups by the bucket that CASE assigns.

Task For the monthly pack, the aging of open receivables as of 30 June 2026: bucket, invoices (number of open invoices) and open_amount (sum of open amounts). The buckets by days past due are called 'current' (0 days or less), '1-30', '31-60', '61-90' and 'over 90'.

invoices 60 rows, 5 columns
invoice_idcustomerinvoice_datedue_dateamount
26001Northgate Cycles Ltd2026-01-032026-02-0226959.83
26002Riverside Bike Co.2026-01-062026-02-0514721.94
26003Hillcrest Cycle Shop2026-01-082026-02-0716992.65
26004Pedal Point Inc.2026-01-122026-02-1123417.22
26005Urban Wheels Online2026-01-152026-03-1628124.62
26006Summit Outdoor Supply2026-01-172026-02-1620273.54
26007Coastline Cycles2026-01-212026-02-2012687.37
26008Greenway Bikes2026-01-232026-02-2211730.56
26009Two Wheel Depot2026-01-262026-02-2519622.5
26010Maple Street Cycles2026-01-292026-02-1215772.49
26011Northgate Cycles Ltd2026-02-032026-03-0534472.71
26012Riverside Bike Co.2026-02-052026-03-0721466.08
26013Hillcrest Cycle Shop2026-02-092026-03-1120975.08
26014Pedal Point Inc.2026-02-122026-03-1430082.56
26015Urban Wheels Online2026-02-142026-04-1540785.91
26016Summit Outdoor Supply2026-02-182026-03-2024787.09
26017Coastline Cycles2026-02-212026-03-2316231.53
26018Greenway Bikes2026-02-242026-03-2614692.65
26019Two Wheel Depot2026-02-262026-03-2826205.7
26020Maple Street Cycles2026-02-262026-03-1219640.44
26021Northgate Cycles Ltd2026-03-032026-04-0254429.19
26022Riverside Bike Co.2026-03-062026-04-0532466.62
26023Hillcrest Cycle Shop2026-03-082026-04-0734701.33
26024Pedal Point Inc.2026-03-112026-04-1052082.83
26025Urban Wheels Online2026-03-142026-05-1359991.26
26026Summit Outdoor Supply2026-03-172026-04-1637796.33
26027Coastline Cycles2026-03-202026-04-1924397.37
26028Greenway Bikes2026-03-232026-04-2221785.83
26029Two Wheel Depot2026-03-272026-04-2639812.27
26030Maple Street Cycles2026-03-292026-04-1218452.9
26031Northgate Cycles Ltd2026-04-022026-05-0259320
26032Riverside Bike Co.2026-04-062026-05-0637402.19
26033Hillcrest Cycle Shop2026-04-082026-05-0842329.35
26034Pedal Point Inc.2026-04-112026-05-1161184.01
26035Urban Wheels Online2026-04-142026-06-1375122.1
26036Summit Outdoor Supply2026-04-182026-05-1842952.05
26037Coastline Cycles2026-04-202026-05-2029968.46
26038Greenway Bikes2026-04-242026-05-2425966.97
26039Two Wheel Depot2026-04-262026-05-2650138.68
26040Maple Street Cycles2026-04-282026-05-1227754.11
26041Northgate Cycles Ltd2026-05-032026-06-0265530.8
26042Riverside Bike Co.2026-05-062026-06-0537631.02
26043Hillcrest Cycle Shop2026-05-082026-06-0741637.92
26044Pedal Point Inc.2026-05-112026-06-1057840.47
26045Urban Wheels Online2026-05-142026-07-1368239.27
26046Summit Outdoor Supply2026-05-172026-06-1644576.3
26047Coastline Cycles2026-05-212026-06-2028255.84
26048Greenway Bikes2026-05-242026-06-2325800.16
26049Two Wheel Depot2026-05-262026-06-2546653.8
26050Maple Street Cycles2026-05-292026-06-1221711
26051Northgate Cycles Ltd2026-06-032026-07-0348415.21
26052Riverside Bike Co.2026-06-062026-07-0627191.07
26053Hillcrest Cycle Shop2026-06-092026-07-0932156.21
26054Pedal Point Inc.2026-06-122026-07-1246139.99
26055Urban Wheels Online2026-06-302026-08-2955652
26056Summit Outdoor Supply2026-06-172026-07-1737295.37
26057Coastline Cycles2026-06-202026-07-2026031.62
26058Greenway Bikes2026-06-232026-07-2321187.55
26059Two Wheel Depot2026-06-272026-07-2741200.86
26060Maple Street Cycles2026-06-282026-07-1214292.64
payments 48 rows, 4 columns
payment_idinvoice_idpayment_dateamount
1260012026-01-3126959.83
2260022026-01-3114721.94
3260032026-02-0216992.65
4260042026-02-0823417.22
5260102026-02-0915772.49
6260082026-02-1911730.56
7260092026-02-259800
8260112026-03-0334472.71
9260122026-03-0721466.08
10260132026-03-0920975.08
11260202026-03-0919640.44
12260142026-03-1130082.56
13260072026-03-1312687.37
14260052026-03-1628124.62
15260062026-03-1620273.54
16260092026-03-279822.5
17260192026-03-2813100
18260212026-03-2854429.19
19260222026-03-3032466.62
20260232026-04-0534701.33
21260242026-04-0552082.83
22260302026-04-0918452.9
23260152026-04-1040785.91
24260172026-04-1710000
25260282026-04-2221785.83
26260162026-04-2524787.09
27260292026-04-2619900
28260192026-04-2713105.7
29260312026-05-0159320
30260322026-05-0537402.19
31260332026-05-0542329.35
32260342026-05-0661184.01
33260402026-05-1027754.11
34260252026-05-1159991.26
35260262026-05-1137796.33
36260382026-05-2425966.97
37260292026-05-2619912.27
38260392026-05-2625000
39260412026-06-0165530.8
40260432026-06-0241637.92
41260422026-06-0537631.02
42260502026-06-0821711
43260442026-06-0957840.47
44260352026-06-1375122.1
45260482026-06-2225800.16
46260362026-06-2342952.05
47260392026-06-2525138.68
48260492026-06-2523300

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.

CASE checks from top to bottom and takes the first condition that's true. Start with days_overdue <= 0 and work upwards, and the upper limit is all each bucket needs.

Common mistakes

JOIN instead of LEFT JOIN

A plain JOIN throws out every invoice with no payment, and those are the open items that matter most. The exercise says: "Your query returns only 2 rows. Wanted: 18 rows."

Forgetting COALESCE

Even with LEFT JOIN, only 2 rows are left if you write i.amount - SUM(p.amount): for unpaid invoices that's NULL, and NULL > 0 isn't true.

Summing the invoice amount

SUM(i.amount) - SUM(p.amount) counts the amount once per payment. Invoices settled in two parts suddenly look open, and the exercise reports too many rows.

Condition in WHERE

WHERE open_amount > 0 fails because open_amount contains a sum. The exercise explains that conditions on groups belong in HAVING.

Counting from the invoice date

Use julianday(i.invoice_date) instead of julianday(i.due_date) and you chase customers whose terms are still running. The exercise reports 8 rows instead of 4.

Bucket limits in the wrong order

If CASE starts with days_overdue <= 90, everything overdue lands in "61-90". And with < instead of <=, an item exactly 30 days past due slips into the wrong bucket; the hidden check data catches that.

Days between two dates in MySQL, PostgreSQL, SQL Server and SQLite

The logic of this case is the same everywhere. Only the gap between two dates is written differently:

DatabaseDays past dueNote
MySQLDATEDIFF('2026-06-30', due_date)first argument minus second, in days; times are ignored
PostgreSQLDATE '2026-06-30' - due_datedate minus date gives the number of days as an integer (column of type date)
SQL ServerDATEDIFF(day, due_date, '2026-06-30')start first, then end; counts the day boundaries crossed
SQLite (these exercises)julianday('2026-06-30') - julianday(due_date)returns a decimal, a whole number for plain dates without a time

Watch the order: MySQL subtracts the second argument from the first, while SQL Server counts from the second argument to the third. Swap them and you get negative days.

Sources: MySQL Reference Manual (Date and Time Functions), PostgreSQL documentation (Date/Time Functions and Operators), Microsoft Learn (DATEDIFF), sqlite.org (Date And Time Functions).

Check your result

  • Task 1: 18 open items totalling 589,277.74.
  • Task 2: four invoices, three of them from Coastline Cycles, the oldest 99 days past due.
  • Task 3: the five buckets again add up to 589,277.74 and 18 invoices. In case 6, month-end close, you compare that total with the receivables account in the general ledger.

Next: case 4, cost centers

What share of costs does each cost center carry, and what is its biggest cost type? With window functions.

Go to case 4

Back to the course overview.

Write to us

Choose language

Select your preferred language