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_id | amount | payment_date | amount |
|---|---|---|---|
| 26018 | 14692.65 | NULL | NULL |
| 26039 | 50138.68 | 2026-05-26 | 25000 |
| 26039 | 50138.68 | 2026-06-25 | 25138.68 |
| 26049 | 46653.8 | 2026-06-25 | 23300 |
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_id | customer | invoice_date | due_date | amount |
|---|---|---|---|---|
| 26001 | Northgate Cycles Ltd | 2026-01-03 | 2026-02-02 | 26959.83 |
| 26002 | Riverside Bike Co. | 2026-01-06 | 2026-02-05 | 14721.94 |
| 26003 | Hillcrest Cycle Shop | 2026-01-08 | 2026-02-07 | 16992.65 |
| 26004 | Pedal Point Inc. | 2026-01-12 | 2026-02-11 | 23417.22 |
| 26005 | Urban Wheels Online | 2026-01-15 | 2026-03-16 | 28124.62 |
| 26006 | Summit Outdoor Supply | 2026-01-17 | 2026-02-16 | 20273.54 |
| 26007 | Coastline Cycles | 2026-01-21 | 2026-02-20 | 12687.37 |
| 26008 | Greenway Bikes | 2026-01-23 | 2026-02-22 | 11730.56 |
| 26009 | Two Wheel Depot | 2026-01-26 | 2026-02-25 | 19622.5 |
| 26010 | Maple Street Cycles | 2026-01-29 | 2026-02-12 | 15772.49 |
| 26011 | Northgate Cycles Ltd | 2026-02-03 | 2026-03-05 | 34472.71 |
| 26012 | Riverside Bike Co. | 2026-02-05 | 2026-03-07 | 21466.08 |
| 26013 | Hillcrest Cycle Shop | 2026-02-09 | 2026-03-11 | 20975.08 |
| 26014 | Pedal Point Inc. | 2026-02-12 | 2026-03-14 | 30082.56 |
| 26015 | Urban Wheels Online | 2026-02-14 | 2026-04-15 | 40785.91 |
| 26016 | Summit Outdoor Supply | 2026-02-18 | 2026-03-20 | 24787.09 |
| 26017 | Coastline Cycles | 2026-02-21 | 2026-03-23 | 16231.53 |
| 26018 | Greenway Bikes | 2026-02-24 | 2026-03-26 | 14692.65 |
| 26019 | Two Wheel Depot | 2026-02-26 | 2026-03-28 | 26205.7 |
| 26020 | Maple Street Cycles | 2026-02-26 | 2026-03-12 | 19640.44 |
| 26021 | Northgate Cycles Ltd | 2026-03-03 | 2026-04-02 | 54429.19 |
| 26022 | Riverside Bike Co. | 2026-03-06 | 2026-04-05 | 32466.62 |
| 26023 | Hillcrest Cycle Shop | 2026-03-08 | 2026-04-07 | 34701.33 |
| 26024 | Pedal Point Inc. | 2026-03-11 | 2026-04-10 | 52082.83 |
| 26025 | Urban Wheels Online | 2026-03-14 | 2026-05-13 | 59991.26 |
| 26026 | Summit Outdoor Supply | 2026-03-17 | 2026-04-16 | 37796.33 |
| 26027 | Coastline Cycles | 2026-03-20 | 2026-04-19 | 24397.37 |
| 26028 | Greenway Bikes | 2026-03-23 | 2026-04-22 | 21785.83 |
| 26029 | Two Wheel Depot | 2026-03-27 | 2026-04-26 | 39812.27 |
| 26030 | Maple Street Cycles | 2026-03-29 | 2026-04-12 | 18452.9 |
| 26031 | Northgate Cycles Ltd | 2026-04-02 | 2026-05-02 | 59320 |
| 26032 | Riverside Bike Co. | 2026-04-06 | 2026-05-06 | 37402.19 |
| 26033 | Hillcrest Cycle Shop | 2026-04-08 | 2026-05-08 | 42329.35 |
| 26034 | Pedal Point Inc. | 2026-04-11 | 2026-05-11 | 61184.01 |
| 26035 | Urban Wheels Online | 2026-04-14 | 2026-06-13 | 75122.1 |
| 26036 | Summit Outdoor Supply | 2026-04-18 | 2026-05-18 | 42952.05 |
| 26037 | Coastline Cycles | 2026-04-20 | 2026-05-20 | 29968.46 |
| 26038 | Greenway Bikes | 2026-04-24 | 2026-05-24 | 25966.97 |
| 26039 | Two Wheel Depot | 2026-04-26 | 2026-05-26 | 50138.68 |
| 26040 | Maple Street Cycles | 2026-04-28 | 2026-05-12 | 27754.11 |
| 26041 | Northgate Cycles Ltd | 2026-05-03 | 2026-06-02 | 65530.8 |
| 26042 | Riverside Bike Co. | 2026-05-06 | 2026-06-05 | 37631.02 |
| 26043 | Hillcrest Cycle Shop | 2026-05-08 | 2026-06-07 | 41637.92 |
| 26044 | Pedal Point Inc. | 2026-05-11 | 2026-06-10 | 57840.47 |
| 26045 | Urban Wheels Online | 2026-05-14 | 2026-07-13 | 68239.27 |
| 26046 | Summit Outdoor Supply | 2026-05-17 | 2026-06-16 | 44576.3 |
| 26047 | Coastline Cycles | 2026-05-21 | 2026-06-20 | 28255.84 |
| 26048 | Greenway Bikes | 2026-05-24 | 2026-06-23 | 25800.16 |
| 26049 | Two Wheel Depot | 2026-05-26 | 2026-06-25 | 46653.8 |
| 26050 | Maple Street Cycles | 2026-05-29 | 2026-06-12 | 21711 |
| 26051 | Northgate Cycles Ltd | 2026-06-03 | 2026-07-03 | 48415.21 |
| 26052 | Riverside Bike Co. | 2026-06-06 | 2026-07-06 | 27191.07 |
| 26053 | Hillcrest Cycle Shop | 2026-06-09 | 2026-07-09 | 32156.21 |
| 26054 | Pedal Point Inc. | 2026-06-12 | 2026-07-12 | 46139.99 |
| 26055 | Urban Wheels Online | 2026-06-30 | 2026-08-29 | 55652 |
| 26056 | Summit Outdoor Supply | 2026-06-17 | 2026-07-17 | 37295.37 |
| 26057 | Coastline Cycles | 2026-06-20 | 2026-07-20 | 26031.62 |
| 26058 | Greenway Bikes | 2026-06-23 | 2026-07-23 | 21187.55 |
| 26059 | Two Wheel Depot | 2026-06-27 | 2026-07-27 | 41200.86 |
| 26060 | Maple Street Cycles | 2026-06-28 | 2026-07-12 | 14292.64 |
payments 48 rows, 4 columns
| payment_id | invoice_id | payment_date | amount |
|---|---|---|---|
| 1 | 26001 | 2026-01-31 | 26959.83 |
| 2 | 26002 | 2026-01-31 | 14721.94 |
| 3 | 26003 | 2026-02-02 | 16992.65 |
| 4 | 26004 | 2026-02-08 | 23417.22 |
| 5 | 26010 | 2026-02-09 | 15772.49 |
| 6 | 26008 | 2026-02-19 | 11730.56 |
| 7 | 26009 | 2026-02-25 | 9800 |
| 8 | 26011 | 2026-03-03 | 34472.71 |
| 9 | 26012 | 2026-03-07 | 21466.08 |
| 10 | 26013 | 2026-03-09 | 20975.08 |
| 11 | 26020 | 2026-03-09 | 19640.44 |
| 12 | 26014 | 2026-03-11 | 30082.56 |
| 13 | 26007 | 2026-03-13 | 12687.37 |
| 14 | 26005 | 2026-03-16 | 28124.62 |
| 15 | 26006 | 2026-03-16 | 20273.54 |
| 16 | 26009 | 2026-03-27 | 9822.5 |
| 17 | 26019 | 2026-03-28 | 13100 |
| 18 | 26021 | 2026-03-28 | 54429.19 |
| 19 | 26022 | 2026-03-30 | 32466.62 |
| 20 | 26023 | 2026-04-05 | 34701.33 |
| 21 | 26024 | 2026-04-05 | 52082.83 |
| 22 | 26030 | 2026-04-09 | 18452.9 |
| 23 | 26015 | 2026-04-10 | 40785.91 |
| 24 | 26017 | 2026-04-17 | 10000 |
| 25 | 26028 | 2026-04-22 | 21785.83 |
| 26 | 26016 | 2026-04-25 | 24787.09 |
| 27 | 26029 | 2026-04-26 | 19900 |
| 28 | 26019 | 2026-04-27 | 13105.7 |
| 29 | 26031 | 2026-05-01 | 59320 |
| 30 | 26032 | 2026-05-05 | 37402.19 |
| 31 | 26033 | 2026-05-05 | 42329.35 |
| 32 | 26034 | 2026-05-06 | 61184.01 |
| 33 | 26040 | 2026-05-10 | 27754.11 |
| 34 | 26025 | 2026-05-11 | 59991.26 |
| 35 | 26026 | 2026-05-11 | 37796.33 |
| 36 | 26038 | 2026-05-24 | 25966.97 |
| 37 | 26029 | 2026-05-26 | 19912.27 |
| 38 | 26039 | 2026-05-26 | 25000 |
| 39 | 26041 | 2026-06-01 | 65530.8 |
| 40 | 26043 | 2026-06-02 | 41637.92 |
| 41 | 26042 | 2026-06-05 | 37631.02 |
| 42 | 26050 | 2026-06-08 | 21711 |
| 43 | 26044 | 2026-06-09 | 57840.47 |
| 44 | 26035 | 2026-06-13 | 75122.1 |
| 45 | 26048 | 2026-06-22 | 25800.16 |
| 46 | 26036 | 2026-06-23 | 42952.05 |
| 47 | 26039 | 2026-06-25 | 25138.68 |
| 48 | 26049 | 2026-06-25 | 23300 |
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_id | customer | invoice_date | due_date | amount |
|---|---|---|---|---|
| 26001 | Northgate Cycles Ltd | 2026-01-03 | 2026-02-02 | 26959.83 |
| 26002 | Riverside Bike Co. | 2026-01-06 | 2026-02-05 | 14721.94 |
| 26003 | Hillcrest Cycle Shop | 2026-01-08 | 2026-02-07 | 16992.65 |
| 26004 | Pedal Point Inc. | 2026-01-12 | 2026-02-11 | 23417.22 |
| 26005 | Urban Wheels Online | 2026-01-15 | 2026-03-16 | 28124.62 |
| 26006 | Summit Outdoor Supply | 2026-01-17 | 2026-02-16 | 20273.54 |
| 26007 | Coastline Cycles | 2026-01-21 | 2026-02-20 | 12687.37 |
| 26008 | Greenway Bikes | 2026-01-23 | 2026-02-22 | 11730.56 |
| 26009 | Two Wheel Depot | 2026-01-26 | 2026-02-25 | 19622.5 |
| 26010 | Maple Street Cycles | 2026-01-29 | 2026-02-12 | 15772.49 |
| 26011 | Northgate Cycles Ltd | 2026-02-03 | 2026-03-05 | 34472.71 |
| 26012 | Riverside Bike Co. | 2026-02-05 | 2026-03-07 | 21466.08 |
| 26013 | Hillcrest Cycle Shop | 2026-02-09 | 2026-03-11 | 20975.08 |
| 26014 | Pedal Point Inc. | 2026-02-12 | 2026-03-14 | 30082.56 |
| 26015 | Urban Wheels Online | 2026-02-14 | 2026-04-15 | 40785.91 |
| 26016 | Summit Outdoor Supply | 2026-02-18 | 2026-03-20 | 24787.09 |
| 26017 | Coastline Cycles | 2026-02-21 | 2026-03-23 | 16231.53 |
| 26018 | Greenway Bikes | 2026-02-24 | 2026-03-26 | 14692.65 |
| 26019 | Two Wheel Depot | 2026-02-26 | 2026-03-28 | 26205.7 |
| 26020 | Maple Street Cycles | 2026-02-26 | 2026-03-12 | 19640.44 |
| 26021 | Northgate Cycles Ltd | 2026-03-03 | 2026-04-02 | 54429.19 |
| 26022 | Riverside Bike Co. | 2026-03-06 | 2026-04-05 | 32466.62 |
| 26023 | Hillcrest Cycle Shop | 2026-03-08 | 2026-04-07 | 34701.33 |
| 26024 | Pedal Point Inc. | 2026-03-11 | 2026-04-10 | 52082.83 |
| 26025 | Urban Wheels Online | 2026-03-14 | 2026-05-13 | 59991.26 |
| 26026 | Summit Outdoor Supply | 2026-03-17 | 2026-04-16 | 37796.33 |
| 26027 | Coastline Cycles | 2026-03-20 | 2026-04-19 | 24397.37 |
| 26028 | Greenway Bikes | 2026-03-23 | 2026-04-22 | 21785.83 |
| 26029 | Two Wheel Depot | 2026-03-27 | 2026-04-26 | 39812.27 |
| 26030 | Maple Street Cycles | 2026-03-29 | 2026-04-12 | 18452.9 |
| 26031 | Northgate Cycles Ltd | 2026-04-02 | 2026-05-02 | 59320 |
| 26032 | Riverside Bike Co. | 2026-04-06 | 2026-05-06 | 37402.19 |
| 26033 | Hillcrest Cycle Shop | 2026-04-08 | 2026-05-08 | 42329.35 |
| 26034 | Pedal Point Inc. | 2026-04-11 | 2026-05-11 | 61184.01 |
| 26035 | Urban Wheels Online | 2026-04-14 | 2026-06-13 | 75122.1 |
| 26036 | Summit Outdoor Supply | 2026-04-18 | 2026-05-18 | 42952.05 |
| 26037 | Coastline Cycles | 2026-04-20 | 2026-05-20 | 29968.46 |
| 26038 | Greenway Bikes | 2026-04-24 | 2026-05-24 | 25966.97 |
| 26039 | Two Wheel Depot | 2026-04-26 | 2026-05-26 | 50138.68 |
| 26040 | Maple Street Cycles | 2026-04-28 | 2026-05-12 | 27754.11 |
| 26041 | Northgate Cycles Ltd | 2026-05-03 | 2026-06-02 | 65530.8 |
| 26042 | Riverside Bike Co. | 2026-05-06 | 2026-06-05 | 37631.02 |
| 26043 | Hillcrest Cycle Shop | 2026-05-08 | 2026-06-07 | 41637.92 |
| 26044 | Pedal Point Inc. | 2026-05-11 | 2026-06-10 | 57840.47 |
| 26045 | Urban Wheels Online | 2026-05-14 | 2026-07-13 | 68239.27 |
| 26046 | Summit Outdoor Supply | 2026-05-17 | 2026-06-16 | 44576.3 |
| 26047 | Coastline Cycles | 2026-05-21 | 2026-06-20 | 28255.84 |
| 26048 | Greenway Bikes | 2026-05-24 | 2026-06-23 | 25800.16 |
| 26049 | Two Wheel Depot | 2026-05-26 | 2026-06-25 | 46653.8 |
| 26050 | Maple Street Cycles | 2026-05-29 | 2026-06-12 | 21711 |
| 26051 | Northgate Cycles Ltd | 2026-06-03 | 2026-07-03 | 48415.21 |
| 26052 | Riverside Bike Co. | 2026-06-06 | 2026-07-06 | 27191.07 |
| 26053 | Hillcrest Cycle Shop | 2026-06-09 | 2026-07-09 | 32156.21 |
| 26054 | Pedal Point Inc. | 2026-06-12 | 2026-07-12 | 46139.99 |
| 26055 | Urban Wheels Online | 2026-06-30 | 2026-08-29 | 55652 |
| 26056 | Summit Outdoor Supply | 2026-06-17 | 2026-07-17 | 37295.37 |
| 26057 | Coastline Cycles | 2026-06-20 | 2026-07-20 | 26031.62 |
| 26058 | Greenway Bikes | 2026-06-23 | 2026-07-23 | 21187.55 |
| 26059 | Two Wheel Depot | 2026-06-27 | 2026-07-27 | 41200.86 |
| 26060 | Maple Street Cycles | 2026-06-28 | 2026-07-12 | 14292.64 |
payments 48 rows, 4 columns
| payment_id | invoice_id | payment_date | amount |
|---|---|---|---|
| 1 | 26001 | 2026-01-31 | 26959.83 |
| 2 | 26002 | 2026-01-31 | 14721.94 |
| 3 | 26003 | 2026-02-02 | 16992.65 |
| 4 | 26004 | 2026-02-08 | 23417.22 |
| 5 | 26010 | 2026-02-09 | 15772.49 |
| 6 | 26008 | 2026-02-19 | 11730.56 |
| 7 | 26009 | 2026-02-25 | 9800 |
| 8 | 26011 | 2026-03-03 | 34472.71 |
| 9 | 26012 | 2026-03-07 | 21466.08 |
| 10 | 26013 | 2026-03-09 | 20975.08 |
| 11 | 26020 | 2026-03-09 | 19640.44 |
| 12 | 26014 | 2026-03-11 | 30082.56 |
| 13 | 26007 | 2026-03-13 | 12687.37 |
| 14 | 26005 | 2026-03-16 | 28124.62 |
| 15 | 26006 | 2026-03-16 | 20273.54 |
| 16 | 26009 | 2026-03-27 | 9822.5 |
| 17 | 26019 | 2026-03-28 | 13100 |
| 18 | 26021 | 2026-03-28 | 54429.19 |
| 19 | 26022 | 2026-03-30 | 32466.62 |
| 20 | 26023 | 2026-04-05 | 34701.33 |
| 21 | 26024 | 2026-04-05 | 52082.83 |
| 22 | 26030 | 2026-04-09 | 18452.9 |
| 23 | 26015 | 2026-04-10 | 40785.91 |
| 24 | 26017 | 2026-04-17 | 10000 |
| 25 | 26028 | 2026-04-22 | 21785.83 |
| 26 | 26016 | 2026-04-25 | 24787.09 |
| 27 | 26029 | 2026-04-26 | 19900 |
| 28 | 26019 | 2026-04-27 | 13105.7 |
| 29 | 26031 | 2026-05-01 | 59320 |
| 30 | 26032 | 2026-05-05 | 37402.19 |
| 31 | 26033 | 2026-05-05 | 42329.35 |
| 32 | 26034 | 2026-05-06 | 61184.01 |
| 33 | 26040 | 2026-05-10 | 27754.11 |
| 34 | 26025 | 2026-05-11 | 59991.26 |
| 35 | 26026 | 2026-05-11 | 37796.33 |
| 36 | 26038 | 2026-05-24 | 25966.97 |
| 37 | 26029 | 2026-05-26 | 19912.27 |
| 38 | 26039 | 2026-05-26 | 25000 |
| 39 | 26041 | 2026-06-01 | 65530.8 |
| 40 | 26043 | 2026-06-02 | 41637.92 |
| 41 | 26042 | 2026-06-05 | 37631.02 |
| 42 | 26050 | 2026-06-08 | 21711 |
| 43 | 26044 | 2026-06-09 | 57840.47 |
| 44 | 26035 | 2026-06-13 | 75122.1 |
| 45 | 26048 | 2026-06-22 | 25800.16 |
| 46 | 26036 | 2026-06-23 | 42952.05 |
| 47 | 26039 | 2026-06-25 | 25138.68 |
| 48 | 26049 | 2026-06-25 | 23300 |
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_id | customer | invoice_date | due_date | amount |
|---|---|---|---|---|
| 26001 | Northgate Cycles Ltd | 2026-01-03 | 2026-02-02 | 26959.83 |
| 26002 | Riverside Bike Co. | 2026-01-06 | 2026-02-05 | 14721.94 |
| 26003 | Hillcrest Cycle Shop | 2026-01-08 | 2026-02-07 | 16992.65 |
| 26004 | Pedal Point Inc. | 2026-01-12 | 2026-02-11 | 23417.22 |
| 26005 | Urban Wheels Online | 2026-01-15 | 2026-03-16 | 28124.62 |
| 26006 | Summit Outdoor Supply | 2026-01-17 | 2026-02-16 | 20273.54 |
| 26007 | Coastline Cycles | 2026-01-21 | 2026-02-20 | 12687.37 |
| 26008 | Greenway Bikes | 2026-01-23 | 2026-02-22 | 11730.56 |
| 26009 | Two Wheel Depot | 2026-01-26 | 2026-02-25 | 19622.5 |
| 26010 | Maple Street Cycles | 2026-01-29 | 2026-02-12 | 15772.49 |
| 26011 | Northgate Cycles Ltd | 2026-02-03 | 2026-03-05 | 34472.71 |
| 26012 | Riverside Bike Co. | 2026-02-05 | 2026-03-07 | 21466.08 |
| 26013 | Hillcrest Cycle Shop | 2026-02-09 | 2026-03-11 | 20975.08 |
| 26014 | Pedal Point Inc. | 2026-02-12 | 2026-03-14 | 30082.56 |
| 26015 | Urban Wheels Online | 2026-02-14 | 2026-04-15 | 40785.91 |
| 26016 | Summit Outdoor Supply | 2026-02-18 | 2026-03-20 | 24787.09 |
| 26017 | Coastline Cycles | 2026-02-21 | 2026-03-23 | 16231.53 |
| 26018 | Greenway Bikes | 2026-02-24 | 2026-03-26 | 14692.65 |
| 26019 | Two Wheel Depot | 2026-02-26 | 2026-03-28 | 26205.7 |
| 26020 | Maple Street Cycles | 2026-02-26 | 2026-03-12 | 19640.44 |
| 26021 | Northgate Cycles Ltd | 2026-03-03 | 2026-04-02 | 54429.19 |
| 26022 | Riverside Bike Co. | 2026-03-06 | 2026-04-05 | 32466.62 |
| 26023 | Hillcrest Cycle Shop | 2026-03-08 | 2026-04-07 | 34701.33 |
| 26024 | Pedal Point Inc. | 2026-03-11 | 2026-04-10 | 52082.83 |
| 26025 | Urban Wheels Online | 2026-03-14 | 2026-05-13 | 59991.26 |
| 26026 | Summit Outdoor Supply | 2026-03-17 | 2026-04-16 | 37796.33 |
| 26027 | Coastline Cycles | 2026-03-20 | 2026-04-19 | 24397.37 |
| 26028 | Greenway Bikes | 2026-03-23 | 2026-04-22 | 21785.83 |
| 26029 | Two Wheel Depot | 2026-03-27 | 2026-04-26 | 39812.27 |
| 26030 | Maple Street Cycles | 2026-03-29 | 2026-04-12 | 18452.9 |
| 26031 | Northgate Cycles Ltd | 2026-04-02 | 2026-05-02 | 59320 |
| 26032 | Riverside Bike Co. | 2026-04-06 | 2026-05-06 | 37402.19 |
| 26033 | Hillcrest Cycle Shop | 2026-04-08 | 2026-05-08 | 42329.35 |
| 26034 | Pedal Point Inc. | 2026-04-11 | 2026-05-11 | 61184.01 |
| 26035 | Urban Wheels Online | 2026-04-14 | 2026-06-13 | 75122.1 |
| 26036 | Summit Outdoor Supply | 2026-04-18 | 2026-05-18 | 42952.05 |
| 26037 | Coastline Cycles | 2026-04-20 | 2026-05-20 | 29968.46 |
| 26038 | Greenway Bikes | 2026-04-24 | 2026-05-24 | 25966.97 |
| 26039 | Two Wheel Depot | 2026-04-26 | 2026-05-26 | 50138.68 |
| 26040 | Maple Street Cycles | 2026-04-28 | 2026-05-12 | 27754.11 |
| 26041 | Northgate Cycles Ltd | 2026-05-03 | 2026-06-02 | 65530.8 |
| 26042 | Riverside Bike Co. | 2026-05-06 | 2026-06-05 | 37631.02 |
| 26043 | Hillcrest Cycle Shop | 2026-05-08 | 2026-06-07 | 41637.92 |
| 26044 | Pedal Point Inc. | 2026-05-11 | 2026-06-10 | 57840.47 |
| 26045 | Urban Wheels Online | 2026-05-14 | 2026-07-13 | 68239.27 |
| 26046 | Summit Outdoor Supply | 2026-05-17 | 2026-06-16 | 44576.3 |
| 26047 | Coastline Cycles | 2026-05-21 | 2026-06-20 | 28255.84 |
| 26048 | Greenway Bikes | 2026-05-24 | 2026-06-23 | 25800.16 |
| 26049 | Two Wheel Depot | 2026-05-26 | 2026-06-25 | 46653.8 |
| 26050 | Maple Street Cycles | 2026-05-29 | 2026-06-12 | 21711 |
| 26051 | Northgate Cycles Ltd | 2026-06-03 | 2026-07-03 | 48415.21 |
| 26052 | Riverside Bike Co. | 2026-06-06 | 2026-07-06 | 27191.07 |
| 26053 | Hillcrest Cycle Shop | 2026-06-09 | 2026-07-09 | 32156.21 |
| 26054 | Pedal Point Inc. | 2026-06-12 | 2026-07-12 | 46139.99 |
| 26055 | Urban Wheels Online | 2026-06-30 | 2026-08-29 | 55652 |
| 26056 | Summit Outdoor Supply | 2026-06-17 | 2026-07-17 | 37295.37 |
| 26057 | Coastline Cycles | 2026-06-20 | 2026-07-20 | 26031.62 |
| 26058 | Greenway Bikes | 2026-06-23 | 2026-07-23 | 21187.55 |
| 26059 | Two Wheel Depot | 2026-06-27 | 2026-07-27 | 41200.86 |
| 26060 | Maple Street Cycles | 2026-06-28 | 2026-07-12 | 14292.64 |
payments 48 rows, 4 columns
| payment_id | invoice_id | payment_date | amount |
|---|---|---|---|
| 1 | 26001 | 2026-01-31 | 26959.83 |
| 2 | 26002 | 2026-01-31 | 14721.94 |
| 3 | 26003 | 2026-02-02 | 16992.65 |
| 4 | 26004 | 2026-02-08 | 23417.22 |
| 5 | 26010 | 2026-02-09 | 15772.49 |
| 6 | 26008 | 2026-02-19 | 11730.56 |
| 7 | 26009 | 2026-02-25 | 9800 |
| 8 | 26011 | 2026-03-03 | 34472.71 |
| 9 | 26012 | 2026-03-07 | 21466.08 |
| 10 | 26013 | 2026-03-09 | 20975.08 |
| 11 | 26020 | 2026-03-09 | 19640.44 |
| 12 | 26014 | 2026-03-11 | 30082.56 |
| 13 | 26007 | 2026-03-13 | 12687.37 |
| 14 | 26005 | 2026-03-16 | 28124.62 |
| 15 | 26006 | 2026-03-16 | 20273.54 |
| 16 | 26009 | 2026-03-27 | 9822.5 |
| 17 | 26019 | 2026-03-28 | 13100 |
| 18 | 26021 | 2026-03-28 | 54429.19 |
| 19 | 26022 | 2026-03-30 | 32466.62 |
| 20 | 26023 | 2026-04-05 | 34701.33 |
| 21 | 26024 | 2026-04-05 | 52082.83 |
| 22 | 26030 | 2026-04-09 | 18452.9 |
| 23 | 26015 | 2026-04-10 | 40785.91 |
| 24 | 26017 | 2026-04-17 | 10000 |
| 25 | 26028 | 2026-04-22 | 21785.83 |
| 26 | 26016 | 2026-04-25 | 24787.09 |
| 27 | 26029 | 2026-04-26 | 19900 |
| 28 | 26019 | 2026-04-27 | 13105.7 |
| 29 | 26031 | 2026-05-01 | 59320 |
| 30 | 26032 | 2026-05-05 | 37402.19 |
| 31 | 26033 | 2026-05-05 | 42329.35 |
| 32 | 26034 | 2026-05-06 | 61184.01 |
| 33 | 26040 | 2026-05-10 | 27754.11 |
| 34 | 26025 | 2026-05-11 | 59991.26 |
| 35 | 26026 | 2026-05-11 | 37796.33 |
| 36 | 26038 | 2026-05-24 | 25966.97 |
| 37 | 26029 | 2026-05-26 | 19912.27 |
| 38 | 26039 | 2026-05-26 | 25000 |
| 39 | 26041 | 2026-06-01 | 65530.8 |
| 40 | 26043 | 2026-06-02 | 41637.92 |
| 41 | 26042 | 2026-06-05 | 37631.02 |
| 42 | 26050 | 2026-06-08 | 21711 |
| 43 | 26044 | 2026-06-09 | 57840.47 |
| 44 | 26035 | 2026-06-13 | 75122.1 |
| 45 | 26048 | 2026-06-22 | 25800.16 |
| 46 | 26036 | 2026-06-23 | 42952.05 |
| 47 | 26039 | 2026-06-25 | 25138.68 |
| 48 | 26049 | 2026-06-25 | 23300 |
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:
| Database | Days past due | Note |
|---|---|---|
| MySQL | DATEDIFF('2026-06-30', due_date) | first argument minus second, in days; times are ignored |
| PostgreSQL | DATE '2026-06-30' - due_date | date minus date gives the number of days as an integer (column of type date) |
| SQL Server | DATEDIFF(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.
Back to the course overview.