TGViewer
SQL Programming Resources SQL Programming Resources @sqlanalyst · 76.7K subscribers
Post #2823 780
The exact workflow should follow your organization's production-change and approval procedures.

29️⃣ COMMIT vs SAVEPOINT vs ROLLBACK

Remember:

• COMMIT → Save the transaction

• ROLLBACK → Undo uncommitted transaction changes

• SAVEPOINT → Mark a point inside a transaction

• ROLLBACK TO SAVEPOINT → Undo changes after that point

Example:

BEGIN;

UPDATE customers
SET status = 'Active'
WHERE customer_id = 101;

SAVEPOINT s1;

UPDATE customers
SET status = 'Inactive'
WHERE customer_id = 102;

ROLLBACK TO SAVEPOINT s1;

COMMIT;


The first update can remain while the second update is rolled back, subject to the database's transaction semantics.

30️⃣ Common Mistakes

❌ Mistake 1: Forgetting COMMIT — You may make changes but not persist them as intended.

❌ Mistake 2: Assuming ROLLBACK always works — If the changes have already been committed, a normal rollback cannot undo them.

❌ Mistake 3: Running UPDATE without checking the WHERE condition — Dangerous:

UPDATE customers SET status = 'Inactive';


Safer workflow:

SELECT * FROM customers WHERE ...;
UPDATE customers SET status = 'Inactive' WHERE ...;


❌ Mistake 4: Assuming transaction behavior is identical everywhere — Database systems differ in areas such as:

- Autocommit

- DDL transactions

- Isolation

- Locking

- Savepoints

- Error handling

🎯 Interview Questions

•

Q1. What is a transaction? A transaction is a logical unit of one or more database operations that are handled together.

•

Q2. What does COMMIT do? It commits the transaction's changes.

•

Q3. What does ROLLBACK do? It reverses uncommitted changes in the transaction.

•

Q4. What is SAVEPOINT? A savepoint marks a point inside a transaction to which you can potentially roll back without undoing the entire transaction.

•

Q5. What does ACID stand for?

A → Atomicity,

C → Consistency,

I → Isolation,

D → Durability

•

Q6. What is atomicity? It treats the transaction as a logical unit so that its operations are committed together or rolled back as appropriate.

•

Q7. What is isolation? It controls how concurrent transactions interact and what changes they can see.

•

Q8. What is durability? Committed changes are intended to survive failures according to the database's durability mechanisms.

•

Q9. What is autocommit? A mode where individual statements may be committed automatically.

•

Q10. What is a dirty read? Reading data changed by another transaction before that transaction commits.

🧠 Practice Questions

Practice 1 Write a transaction that updates a customer's status and commits it.

BEGIN;
UPDATE customers SET status = 'Active' WHERE customer_id = 101;
COMMIT;


Practice 2 Update a customer's status but roll back the change.

BEGIN;
UPDATE customers SET status = 'Inactive' WHERE customer_id = 101;
ROLLBACK;


Practice 3 Create a savepoint after the first update.

BEGIN;
UPDATE customers SET status = 'Active' WHERE customer_id = 101;
SAVEPOINT customer_update;
UPDATE customers SET status = 'Inactive' WHERE customer_id = 102;
ROLLBACK TO SAVEPOINT customer_update;
COMMIT;


Practice 4 Before executing this UPDATE:

UPDATE orders SET status = 'Cancelled' WHERE order_date < DATE '2025-01-01';


write a SELECT that lets you inspect the affected records.

SELECT * FROM orders WHERE order_date < DATE '2025-01-01';


Practice 5 Explain what happens here:

BEGIN;
UPDATE accounts SET balance = balance - 5000 WHERE account_id = 101;
UPDATE accounts SET balance = balance + 5000 WHERE account_id = 202;
ROLLBACK;
More from @sqlanalyst
  1. Sep 29, 2026SQL Interview Series — Part 2 📌 Question 2: Find Duplicate Records Suppose you have an Em…
  2. Sep 29, 2026𝗙𝗥𝗘𝗘 𝗥𝗲𝘀𝗼𝘂𝗿𝗰𝗲𝘀 𝗧𝗼 𝗟𝗲𝗮𝗿𝗻 𝗔𝗜 𝗶𝗻 𝟮𝟬𝟮𝟲🚀 ​ Explore 6 free resource…
  3. Sep 29, 2026SQL Interview Series — Part 1 Hi guys, let's start a SQL interview series covering frequen…
  4. Sep 28, 2026🧠 Real-World SQL Scenario-Based Questions & Answers 1. Get the 2nd highest salary from th…
  5. Sep 28, 2026🎓 𝗛𝗔𝗥𝗩𝗔𝗥𝗗 𝗨𝗡𝗜𝗩𝗘𝗥𝗦𝗜𝗧𝗬 𝗙𝗥𝗘𝗘 𝗢𝗡𝗟𝗜𝗡𝗘 𝗖𝗢𝗨𝗥𝗦𝗘𝗦 😍 Dreaming of…
  6. Sep 27, 2026Data Analytics Roadmap | |-- Fundamentals | |-- Mathematics | | |-- Descriptive Statistics…
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook →Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 →