âī¸ The UPDATE statement in SQL is used to modify existing records in a database table. It allows you to change the values of one or more columns for specific rows or, if used without a filter, for every row in the table. UPDATE is one of the core Data Manipulation Language (DML) statements.
đ What is the UPDATE Statement?
The UPDATE statement changes data that already exists in a table. You specify the columns to modify using the SET clause and typically use a WHERE clause to identify which rows should be updated.
Information
đ¯ Why Use UPDATE?
The UPDATE statement is essential whenever existing information needs to be corrected or changed.
- đ Correct incorrect data.
- đ Update employee salaries or student marks.
- đ Modify customer contact information.
- đ Change product prices.
- đ Maintain accurate and up-to-date records.
đ Sample Table
Consider the following Students table:
| StudentID | Name | Department | Age | Marks |
|---|---|---|---|---|
| 101 | Alice | Computer Science | 20 | 92 |
| 102 | Bob | Mathematics | 21 | 85 |
| 103 | Charlie | Physics | 22 | 78 |
đ Basic Syntax
UPDATE Syntax
UPDATE table_name
SET column1 = value1,
column2 = value2
WHERE condition;đĄ Update a Single Row
Update the marks of student 102.
Update One Record
UPDATE Students
SET Marks = 90
WHERE StudentID = 102;Result:
| StudentID | Name | Marks |
|---|---|---|
| 101 | Alice | 92 |
| 102 | Bob | 90 |
| 103 | Charlie | 78 |
đ Update Multiple Columns
You can update multiple columns in a single statement.
Update Multiple Columns
UPDATE Students
SET Department = 'Data Science',
Marks = 95
WHERE StudentID = 101;đ Update Multiple Rows
Use a condition that matches multiple rows to update all of them.
Update Multiple Records
UPDATE Students
SET Department = 'Engineering'
WHERE Department = 'Computer Science';đ Update All Rows
If you omit the WHERE clause, every row in the table is updated.
Update Entire Table
UPDATE Students
SET Marks = 100;Warning
đ UPDATE Using Another Table
Many database systems support updating rows using data from another table, although the exact syntax varies between SQL implementations.
Update from Another Table (Example)
UPDATE Students s
SET Department = d.DepartmentName
FROM Departments d
WHERE s.DepartmentID = d.DepartmentID;Important
đ UPDATE with Subquery
A subquery can be used to assign values dynamically.
Update Using a Subquery
UPDATE Employees
SET Salary =
(
SELECT AVG(Salary)
FROM Employees
)
WHERE Department = 'HR';đ UPDATE with CASE
The CASE expression allows different values to be assigned based on conditions.
Conditional UPDATE
UPDATE Students
SET Grade =
CASE
WHEN Marks >= 90 THEN 'A'
WHEN Marks >= 80 THEN 'B'
ELSE 'C'
END;âī¸ INSERT vs UPDATE
| Feature | INSERT | UPDATE |
|---|---|---|
| Purpose | Adds new rows. | Modifies existing rows. |
| Creates New Records | â Yes | â No |
| Changes Existing Data | â No | â Yes |
đŧ Real-World Example
A university decides to increase the marks of all students in the Physics department by 5 points.
Increase Marks
UPDATE Students
SET Marks = Marks + 5
WHERE Department = 'Physics';This statement updates only students in the Physics department while leaving all other records unchanged.
â ī¸ Common Mistakes
- â Forgetting the WHERE clause and updating every row.
- â Updating the wrong column.
- â Using an incorrect filter condition.
- â Assigning values with incompatible data types.
Warning
â ī¸ Best Practices
Best Practice
đ Key Points to Remember
- đ UPDATE modifies existing records.
- đ The SET clause specifies new values.
- đ The WHERE clause determines which rows are updated.
- đ Omitting WHERE updates every row.
- đ Multiple columns can be updated in one statement.
- đ Subqueries and CASE expressions can be used for advanced updates.