Artificial Intelligence, Data Analytics and Cyber Security - Laws and Practice · Database Management
SQL and Database Transactions: Commands, Joins and ACID
Updated 11 October 2026 · Fact-checked
SQL is the standard language used to define, change, query and control data in a relational database. A transaction is a group of SQL operations treated as one unit. It must follow ACID: atomicity, consistency, isolation and durability. Concurrency control and recovery protect these properties.
Understand SQL and Database Transactions
A database stores data in tables made of rows and columns. SQL (Structured Query Language) is how you talk to it. You use SQL to create tables, insert and change rows, fetch data and decide who can see what.
SQL commands fall into groups. DDL (Data Definition Language) defines structure: CREATE, ALTER, DROP, TRUNCATE. DML (Data Manipulation Language) changes data: INSERT, UPDATE, DELETE. DQL is the query part: SELECT. DCL (Data Control Language) controls access: GRANT, REVOKE. TCL (Transaction Control Language) manages transactions: COMMIT, ROLLBACK, SAVEPOINT. Some books place SELECT under DML, so state the grouping you follow.
A join combines rows from two or more tables using a related column, usually a primary key matched to a foreign key. An INNER JOIN returns only matching rows. A LEFT JOIN returns all rows of the left table plus matches from the right, with NULL where there is no match. A RIGHT JOIN does the reverse. A FULL JOIN returns all rows from both. A CROSS JOIN pairs every row of one table with every row of the other.
A transaction is a logical unit of work. Think of a bank transfer: debit one account and credit another. Both must happen or neither. The ACID properties guarantee this. Atomicity: all or nothing. Consistency: the database moves from one valid state to another, respecting its rules. Isolation: concurrent transactions do not interfere with one another. Durability: once committed, changes survive a crash.
When many users work at once, problems arise: lost updates, dirty reads (reading uncommitted data), non-repeatable reads and phantom rows. Concurrency control prevents them, mainly through locking, timestamps and multiversion methods. Recovery restores the database after failure using logs, checkpoints, rollback and redo. For a law-and-practice paper, link these ideas to data integrity, audit trails and the duty to keep data secure.
Key rules to remember
- Basic query structure
- SELECT columns FROM table WHERE condition GROUP BY column HAVING condition ORDER BY column
- WHERE filters rows before grouping. HAVING filters groups after aggregation.
- Insert, update, delete
- INSERT INTO t (c1, c2) VALUES (v1, v2); UPDATE t SET c1 = v WHERE condition; DELETE FROM t WHERE condition;
- Without WHERE, UPDATE and DELETE affect every row.
- Inner join
- SELECT … FROM A INNER JOIN B ON A.key = B.key
- Returns only rows that match in both tables.
- Outer joins
- LEFT JOIN: all of A + matches in B; RIGHT JOIN: all of B + matches in A; FULL JOIN: all of both
- Unmatched side shows NULL.
- ACID
- Atomicity, Consistency, Isolation, Durability
- Learn each with a one-line meaning and a bank-transfer example.
- Transaction control
- COMMIT saves changes; ROLLBACK undoes uncommitted changes; SAVEPOINT marks a point to roll back to
- These are TCL commands.
- Two-phase locking (2PL)
- Growing phase: acquire locks only; shrinking phase: release locks only
- Guarantees serialisable schedules. It can still cause deadlock.
- Serialisability
- A concurrent schedule is acceptable if its result equals that of some serial execution
- This is the correctness test for concurrency control.
- Deadlock
- Transactions wait for each other's locks in a cycle
- Handled by prevention, detection (wait-for graph) or timeout, then rolling back a victim.
How to solve SQL and Database Transactions questions
Exam questions in this paper are descriptive. They ask you to explain a concept, write or read a query, or apply a principle to a short scenario. Use this order.
- 1Identify the type of question: define or explain, write a query, compare, or apply to facts.
- 2For a definition, give the meaning first, then the components or types, then a short practical example.
- 3For a query, list the tables and the common column. Decide the clause order: SELECT, FROM, JOIN, WHERE, GROUP BY, HAVING, ORDER BY.
- 4Choose the join by asking which rows must appear: only matches (INNER) or all rows from one side (LEFT or RIGHT).
- 5For a transaction scenario, name the ACID property that is at risk and say why.
- 6For concurrency or recovery, name the problem (lost update, dirty read, deadlock, crash) and then the mechanism that solves it (locks, timestamps, logs, rollback, redo).
- 7Close with a conclusion tied to the facts, such as data integrity, reliability or security obligations of the company.
Quickest way: Four-line answer pattern
When to use it: Use when you have about five minutes for a theory question on SQL, ACID or concurrency.
- Line 1: define the term in one sentence.
- Line 2: list the parts (the five SQL command groups, the four ACID properties, the join types).
- Line 3: give one small example, such as a fund transfer of ₹10,000 between two accounts.
- Line 4: state why it matters: accuracy, integrity, recovery or compliance.
Common mistakes in SQL and Database Transactions
Confusing DELETE, TRUNCATE and DROP.
All three seem to remove things.
Fix: DELETE removes selected rows and is DML. TRUNCATE removes all rows but keeps the table structure. DROP removes the table itself. TRUNCATE and DROP are DDL.
Using WHERE to filter on aggregate results.
Students treat WHERE and HAVING as the same.
Fix: WHERE filters individual rows before grouping. Use HAVING for conditions such as COUNT(*) > 5 or SUM(amount) > 1,00,000.
Writing an UPDATE or DELETE without WHERE.
Students focus on the change and forget the condition.
Fix: Always write the WHERE clause first. State in your answer that omitting it affects all rows.
Mixing up atomicity and consistency.
Both relate to a transfer completing correctly.
Fix: Atomicity is about all steps happening or none. Consistency is about the database obeying its rules and constraints before and after.
Mixing up isolation and durability.
Both sound like protection of data.
Fix: Isolation concerns concurrent transactions not seeing each other's partial work. Durability concerns committed data surviving a failure.
Saying locking removes deadlock.
Locking is taught as the solution to concurrency problems.
Fix: Locking, especially 2PL, ensures serialisability but can create deadlock. Add that deadlock is handled by prevention, detection or timeout and rollback of one transaction.
Worked examples
Example 1
Table Customer(CustID, Name) has rows (1, Asha), (2, Ravi), (3, Meena). Table Orders(OrderID, CustID, Amount) has rows (101, 1, ₹5,000), (102, 1, ₹3,000), (103, 3, ₹7,000). Show the result of an INNER JOIN and a LEFT JOIN of Customer with Orders on CustID.
Show the solution
- Write the join condition: Customer.CustID = Orders.CustID.
- INNER JOIN keeps only matching pairs. Asha matches orders 101 and 102. Meena matches order 103. Ravi has no order, so he is dropped.
- INNER JOIN result has 3 rows: (Asha, 101, ₹5,000), (Asha, 102, ₹3,000), (Meena, 103, ₹7,000).
- LEFT JOIN keeps every row of Customer, the left table. It contains the same 3 matched rows.
- Ravi has no match, so he is added with NULL in the order columns: (Ravi, NULL, NULL).
- LEFT JOIN result has 4 rows.
Answer: INNER JOIN returns 3 rows (two for Asha, one for Meena). LEFT JOIN returns 4 rows, the same 3 plus Ravi with NULL order details.
Example 2
Rohit transfers ₹10,000 from Account A (balance ₹50,000) to Account B (balance ₹20,000). The system crashes after A is debited but before B is credited. Which ACID property is at risk, and how should the DBMS recover?
Show the solution
- Write the transaction: debit A by ₹10,000, credit B by ₹10,000, then commit.
- Before the crash the total across both accounts is ₹70,000. After the debit alone, A shows ₹40,000 and B shows ₹20,000, a total of ₹60,000. ₹10,000 has vanished.
- The transaction did not complete all its steps. This breaks atomicity, which requires all or nothing. Consistency is also affected, since the total no longer matches.
- The transaction had not committed. On restart, the DBMS reads its log and finds this transaction unfinished.
- Recovery undoes (rolls back) the debit using the log. A returns to ₹50,000 and B stays ₹20,000.
- If a transaction had committed before the crash, the DBMS would instead redo its changes from the log. This is durability.
Answer: Atomicity is violated, with consistency affected. The DBMS rolls back the uncommitted debit using its log, restoring A to ₹50,000 and B to ₹20,000. Committed work is redone, never lost.
Exam tips
- Learn the five command groups (DDL, DML, DQL, DCL, TCL) with two or three commands each. Examiners often ask for classification with examples.
- For ACID, always add a one-line example for each property. A bare list earns fewer marks.
- Practise reading small queries and stating the output, especially INNER versus LEFT joins.
- In concurrency answers, name the problem first, then the mechanism. Mention locks, timestamps, deadlock and serialisability.
- Link technical points to compliance where the question allows: data integrity, audit trails, backups and security of records.
Practice questions from Database Management
- A fintech firm in Pune executes the following in sequence: INSERT of a new loan record, then SAVEPOINT s1, then UPDATE of the interest rate,…
- A firm's database administrator runs a DELETE statement without a WHERE clause inside a transaction that has not been committed, then realis…
- A fintech company mines its big-data store of customer records to profile individual behaviour and sells the profiles to advertisers. The re…
- In an ER design for a listed company's compliance system, 'Dependent' (family members of an employee) has no key of its own and is identifie…
- In an Employee table, Emp_ID is the primary key, and columns are Dept_ID and Dept_Head, where Dept_ID determines Dept_Head. A company wants …
SQL and Database Transactions: frequently asked questions
What are the basic SQL commands for beginners?
Start with CREATE to build a table, INSERT to add rows, SELECT to read data, UPDATE to change rows and DELETE to remove rows. Then learn GRANT and REVOKE for access, and COMMIT and ROLLBACK for transactions.
What are the types of joins in SQL?
The main types are INNER, LEFT, RIGHT, FULL and CROSS joins. INNER returns only matching rows. LEFT, RIGHT and FULL are outer joins that keep unmatched rows with NULL values. CROSS pairs every row of one table with every row of the other.
What are ACID properties in DBMS?
ACID stands for atomicity, consistency, isolation and durability. Together they make sure a transaction is completed fully or not at all, keeps the data valid, does not disturb other users, and survives failures once committed.
How does concurrency control work in DBMS?
It controls how simultaneous transactions access the same data so that the result is as if they ran one after another. Common methods are locking (such as two-phase locking), timestamp ordering and multiversion techniques. Deadlocks are detected or prevented and one transaction is rolled back.