Master SQL Server From Scratch

Clear, interactive, and structured SQL Server lessons designed for beginners and developers.

COURSE SYLLABUS
1. Introduction to SQL Server
Introduction to SQL Server SQL Server Installation SQL Server Fundamentals
2. Data Types
Data Types Numeric Data Types Character & String Data Types Date & Time Data Types Other SQL Server Data Types
3. Database & Tables
Database & Tables Introduction Creating a Database Creating Tables Table Structure Altering Tables Dropping Tables
4. Constraints
Constraints Introduction PRIMARY KEY FOREIGN KEY UNIQUE Constraint NOT NULL & DEFAULT CHECK Constraint
5. SQL Basics
SQL Basics Introduction SQL Commands SQL Command Categories SELECT Statement INSERT, UPDATE & DELETE
6. CRUD Operations
CRUD Operations Introduction INSERT Operation SELECT Operation UPDATE Operation DELETE Operation
7. SQL Queries
Queries Introduction WHERE Clause ORDER BY Clause DISTINCT LIKE Operator IN, BETWEEN & IS NULL
8. Joins
Joins Introduction INNER JOIN LEFT JOIN RIGHT JOIN FULL OUTER JOIN CROSS JOIN
9. SQL Functions
SQL Functions Introduction String Functions Numeric Functions Date & Time Functions Conversion Functions NULL Functions
10. Grouping & Aggregate Functions
GROUP BY Aggregate Functions COUNT() SUM() AVG() MIN() & MAX()
11. Subqueries
Subqueries Introduction Single-Row Subquery Multiple-Row Subquery Correlated Subquery Subquery with DML
12. Views
SQL Views Introduction Creating a View Updating a View Altering a View Dropping a View
13. Stored Procedures
Stored Procedures Introduction Creating a Stored Procedure Input Parameters Output Parameters Executing & Modifying Stored Procedures
14. Triggers
Triggers Introduction AFTER INSERT Trigger AFTER UPDATE Trigger AFTER DELETE Trigger INSTEAD OF Trigger
15. Transactions
Transactions Introduction COMMIT ROLLBACK SAVEPOINT Transaction with TRY...CATCH
16. Indexes
Indexes Introduction Clustered Index Non-Clustered Index Creating & Dropping Indexes Index Performance
17. Common Table Expressions
CTE Introduction Simple CTE CTE with JOIN CTE with Aggregate Functions Recursive CTE
18. Window Functions
Window Functions Introduction ROW_NUMBER() RANK() and DENSE_RANK() LEAD() and LAG() PARTITION BY
19. Advanced SQL
Advanced SQL Introduction Temporary Tables Table Variables Dynamic SQL Error Handling Performance Optimization

ROLLBACK

Learn how the ROLLBACK statement is used to undo uncommitted changes made during a SQL Server transaction.

What is ROLLBACK?

ROLLBACK is a SQL Server transaction command used to undo changes made during the current transaction.

ROLLBACK is useful when an error occurs or when we decide that the changes made during the transaction should not be saved.

Why Do We Use ROLLBACK?

  • To undo unwanted changes.
  • To handle errors during a transaction.
  • To prevent incorrect data from being saved.
  • To restore the database to the state before the current transaction.

ROLLBACK Syntax

BEGIN TRANSACTION;

-- SQL statements

ROLLBACK;

Simple ROLLBACK Example

Suppose we insert a student record but later decide that the operation should not be saved.

BEGIN TRANSACTION;

INSERT INTO Students
(
    StudentId,
    StudentName,
    City,
    Age
)
VALUES
(
    301,
    'Rahul',
    'Pune',
    22
);

ROLLBACK;

The INSERT operation is undone because the transaction is rolled back.

UPDATE with ROLLBACK

BEGIN TRANSACTION;

UPDATE Students
SET City = 'Mumbai'
WHERE StudentId = 301;

ROLLBACK;

The UPDATE is performed inside the transaction. ROLLBACK then undoes the uncommitted change.

DELETE with ROLLBACK

BEGIN TRANSACTION;

DELETE FROM Students
WHERE StudentId = 301;

ROLLBACK;

The DELETE operation is undone because ROLLBACK cancels the uncommitted transaction changes.

COMMIT vs ROLLBACK

COMMIT ROLLBACK
Permanently saves changes. Undoes uncommitted changes.
Used when the transaction is successful. Used when the transaction should be cancelled or an error occurs.
Completes the transaction. Rolls back the transaction changes.

ROLLBACK After an Error

ROLLBACK is commonly used when an error occurs during a transaction.

BEGIN TRANSACTION;

UPDATE Students
SET Age = Age + 1
WHERE StudentId = 301;

-- If an error occurs

ROLLBACK;

The transaction changes can be undone instead of leaving the database with unwanted changes.

ROLLBACK with Multiple Operations

BEGIN TRANSACTION;

INSERT INTO Students
(
    StudentId,
    StudentName,
    City,
    Age
)
VALUES
(
    302,
    'Priya',
    'Pune',
    21
);

UPDATE Students
SET Age = 22
WHERE StudentId = 302;

DELETE FROM Students
WHERE StudentId = 302;

ROLLBACK;

All uncommitted changes made during the transaction are rolled back.

Important Points

  • ROLLBACK undoes uncommitted transaction changes.
  • ROLLBACK is commonly used when an error occurs.
  • ROLLBACK can undo INSERT, UPDATE and DELETE operations performed in the transaction.
  • Once changes are committed, a normal ROLLBACK cannot undo those committed changes.
  • ROLLBACK helps prevent unwanted data changes from being permanently saved.

Summary

ROLLBACK is used to undo changes made during a transaction that have not been committed.

It is especially useful when an error occurs or when the transaction should not be saved.