What is the UPDATE Statement in SQL ?
Answer : The SQL UPDATE statement is used to modify the existing records in a table. We can update single columns as well as multiple columns using UPDATE statement as per our requirement. We can use the WHERE clause with the UPDATE query to update the selected rows, otherwise all the rows would be affected.
Syntax
If you want to update a single record :
UPDATE table_name
SET column_name = value
WHERE condition;
We can update multiple columns in a single UPDATE statement by separating each column with a comma.
UPDATE table_name
SET column1 = value1,
column2 = value2
WHERE condition;
You can combine N number of conditions using the AND or the OR operators.
Example
Suppose we have an Employees table:
| Employee_ID | Employee_Name | Salary |
|---|---|---|
| 101 | Anjali | 50000 |
| 102 | Priya | 45000 |
| 103 | Amit | 55000 |
If we want to update Priya’s salary to 5000
UPDATE Employees
SET Salary = 5000
WHERE Employee_Name = ‘Priya’;
After Update
| Employee_ID | Employee_Name | Salary |
|---|---|---|
| 101 | Anjali | 50000 |
| 102 | Priya | 5000 |
| 103 | Amit | 55000 |
If we want to update Priya’s name and salary:
UPDATE Employees
SET Employee_Name = ‘Priyanka’,
Salary = 60000
WHERE Employee_ID = 102;
After Update
| Employee_ID | Employee_Name | Salary |
|---|---|---|
| 101 | Anjali | 50000 |
| 102 | Priyanka | 60000 |
| 103 | Amit | 55000 |