AWS Global Infrastructure

Databases

Topics Covered
  • Cassandra (13 Blogs)
  • MongoDB Dev and Admin (15 Blogs)
  • MySQL (31 Blogs)
  • SQL Essentials Training and Certification (3 Blogs)
SEE MORE Databases blog posts

SQL UPDATE : Learn How To Update Values In A Table

Last updated on Feb 21,2025 12.6K Views

image not found!image not found!image not found!image not found!Copy Link!
15 / 37 Blog from Introduction to SQL

While using databases, we may often want to update a few data values either in a single record or for multiple records. Structured Query Language(SQL) provides various commands to access, retrieve and manage databases. Out of the lot, one such command is the UPDATE command. The UPDATE command is used to update data existing in a table.

SQL UPDATE statement

The UPDATE command is used to modify a single record or multiple records existing in a table. 

Syntax:

UPDATE TableName
SET Column1 = Value1, Column2 = Value2, …, ColumnN = ValueN
WHERE Condition;

Here, the WHERE clause specifies which records must be updated. Just in case, you omit the WHERE clause, all the records existing in the table will be updated. 

Since you have understood the syntax, let us now discuss the various ways to use it with examples.

Examples:

For your better understanding, I have divided the examples into the following sections:

I am going to consider the following table to explain to you the examples:

EmpIDEmpNameEmpEmailPhoneNumberCity

1

Mohan

[email protected]

9966449966

Delhi

2

Sonia

[email protected]

9746964799

Mumbai

3

Sanjay

[email protected]

9654323456

Bengaluru

4

Avni

[email protected]

9876543678

Mumbai

5

Rahul

[email protected]

9542456786

Delhi

Let us take a look at each one of them. 

Update Single Record

Example: 

Write a query to update the 3rd employee (Employee ID) with a new phone number and city.

UPDATE Employees
SET PhoneNumber ='9646879876', City= 'Kolkata'
WHERE EmpID = 3;

Output:

You will see the following table as output:

EmpIDEmpNameEmpEmailPhoneNumberCity

1

Mohan

[email protected]

9966449966

Delhi

2

Sonia

[email protected]

9746964799

Mumbai

3

Sanjay

[email protected]

9646879876

Kolkata

4

Avni

[email protected]

9876543678

Mumbai

5

Rahul

[email protected]

9542456786

Delhi

Next, in this article, let us understand how to update data values in multiple records.

Update Multiple Records

To update multiple records in the table, we must use the WHERE clause. The WHERE clause determines the number of records that will be updated.

Example: 

Write a query to update the employees EmpEmail to [email protected]  for all records to city name Delhi.

UPDATE Employees
Set EmpEmail = '[email protected]’
WHERE City =‘Delhi’;

Output:

You will see the following table as output:

EmpIDEmpNameEmpEmailPhoneNumberCity

1

Mohan

[email protected]

9966449966

Delhi

2

Sonia

[email protected]

9746964799

Mumbai

3

Sanjay

[email protected]

9646879876

Kolkata

4

Avni

[email protected]

9876543678

Mumbai

5

Rahul

[email protected]

9542456786

Delhi

Moving on in this article, let us understand how to update the data of a table by omitting the WHERE clause.

Update data by omitting WHERE Clause

When we omit the WHERE clause while using the UPDATE statement in SQL, then there is no limit set on the number of records that must be updated. So, all the records will be updated automatically.

Example: 

Write a query to update the employees’ emails to [email protected].

UPDATE Employees
Set EmpEmail = '[email protected]’;

Output:

You will see the following table as output:

EmpIDEmpNameEmpEmailPhoneNumberCity

1

Mohan

[email protected]

9966449966

Delhi

2

Sonia

[email protected]

9746964799

Mumbai

3

Sanjay

[email protected]

9646879876

Kolkata

4

Avni

[email protected]

9876543678

Mumbai

5

Rahul

[email protected]

9542456786

Delhi

Master the language of databases with our comprehensive SQL Server Course.

Next in this article, let us understand how to update data of a specific table from another table.

Update data from another table

We can use the UPDATE statement to update the data of a specific table considering the data of another table. 

Let us consider the following table:

ContactIDContactNameContactEmailPhoneNumberCity

1

Mohan Sharma

[email protected]

9962449966

Delhi

2

Sonia Khanna

[email protected]

9461964799

Mumbai

3

Sanjay Kapoor

[email protected]

9719879876

Kolkata

4

Avni Mishra

[email protected]

9889743678

Mumbai

5

Rahul Roy

[email protected]

9818256786

Delhi

Example: 

Write a query to update the employees’ names by taking the data from the contacts table.

UPDATE Employees
SET EmpName = (SELECT EmpName
                  FROM Contacts
                  WHERE Contacts.City = Employees.City);

Output:

You will see the following table as output:

EmpIDEmpNameEmpEmailPhoneNumberCity

1

Mohan Sharma

[email protected]

9966449966

Delhi

2

Sonia Khanna

[email protected]

9746964799

Mumbai

3

Sanjay Kapoor

[email protected]

9646879876

Kolkata

4

Avni Mishra

[email protected]

9876543678

Mumbai

5

Rahul Roy

[email protected]

9542456786

Delhi

 We can also rewrite the above query as follows:

UPDATE Employees
SET Employees.EmpName = Contacts.EmpName
FROM Employees
INNER JOIN Contacts
ON (Employees.City = Contacts.City);

So, folks that is how you can use the UPDATE statement in SQL. With that, we come to an end to this article on SQL UPDATE. I hope you found this article informative.

If you wish to learn more about MySQL and get to know this open-source relational database, then check out our MySQL DBA Certification Training which comes with instructor-led live training and real-life project experience. This training will help you understand MySQL in-depth and help you achieve mastery over the subject.

Got a question for us? Please mention it in the comments section of this article on “SQL UPDATE” and I will get back to you.

Comments
0 Comments

Join the discussion

Browse Categories

webinar REGISTER FOR FREE WEBINAR
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.

image not found!
image not found!

SQL UPDATE : Learn How To Update Values In A Table

edureka.co