Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Understanding Ledgers and Joins | Ledger-Style Joins
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
SQL for Finance and Accounting

bookUnderstanding Ledgers and Joins

When working in finance and accounting, you often need to track the movement of money between various accounts over time. These movements are recorded in ledgers, which are essentially records of all transactions affecting each account. In a database, you typically have separate tables for accounts and entries. The accounts table lists each account, while the entries table records every debit or credit, along with related details such as the date, amount, and account affected. To analyze account activityβ€”such as listing all transactions for each accountβ€”you need to combine information from both tables. This is where joins in SQL become essential, allowing you to bring together related data and reconcile account activity efficiently.

123456789
SELECT accounts.account_name, entries.entry_date, entries.amount, entries.entry_type FROM accounts INNER JOIN entries ON accounts.id = entries.account_id ORDER BY accounts.account_name, entries.entry_date;
copy

The query above uses an INNER JOIN to combine the accounts and entries tables. This join matches each account to its corresponding entries, providing a complete list of transactions for every account that has at least one entry. INNER JOIN only includes rows where there is a match in both tables, so accounts without any transactions will not appear in the result. This approach is useful when you want to focus solely on accounts with recorded activity and ignore inactive accounts.

123456789
SELECT accounts.account_name, entries.entry_date, entries.amount, entries.entry_type FROM accounts LEFT JOIN entries ON accounts.id = entries.account_id ORDER BY accounts.account_name, entries.entry_date;
copy

A LEFT JOIN works differently. It returns all rows from the accounts table, along with any matching entries from the entries table. If an account has no corresponding entries, the columns from the entries table will contain NULL values. This is especially helpful when you want a full list of accounts, regardless of whether they have transactions, such as when checking for inactive accounts or ensuring all accounts are being used as expected.

The key difference between INNER JOIN and LEFT JOIN in the context of ledgers is what happens when an account has no entries. INNER JOIN will exclude these accounts from the results, while LEFT JOIN will include them, showing NULL for the entry details. This distinction is crucial when reconciling accounts or preparing comprehensive reports.

Note
Note

A debit increases asset or expense accounts and decreases liability, equity, or revenue accounts. A credit does the opposite: it increases liability, equity, or revenue accounts and decreases asset or expense accounts.

1. What is the purpose of joining accounts and entries tables in a ledger?

2. What is the difference between INNER JOIN and LEFT JOIN?

3. Why might you use a LEFT JOIN when analyzing accounts?

question mark

What is the purpose of joining accounts and entries tables in a ledger?

Select the correct answer

question mark

What is the difference between INNER JOIN and LEFT JOIN?

Select the correct answer

question mark

Why might you use a LEFT JOIN when analyzing accounts?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 3. ChapterΒ 1

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

bookUnderstanding Ledgers and Joins

Swipe to show menu

When working in finance and accounting, you often need to track the movement of money between various accounts over time. These movements are recorded in ledgers, which are essentially records of all transactions affecting each account. In a database, you typically have separate tables for accounts and entries. The accounts table lists each account, while the entries table records every debit or credit, along with related details such as the date, amount, and account affected. To analyze account activityβ€”such as listing all transactions for each accountβ€”you need to combine information from both tables. This is where joins in SQL become essential, allowing you to bring together related data and reconcile account activity efficiently.

123456789
SELECT accounts.account_name, entries.entry_date, entries.amount, entries.entry_type FROM accounts INNER JOIN entries ON accounts.id = entries.account_id ORDER BY accounts.account_name, entries.entry_date;
copy

The query above uses an INNER JOIN to combine the accounts and entries tables. This join matches each account to its corresponding entries, providing a complete list of transactions for every account that has at least one entry. INNER JOIN only includes rows where there is a match in both tables, so accounts without any transactions will not appear in the result. This approach is useful when you want to focus solely on accounts with recorded activity and ignore inactive accounts.

123456789
SELECT accounts.account_name, entries.entry_date, entries.amount, entries.entry_type FROM accounts LEFT JOIN entries ON accounts.id = entries.account_id ORDER BY accounts.account_name, entries.entry_date;
copy

A LEFT JOIN works differently. It returns all rows from the accounts table, along with any matching entries from the entries table. If an account has no corresponding entries, the columns from the entries table will contain NULL values. This is especially helpful when you want a full list of accounts, regardless of whether they have transactions, such as when checking for inactive accounts or ensuring all accounts are being used as expected.

The key difference between INNER JOIN and LEFT JOIN in the context of ledgers is what happens when an account has no entries. INNER JOIN will exclude these accounts from the results, while LEFT JOIN will include them, showing NULL for the entry details. This distinction is crucial when reconciling accounts or preparing comprehensive reports.

Note
Note

A debit increases asset or expense accounts and decreases liability, equity, or revenue accounts. A credit does the opposite: it increases liability, equity, or revenue accounts and decreases asset or expense accounts.

1. What is the purpose of joining accounts and entries tables in a ledger?

2. What is the difference between INNER JOIN and LEFT JOIN?

3. Why might you use a LEFT JOIN when analyzing accounts?

question mark

What is the purpose of joining accounts and entries tables in a ledger?

Select the correct answer

question mark

What is the difference between INNER JOIN and LEFT JOIN?

Select the correct answer

question mark

Why might you use a LEFT JOIN when analyzing accounts?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 3. ChapterΒ 1
some-alt