Hook: ROLLBACK is the database's undo button: interviewers love it because it checks whether you understand how SQL keeps data safe when something goes wrong.
Question: What does ROLLBACK do in SQL?
Answer: ROLLBACK cancels the changes made in the current transaction and returns the database to the last committed state. It is used when something fails, when you decide not to keep the changes, or when you want to undo part of a transaction with a savepoint. A key idea is that it only affects uncommitted work; once data is committed, ROLLBACK cannot bring it back.
Interview-Ready Answer: I use ROLLBACK to undo all uncommitted changes in the current transaction. That means inserts, updates, and deletes done since BEGIN are discarded, and the database goes back to the last committed state. It is especially important in multi-step operations like checkout or money movement, because one failed step should not leave partial data behind. If I need only part of the work undone, I use a SAVEPOINT and roll back to that point instead of canceling the whole transaction.
Detailed Explanation: Think of a transaction as a work session with a clear finish line. Until you COMMIT, the database treats the changes as temporary. ROLLBACK says, “discard this session’s work and restore the previous committed version.” That is the heart of atomicity (all-or-nothing behavior), one of the ACID properties of transactions.
BEGIN (or your client starts one implicitly in autocommit-off mode).ROLLBACK.One important mental model: a rollback is not a time machine for the whole database. It only applies to the current transaction. If another session already committed its work, your rollback cannot touch it.
| Command | What it does | Typical use |
|---|---|---|
COMMIT | Makes changes permanent | Success path |
ROLLBACK | Undoes all uncommitted changes | Failure or cancel |
ROLLBACK TO SAVEPOINT | Undoes changes after a marker | Partial undo |
SAVEPOINT, you can undo only the risky part while keeping earlier valid work.From the application’s point of view, asking for a rollback is fast. But the database still has to clean up the transaction’s changes, so the real cost depends on how much work the transaction did. A small transaction with a few rows often rolls back in milliseconds; a huge batch that touched hundreds of thousands of rows can take noticeable time because the engine must discard that work and release resources. In interview terms, the logical complexity is roughly proportional to the amount of uncommitted change, not a fixed constant.
ROLLBACK or raise an error if nothing is active.COMMIT happens, rollback cannot undo it.Memory angle for the whiteboard: ROLLBACK is the “erase the draft, not the published book” command. Draft work disappears; published work stays.
Real-World Example: Imagine a checkout service for an online store. In one transaction, it inserts an order, reserves inventory, and marks the cart as paid. If the inventory update fails because stock is gone, the service should ROLLBACK so the order row, payment flag, and stock reservation do not end up half-finished.
What goes wrong when people misunderstand this? They might commit each step separately, thinking they can “fix it later.” Then a failure leaves the system in a messy state: the customer sees an order confirmation, inventory is reserved incorrectly, and support logs show constraint errors or orphan rows. The symptom is usually partial data, angry customers, and reconciliation jobs that have to clean up what should have been prevented by one transaction.
One subtle but important lesson: if the checkout service charged a credit card through an external payment API before the database rollback, the database cannot undo that charge. That is why teams often use a careful order of operations, idempotency keys, and compensating actions for external systems.
-- Demonstrates full rollback and partial rollback with SAVEPOINT.
-- This script is intentionally simple and runnable in common SQL databases
-- such as PostgreSQL and SQLite.
DROP TABLE IF EXISTS orders_demo;
CREATE TABLE orders_demo (
id INTEGER PRIMARY KEY,
item TEXT NOT NULL,
quantity INTEGER NOT NULL CHECK (quantity > 0)
);
-- Start a transaction, make some changes, then undo everything.
BEGIN;
INSERT INTO orders_demo (id, item, quantity) VALUES (1, 'Book', 1);
INSERT INTO orders_demo (id, item, quantity) VALUES (2, 'Pen', 3);
ROLLBACK;
-- After a full rollback, the table is still empty because nothing was committed.
SELECT 'after full rollback' AS stage, COUNT(*) AS row_count
FROM orders_demo;
-- Now keep the good work, but undo only the risky part.
BEGIN;
INSERT INTO orders_demo (id, item, quantity) VALUES (3, 'Notebook', 2);
SAVEPOINT before_optional_change;
INSERT INTO orders_demo (id, item, quantity) VALUES (4, 'Pencil', 5);
-- If we change our mind here, we can discard only the rows after the savepoint.
ROLLBACK TO SAVEPOINT before_optional_change;
-- This row is kept because it happens after the savepoint rollback.
INSERT INTO orders_demo (id, item, quantity) VALUES (5, 'Eraser', 1);
COMMIT;
SELECT *
FROM orders_demo
ORDER BY id;Follow-up & Tricky Questions:
ROLLBACK and COMMIT? COMMIT makes the transaction permanent, while ROLLBACK throws away the uncommitted work. They are opposites: one publishes the draft, the other deletes it.SAVEPOINT? A savepoint is a marker inside a transaction that lets you undo only the later part of the work. It is useful when one sub-step is risky but you still want to keep the earlier successful changes.ROLLBACK after an error? In many databases, the transaction becomes aborted after a statement error, and rollback is required before you can continue. That is why application code usually catches the failure and rolls back immediately.ROLLBACK undo locks? Yes, the transaction’s locks are released when the transaction ends. That is one reason long-running transactions are dangerous: they can block other users until rollback or commit happens.ROLLBACK undo schema changes? Sometimes, but not always. It depends on the database and the specific DDL statement, because some systems auto-commit certain schema operations.Common Mistakes:
ROLLBACK can undo committed data. Correction: It only affects the current uncommitted transaction.Memory Hook: “Rollback is the undo button for the current draft. If it’s committed, the ink is dry.”
Cheat Sheet:
ROLLBACK cancels uncommitted changes.COMMIT makes changes permanent.SAVEPOINT lets you undo part of a transaction.Practice Tasks:
ROLLBACK leaves the table unchanged.SAVEPOINT, make a second insert, then roll back only to the savepoint and confirm the first insert remains.