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

UPDATE Operation

The UPDATE statement is used to modify existing records in a SQL Server table.

When information already stored in a table needs to be changed, we use the UPDATE statement. For example, we can update a student's city, age, fees or any other existing information.

What is UPDATE?

The UPDATE statement is used to change existing values in one or more rows of a table.

The SET clause specifies the new value, while the WHERE clause identifies which records should be modified.

UPDATE Syntax

SQL
UPDATE table_name
SET column_name = new_value
WHERE condition;
Important: Always use a suitable WHERE condition when you want to update specific records. Without WHERE, all rows can be updated.

Example Table

We will use the following Students table for the examples.

StudentId StudentName Age City
101 Rahul 22 Pune
102 Priya 23 Mumbai
103 Amit 21 Nashik
104 Neha 22 Pune

Update a Single Record

Suppose we want to change Rahul's city from Pune to Mumbai.

SQL
UPDATE Students
SET City = 'Mumbai'
WHERE StudentId = 101;

Output

After executing the query, Rahul's City value is changed from Pune to Mumbai.

StudentId StudentName Age City
101 Rahul 22 Mumbai
102 Priya 23 Mumbai
103 Amit 21 Nashik
104 Neha 22 Pune

Update Multiple Columns

Multiple columns can be updated in the same UPDATE statement.

SQL
UPDATE Students
SET Age = 24,
    City = 'Pune'
WHERE StudentId = 102;

This query changes both the Age and City of the student whose StudentId is 102.

UPDATE Using a Condition

We can use conditions to update records that satisfy a particular requirement.

SQL
UPDATE Students
SET City = 'Pune'
WHERE Age = 22;

This statement updates the City for all students whose Age is 22.

UPDATE Without WHERE

If the WHERE clause is omitted, the UPDATE statement affects all rows in the table.

SQL
UPDATE Students
SET City = 'Pune';
Warning: The above statement updates the City column for every record in the Students table.

Updating a String Value

String values are written inside single quotation marks.

SQL
UPDATE Students
SET StudentName = 'Rahul Sharma'
WHERE StudentId = 101;

Updating a Numeric Value

Numeric values do not require quotation marks.

SQL
UPDATE Students
SET Age = 25
WHERE StudentId = 101;

Practical Example

Suppose a student changes their city after admission. We can update the student's existing record using StudentId.

SQL
UPDATE Students
SET City = 'Bangalore'
WHERE StudentId = 103;

Only the record with StudentId 103 will be modified.

Important Points

  • UPDATE is used to modify existing records.
  • SET is used to specify the new value.
  • WHERE is used to identify the records to update.
  • Multiple columns can be updated in one statement.
  • Without WHERE, all rows may be updated.
  • String values should be enclosed in single quotes.

Summary

In this section, we learned how the UPDATE statement modifies existing records in a SQL Server table. We covered updating a single column, multiple columns, using conditions, and the importance of using the WHERE clause.