Microsoft SQL Server Certification Course
- 6k Enrolled Learners
- Weekend
- Live Class
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. The following topics will be covered in this article:
The UPDATE command is used to modify a single record or multiple records existing in a table.
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.
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:
EmpID | EmpName | EmpEmail | PhoneNumber | City |
1 | Mohan | mohan@xyz.com | 9966449966 | Delhi |
2 | Sonia | sonia@abc.com | 9746964799 | Mumbai |
3 | Sanjay | sanjay@pqr.com | 9654323456 | Bengaluru |
4 | Avni | avni@xyz.com | 9876543678 | Mumbai |
5 | Rahul | rahul@abc.com | 9542456786 | Delhi |
Let us take a look at each one of them.
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;
You will see the following table as output:
EmpID | EmpName | EmpEmail | PhoneNumber | City |
1 | Mohan | mohan@xyz.com | 9966449966 | Delhi |
2 | Sonia | sonia@abc.com | 9746964799 | Mumbai |
3 | Sanjay | sanjay@pqr.com | 9646879876 | Kolkata |
4 | Avni | avni@xyz.com | 9876543678 | Mumbai |
5 | Rahul | rahul@abc.com | 9542456786 | Delhi |
Next, in this article, let us understand how to update data values in 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.
Write a query to update the employees EmpEmail to sample@abc.com for all records to city name Delhi.
UPDATE Employees Set EmpEmail = 'sample@abc.com’ WHERE City =‘Delhi’;
You will see the following table as output:
EmpID | EmpName | EmpEmail | PhoneNumber | City |
1 | Mohan | sample@abc.com | 9966449966 | Delhi |
2 | Sonia | sonia@abc.com | 9746964799 | Mumbai |
3 | Sanjay | sanjay@pqr.com | 9646879876 | Kolkata |
4 | Avni | avni@xyz.com | 9876543678 | Mumbai |
5 | Rahul | sample@abc.com | 9542456786 | Delhi |
Moving on in this article, let us understand how to update the data of a table by omitting the 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.
Write a query to update the employees’ emails to example@xyz.com.
UPDATE Employees Set EmpEmail = 'example@xyz.com’;
You will see the following table as output:
EmpID | EmpName | EmpEmail | PhoneNumber | City |
1 | Mohan | example@xyz.com | 9966449966 | Delhi |
2 | Sonia | example@xyz.com | 9746964799 | Mumbai |
3 | Sanjay | example@xyz.com | 9646879876 | Kolkata |
4 | Avni | example@xyz.com | 9876543678 | Mumbai |
5 | Rahul | example@xyz.com | 9542456786 | Delhi |
Master the language of databases with our comprehensive SQL Course.
Find out our MS SQL Course in Top Cities
India | India |
SQL Training in Bangalore | SQL Course in Pune |
SQL Training in Chennai | SQL Course in Mumbai |
SQL Training in Hyderabad | SQL Course in Kolkata |
Next in this article, let us understand how to update data of a specific table 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:
ContactID | ContactName | ContactEmail | PhoneNumber | City |
1 | Mohan Sharma | contactmohan@xyz.com | 9962449966 | Delhi |
2 | Sonia Khanna | contactsonia@xyz.com | 9461964799 | Mumbai |
3 | Sanjay Kapoor | contactsanjay@xyz.com | 9719879876 | Kolkata |
4 | Avni Mishra | contactavni@xyz.com | 9889743678 | Mumbai |
5 | Rahul Roy | contactrahul@xyz.com | 9818256786 | Delhi |
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);
You will see the following table as output:
EmpID | EmpName | EmpEmail | PhoneNumber | City |
1 | Mohan Sharma | example@xyz.com | 9966449966 | Delhi |
2 | Sonia Khanna | example@xyz.com | 9746964799 | Mumbai |
3 | Sanjay Kapoor | example@xyz.com | 9646879876 | Kolkata |
4 | Avni Mishra | example@xyz.com | 9876543678 | Mumbai |
5 | Rahul Roy | example@xyz.com | 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.
edureka.co