Open In App

SQL | UPDATE Statement

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

The UPDATE statement in SQL is used to update the data of an existing table in the database. We can update single columns as well as multiple columns using the UPDATE statement as per our requirement.

In a very simple way, we can say that SQL commands(UPDATE and DELETE) are used to change the data that is already in the database. The SQL DELETE command uses a WHERE clause.

Syntax

UPDATE table_name SET column1 = value1, column2 = value2,… 

WHERE condition;

table_name: name of the table

column1: name of first , second, third column….

value1: new value for first, second, third column….

condition: condition to select the rows for which the 

values of columns needs to be updated.

Parameter Explanation

  1. UPDATE: Command is used to update the column value in the table.
  2. WHERE: Specifies the condition which we want to implement on the table.

Note: In the above query the SET statement is used to set new values to the particular column and the WHERE clause is used to select the rows for which the columns are needed to be updated. If we have not used the WHERE clause then the columns in all the rows will be updated. So the WHERE clause is used to choose the particular rows. 

Let’s see the SQL update statement with examples.

Query:

CREATE TABLE Customer(
CustomerID INT PRIMARY KEY,
CustomerName VARCHAR(50),
LastName VARCHAR(50),
Country VARCHAR(50),
Age int(2),
Phone int(10)
);

-- Insert some sample data into the Customers table
INSERT INTO Customer (CustomerID, CustomerName, LastName, Country, Age, Phone)
VALUES (1, 'Shubham', 'Thakur', 'India','23','xxxxxxxxxx'),
(2, 'Aman ', 'Chopra', 'Australia','21','xxxxxxxxxx'),
(3, 'Naveen', 'Tulasi', 'Sri lanka','24','xxxxxxxxxx'),
(4, 'Aditya', 'Arpan', 'Austria','21','xxxxxxxxxx'),
(5, 'Nishant. Salchichas S.A.', 'Jain', 'Spain','22','xxxxxxxxxx');

Select * from Customer;

Output:

 

Update Single Column 

Update the column NAME and set the value to ‘Nitin’ in the rows where the Age is 22.

UPDATE Customer SET CustomerName  
= 'Nitin' WHERE Age = 22;

Output:

 

Updating Multiple Columns

Update the columns NAME to ‘Satyam’ and Country to ‘USA’ where CustomerID is 1.

UPDATE Customer SET CustomerName = 'Satyam', 
Country = 'USA' WHERE CustomerID = 1;

Output:

 

Note: For updating multiple columns we have used comma(,) to separate the names and values of two columns.

Omitting WHERE Clause

If we omit the WHERE clause from the update query then all of the rows will get updated.

UPDATE Customer SET CustomerName = 'Shubham';

Output: 

The table Customer will now look like this,

 


Last Updated : 30 Oct, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads