SQL UPDATE Query
The SQL UPDATE query is used to modify existing records in a database table. It allows changing the values stored in one or more fields of selected records without replacing the entire record. The basic structure includes specifying the table name, field(s) to update, new value(s) to replace old ones, and a condition in the WHERE clause to identify which record(s) to update.
SQL UPDATE query provides a convenient way to keep data up-to-date by correcting mistakes, adding or removing information, and modifying values directly in the database table.
Watch this Update Query in SQL video
How to Use UPDATE Query in SQL?
The UPDATE command in SQL is used to modify or change the existing records in a table. If we want to update a particular value, we use the WHERE clause along with the UPDATE clause. If you do not use the WHERE clause, all the rows will be affected. Moreover, we can use the UPDATE statement to update single or several columns depending on our needs.
Syntax
UPDATE table_name
SET col1=val1, col2=val2…
[Where condition];
where UPDATE, SET, and WHERE are the keywords, table_name is the name of the table you want the update, col1, col2, … are the columns considered to be updated, val1, val2, … assign new values, and the condition section is where the condition is given, followed by a semicolon.
Let’s update some records of the employee table using the Update command in SQL
UPDATE employee
SET e_age=42
WHERE e_name=’sam’;
Get 100% Hike!
Master Most in Demand Skills Now!
Update Table in SQL
- After writing the query, click on the execute button to check for errors
- Once the query is executed, a message appears like ‘Commands completed successfully’
- Let’s check the update
SELECT * from employee;
- Now you can check out the updated data in the table.
SQL Update Multiple Columns
Here is an example for updating multiple columns in SQL.
UPDATE EMPLOYEE
SET Last_Name='KAPADIA',First_Name='MANISH'
WHERE Employee_ID=7369
- After writing the query, click on the execute button to check for errors
- Once the query is executed, a message appears like ‘1 row affected ‘.
SELECT * from employee;
- Now you can check out the updated data in the table.
SQL Update Multiple Rows
UPDATE Employee
SET Middle_Name
= CASE Employee_ID
WHEN 7369 THEN 'A'
WHEN 7499 THEN 'B'
ELSE Middle_Name
END
WHERE Employee_ID IN(7369,7499);
- After writing the query, click on the execute button to check for errors
- Once the query is executed, a message appears like ‘1 row affected ‘.
- Let’s check the update
- SELECT * from employee;
This brings us to the end of this tutorial section, and you’re ready for updating your database with the Update query in SQL.