Lesson 60/100

Tutorials SQL Server Tutorial

Banking Transaction Systems — Complete Guide

Banking Transaction Systems — Complete Guide: free step-by-step lesson with examples, common mistakes, and interview tips — part of SQL Server Tutorial on Toolliyo Academy.

On this page

SQL Server Tutorial · Lesson 60 of 100

Banking Transaction Systems

SQL basics ✓QueriesAdvanced

Queries · 2 — JOINs · ~10 min · SQL — Transactions & Concurrency

What is this?

Banking transfers combine ACID transactions, ordered locking, audit inserts, and balance CHECKs so money never appears or disappears.

Why should you care?

This is the reference workload for SQL Server concurrency — get it right once and reuse the pattern.

See it live — copy this example

Run in SQL Server Management Studio (SSMS) or Azure Data Studio.

USE DataVerse;
CREATE OR ALTER PROCEDURE dbo.usp_TransferFunds
    @FromId INT, @ToId INT, @Amount DECIMAL(18,2)
AS
BEGIN
    SET NOCOUNT ON; SET XACT_ABORT ON;
    IF @Amount <= 0 THROW 50001, 'Amount must be positive.', 1;
    IF @FromId = @ToId THROW 50002, 'Accounts must differ.', 1;
    DECLARE @First INT = IIF(@FromId < @ToId, @FromId, @ToId);
    DECLARE @Second INT = IIF(@FromId < @ToId, @ToId, @FromId);
    BEGIN TRY
        BEGIN TRAN;
        UPDATE dbo.Accounts WITH (UPDLOCK, ROWLOCK) SET Balance = Balance WHERE AccountId IN (@First,@Second);
        UPDATE dbo.Accounts SET Balance = Balance - @Amount WHERE AccountId = @FromId AND Balance >= @Amount;
        IF @@ROWCOUNT <> 1 THROW 50003, 'Insufficient funds or missing account.', 1;
        UPDATE dbo.Accounts SET Balance = Balance + @Amount WHERE AccountId = @ToId;
        IF @@ROWCOUNT <> 1 THROW 50004, 'Destination account missing.', 1;
        COMMIT;
    END TRY
    BEGIN CATCH
        IF @@TRANCOUNT > 0 ROLLBACK;
        THROW;
    END CATCH
END

What happened?

  • Validates input, locks accounts in order, debits only if enough balance, credits destination, rolls back on any failure.
  • Extend with an audit insert in the same tran for production.

Practice next

  1. Ensure Accounts CHECK (Balance >= 0).
  2. Create and test usp_TransferFunds.
  3. Try overdraft and confirm THROW/rollback.
  4. Return new balances as a result set.
  5. Add idempotency key column for retries.

Remember

Validate → lock ordered → debit → credit → audit → commit. Rowcount checks catch missing rows. Rollback on any failure.

DataVerse wallet transfer

UPI-like wallet moves INR between accounts with this proc pattern.

Outcome: Zero silent balance corruption in soak tests.

Interview prep for this lesson

Practice these questions aloud after reading—each links to a full structured answer.

Mid PDF Detailed
Partition Tolerance: The system continues to operate despite network partitions. Trade-offs: ● CA (Consistency and Availability): Systems that prioritize consistency and
Short answer: vailability will fail during network partitions. CP (Consistency and Partition Tolerance): Systems that prioritize consistency and partition tolerance may not be available during network issues. AP (Availab…
Mid PDF Detailed
Transaction Log Backup (for databases that support it, like SQL Server):?
Short answer: A transaction log backup records all the changes made to the database since the last transaction log backup. Explain a bit more It allows point-in-time recovery. Advantages: Enables recovery of the database…
Mid PDF Detailed
Database Metrics: ○ Track buffer cache hit ratio, transaction log size, locks and deadlocks,?
Short answer: And cache usage to monitor the internal database performance. Real-world example (ShopNest) Checkout wraps stock decrement + order insert in a transaction so you never sell stock you do not have. Say this i…
Mid PDF Detailed
What are the differences between SQL and NoSQL databases?
Short answer: SQL Databases (Relational Databases): These are structured databases that use Structured Query Language (SQL) for defining and manipulating data. Explain a bit more They store data in tables with rows and c…
Mid PDF Detailed
Query Performance:?
Short answer: Use EXPLAIN or QUERY PLAN to analyze query execution times and identify slow queries. Track metrics like response time, execution time, and query throughput. Real-world example (ShopNest) ShopNest adds an i…
Questions on this lesson 0

Sign in to ask a question or upvote helpful answers.

No questions yet — be the first to ask!

SQL Server Tutorial
Course syllabus

SQL Server Tutorial

SQL — Foundations
SQL — SQL Queries & Clauses
SQL — Joins & Relationships
SQL — Indexing & Performance
SQL — Stored Procedures & Functions
SQL — Transactions & Concurrency
SQL — Advanced SQL Server
SQL — Security & High Availability
SQL — 2022 & Cloud
SQL — Real-World Projects
Toolliyo Assistant
Ask about tutorials, ebooks, training, pricing, mentor services, and support. I use public site content only—not admin or internal tools.

care@toolliyo.com

Need callback? Share your details