;

SQL UPDATE


In this tutorial, we will learn how to UPDATE records into a table of a database using SQL.

SQL UPDATE Statement

  • SQL UPDATE statement is used to update/modify the existing record in a table.
  • UPDATE statement can update one or more records in a table.
  • Use WHERE clause to update/modify only specific records otherwise all records of selected columns updated.
Syntax - UPDATE Statement
UPDATE tableName 
SET column1 = value1, column2 = value2,...columnN=valueN
WHERE [CONDITION]

Example:

Let us consider this table "Employee" for records.

Table Name: Employee

ID EmpName City Country Gender Salary
1 Shankar Delhi India male 25000
2 Sourabh Delhi India male 30000
3 Ranvijay Mumbai India male 15000
4 Kapil Noida India male 25000
5 Shalini Jaipur India female 18000
6 Rakesh Faridabad India male 23000
7 Akshay Mumbai India male 21000
8 Sarah New York US female 76000
9 Rocky Noida India male 28000

Example of UPDATE Statement

Updating a Single Column

For Updating Salary of ID=3 in a table "Employee", a query will be: 

Example 
UPDATE Employee 
SET Salary = 16000
WHERE ID=3

Now if we SELECT this record, we can see the updated value

Example - SELECT Updated Record
--For Selecting Updated Record,query will be
SELECT * FROM Employee WHERE ID=3
Output 
ID EmpName City Country Gender Salary
3 Ranvijay Mumbai India male 15000

Updating Multiple Columns

For Updating "City" and "Salary" of ID=3 in a table "Employee", a query will be:

Example
UPDATE Employee
SET City = 'Chennai', Salary= 18000
WHERE ID=3
Output
ID EmpName City Country Gender Salary
3 Ranvijay Chennai India male 18000