The UPDATE statement in SQL is used to modify the existing records in a table. Itβs part of Data Manipulation Language (DML) and allows you to update one or more fields in a table.
Hereβs the basic syntax of the UPDATE query:
UPDATE table_name SET column1 = value1, column2 = value2, ... WHERE condition;
Explanation: The SET keyword is used to specify the column(s) to be updated, and the WHERE clause ensures that only the rows matching the condition are updated. If you omit the WHERE clause, all rows in the table will be updated, which might not be what you intend.
Letβs say we have a table called Employees with columns EmployeeID, EmployeeName, and Department.
-- Updating the department of an employee UPDATE Employees SET Department = 'Finance' WHERE EmployeeID = 1;
This statement updates the department of the employee with EmployeeID 1 to "Finance".
You can also update multiple records at once by using a condition in the WHERE clause that matches multiple rows.
-- Updating the department for multiple employees UPDATE Employees SET Department = 'Sales' WHERE EmployeeID IN (2, 3, 4);
This statement updates the department of employees with EmployeeID 2, 3, and 4 to "Sales".
Let's test your understanding of the UPDATE query. Youβre managing a Books table, and the columns are:
Your task is to update the price of the book titled "The Great Adventure" to 22.99.
Write the UPDATE statement below:
WHERE clause is essential to target specific rows. If omitted, all rows will be updated.The UPDATE statement is a powerful tool for modifying data in your database. By specifying the correct condition in the WHERE clause, you can update one or multiple records with ease!
Quick Tip:
Always test your UPDATE queries with a WHERE condition before running them on a live database to avoid accidental updates!
Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!