Downloads

SQL recipes for accounting and finance

Ready-to-run SQL for month-end close, receivables, payables, cash, margin, budgets, controls, payroll, nonprofit and inventory, on live sample data.

Testedon live dataeach recipe ran on the sample databases

How it works

  1. Search or filter

    Find a finance question by word or topic.

  2. Read the recipe

    Each one explains the question, how to read the answer and what to watch for.

  3. Copy the SQL

    Run it in the playground or on your own database.

How the recipes work

Each recipe answers a finance or accounting question with SQL against one of the free sample databases on LearnSQL. Press Run it and the query executes live, read-only, so the result is the current data in the sample rather than a screenshot.

Every recipe has two versions, one for SQL Server and one for MariaDB (MySQL). They ask the same question and return the same kind of answer. Copy the version that matches your database system. The SQL Server version names each table with its schema, such as dbo.invoices, or with the database name where a sample uses that layout, such as classicmodels.orders. The MariaDB version uses plain table names because the database is selected on the connection.

How to read a recipe

Start with the business question. The "What to look for" note tells you how to read the output. The "Watch out for" list names the mistakes people make with that kind of question: which rows are left out, which dates are used, and where the totals can mislead.

The sample dates move forward every day, so anything described as "as of today" or "last twelve months" changes when you run it again. The recipes are written to stay correct as the dates move. The text never quotes a number from a run, so check each result yourself.

Using a recipe at work

Treat a recipe as a starting point. Replace the sample table and column names with your own, check the join keys and sign conventions in your system, and tie the total to a report you already trust before you use it for a decision. A trial balance whose debits and credits do not agree, or an aging that does not tie to the receivable account, usually means a join or a filter is wrong.

This page shows at most 200 rows of each result.

Frequently asked questions

Can I change the SQL and run my own version?

Yes. Copy the SQL into the SQL tool you already use and connect with the sample database settings. The sample logins are read-only, so an edit cannot change the data. For a guided version, try the visual SQL builder.

Why are there two SQL versions?

SQL Server and MariaDB use different functions for dates, for limiting rows and for joining text. Each version uses the native function. For example, SQL Server writes DATEDIFF with the unit first, and it limits rows with TOP while MariaDB uses LIMIT at the end of the query.

Why does a recipe return fewer rows than I expected?

Many recipes keep only the top rows, or they leave out cancelled, written-off or future-dated items. The watch-out notes list the filters that matter for each question.

Is the data real?

No. The sample databases are practice data. Use them to learn the patterns, then apply the same patterns to your own records.

More tools: all tools · function reference · guides