Español Practice SQL

SQL for finance teams: the queries you will actually write

Finance requests repeat. Learn five query shapes and most of the questions your controller sends become ten minute jobs.

1. The filtered list

“Send me every overdue invoice over $5,000.” This is SELECT with WHERE and two conditions joined by AND.

SELECT invoice_id, customer, amount, due_date
FROM invoices
WHERE status = 'Overdue' AND amount > 5000
ORDER BY due_date

2. The total per group

“What is outstanding per region?” GROUP BY with SUM. Add HAVING when only the big groups matter.

SELECT region, SUM(amount) AS outstanding
FROM invoices
WHERE status <> 'Paid'
GROUP BY region
ORDER BY outstanding DESC

3. The join to customer data

Invoices usually store a customer ID; names, regions and account managers live in another table. JOIN brings them together, and LEFT JOIN finds customers with nothing invoiced.

4. Aging buckets

CASE turns days overdue into the buckets every AR report uses.

SELECT
  CASE WHEN days_overdue > 90 THEN '90+'
       WHEN days_overdue > 60 THEN '61 to 90'
       WHEN days_overdue > 30 THEN '31 to 60'
       ELSE '0 to 30' END AS bucket,
  SUM(amount) AS total
FROM ar_open
GROUP BY 1

5. Month over month

Group by month, then compare each month with the previous one using LAG. That one query replaces a lot of spreadsheet formulas.

All five shapes are in the practice with the Finance role: the tasks use invoice tables and read like messages from a controller or an AR manager.

Practice what you just read

Open the practice

Guides for this article

More articles