Masar-Fintech

Worked exampleData

SQL for finance: the first query against a ledger table

Your first query against a ledger table should return a number you can already check in a report; here it sums October by account to EGP 465,000, the same as the trial balance.

  • Written by Mahmoud Sabry
  • Published
  • Last updated
  • Reading time 5 min read
  • Checks Check lines recalculated by script Editorial policy

The problem: the same export every month

An accountant at a manufacturing company in Egypt exports the general ledger to a spreadsheet every month, deletes the rows for other months and builds the same totals by hand. IT offers read-only access to a reporting copy of the ledger. The question is where to start.

Start with a first query whose answer you already know. If it matches, you have learned the table and proved your query in one step.

The ledger table

In the reporting copy, the ledger lines sit in a table called GL. Each row is one line, with three columns this query needs: period, a number such as 202610 for October 2026 or 202609 for September; account, a number such as 6100; and amount, in EGP, with expenses positive.

Table 1

8 ledger lines across 2 periods and 3 expense accounts

The GL table, the columns this query uses, EGP, hypothetical company

A table of eight lines. Period 202609: account 6100, 87,500; account 6200, 60,000; account 6300, 225,000. Period 202610: account 6100, 92,500; account 6100, 87,500; account 6200, 60,000; account 6300, 180,000; account 6300, 45,000.
periodaccountamount
202609610087,500
202609620060,000
2026096300225,000
202610610092,500
202610610087,500
202610620060,000
2026106300180,000
202610630045,000

Source: Illustrative data.

Account 6100 is fuel, 6200 is rent and 6300 is salaries. A real table has more columns and many thousands of rows, but the query works the same way.

Your first query, line by line

SELECT account, SUM(amount) FROM GL WHERE period = 202610 GROUP BY account ORDER BY account

Read it in the order the database runs it. FROM GL names the table. WHERE period = 202610 keeps only October's lines. GROUP BY account puts the lines for each account together. SELECT account, SUM(amount) returns each account with the total of its lines. ORDER BY account sorts the result.

Table 2

The first query returns 3 accounts totalling 465,000, the same as the trial balance

Result of the query for period 202610, EGP

A table: account 6100, 180,000; account 6200, 60,000; account 6300, 225,000; total 465,000.
accountSUM(amount)
6100180,000
620060,000
6300225,000
Total465,000

Source: Illustrative calculation from hypothetical data.

Each total can be recomputed from the table: fuel is the two October lines, 92,500 + 87,500 = 180,000, and salaries are 180,000 + 45,000 = 225,000. The October trial balance shows the same three balances and the same 465,000.

The query is right, but it returns what the ledger holds: 87,500 of that fuel is the September accrual nobody reversed, the same double count a pivot table finds in these lines. A query reports the ledger; it does not fix it.

The error to expect: no period filter

Exhibit 1

Without the period filter the query returns 837,500, two months added together

The same query without and with WHERE period = 202610, EGP

Before: no period filter

AB
1accountSUM(amount)
26100267,500
36200120,000
46300450,000
5Total837,500

After: October only

AB
1accountSUM(amount)
26100180,000
3620060,000
46300225,000
5Total465,000

Source: Illustrative calculation from hypothetical data.

View data table
Before: no period filter 26100 | 267,500
Before: no period filter 36200 | 120,000
Before: no period filter 46300 | 450,000
Before: no period filter 5Total | 837,500
After: October only 26100 | 180,000
After: October only 36200 | 60,000
After: October only 46300 | 225,000
After: October only 5Total | 465,000

Leave out the WHERE line and the query adds September and October together: 465,000 + 372,500 = 837,500. Nothing warns you. The total looks like a busy month. Only the tie to the trial balance shows it is wrong, which is why the first query is always one you can check.

Working safely with ledger data

Use the read-only reporting copy, never the live accounting system. Keep queries in a text file with a comment saying what each one returns and which report it ties to. And treat the results as confidential, exactly like the ledger itself.

Common errors

  • No period filter: the query adds every month and returns a plausible total. Correction: always filter the period with WHERE.
  • Trusting a first query you cannot check: a wrong join or filter goes unnoticed. Correction: start with a total that is already in a report.
  • Querying the live system: a slow query can hold up everyone's work. Correction: use the read-only reporting copy.

How to check your work

  1. Confirm the October total: 180000 + 60000 + 225000 = 465000.
  2. Confirm it ties to the trial balance, account by account.
  3. Confirm the query without the filter adds both months: 465000 + 372500 = 837500.

Limits of the method

This query reads one table with clean numbers. Real ledgers have reversed entries, credits stored as negatives and accounts mapped to report lines in other tables; joins to those tables are the next step. A query that ties today can drift if the table's rules change, so keep the tie-out with the query.

Frequently asked questions

Do I need to install anything?

Usually only a query tool your IT team approves and read-only access to a reporting copy.

Why does GROUP BY come after WHERE?

The database filters the lines first and groups what is left, so the filter decides which lines are totalled.

Should SQL replace my spreadsheet?

No. Let SQL fetch and total the lines, and use the spreadsheet for analysis and presentation.

Terms in this article

EnglishArabicMeaning
QueryالاستعلامAn instruction that asks a database to return specific rows or totals
General ledgerالأستاذ العامThe full record of every accounting entry, by account
Trial balanceميزان المراجعةThe list of account balances that must total to zero across debits and credits

Related lessons

Read next

Sources and notes

Figures and companies in the examples are illustrative and hypothetical, and do not refer to any real organisation.

Amounts are in EGP; the ledger table and account numbers are hypothetical.

Educational content, not financial, tax or investment advice. Check the standards and rules in force before applying anything.

Share this article

Your first SQL query against the ledger: one month, totalled by account, tied to the trial balance. An EGP example:

Share on WhatsApp Share on LinkedIn