Data rarely stays static. After data has been added to tables, it often needs modification. For example, a customer might change their address, an employee could receive a promotion and salary increase, or an order's status might need updating. The UPDATE statement provides the functionality to change existing data within the rows of database tables.
The fundamental syntax for modifying data looks like this:
UPDATE table_name
SET column1 = new_value1,
column2 = new_value2,
...
WHERE condition;
Let's break down the components:
UPDATE table_name: You start by specifying the table you want to modify. Replace table_name with the actual name of your table (e.g., Employees, Products, Orders).SET column1 = new_value1, ...: The SET clause is where you define the changes. You list the column(s) you want to update and the new value(s) they should hold.
column1, column2, etc., are the names of the columns whose values you want to change.new_value1, new_value2, etc., are the corresponding new values. Ensure the data type of the new value matches the column's data type (e.g., provide text for a VARCHAR column, a number for an INTEGER column).column = value pair with a comma.WHERE condition: This clause is absolutely essential for targeted updates. It specifies which rows in the table should be modified. The condition works exactly like the WHERE clause you learned about for the SELECT statement. It filters the rows, and only those rows that satisfy the condition will be updated.You might be tempted to leave out the WHERE clause. Be very careful about doing this. If you execute an UPDATE statement without a WHERE clause, the database will apply the changes specified in the SET clause to every single row in the table.
Imagine accidentally setting every employee's salary to the same value or changing every customer's city to the same location. This kind of mistake can be difficult and time-consuming to fix, especially in large databases.
Rule of Thumb: Always include a WHERE clause in your UPDATE statements unless you are absolutely certain you intend to modify every row in the table. It's often a good practice to first write a SELECT statement with the same WHERE clause to verify which rows would be affected before running the actual UPDATE.
Let's use a Employees table to see UPDATE in action.
Employees Table:
| EmployeeID | FirstName | LastName | Department | Salary |
|---|---|---|---|---|
| 101 | Alice | Smith | Sales | 60000 |
| 102 | Bob | Jones | IT | 75000 |
| 103 | Charlie | Brown | Sales | 62000 |
| 104 | Diana | Prince | HR | 68000 |
Example 1: Updating a Single Column for a Specific Row
Suppose Bob Jones (EmployeeID 102) moves from the IT department to the Engineering department. We need to update his record.
UPDATE Employees
SET Department = 'Engineering'
WHERE EmployeeID = 102;
UPDATE Employees: We specify the Employees table.SET Department = 'Engineering': We set the Department column to the new value 'Engineering'.WHERE EmployeeID = 102: We target only the row where the EmployeeID is 102.After executing this statement, the table would look like this:
| EmployeeID | FirstName | LastName | Department | Salary |
|---|---|---|---|---|
| 101 | Alice | Smith | Sales | 60000 |
| 102 | Bob | Jones | Engineering | 75000 |
| 103 | Charlie | Brown | Sales | 62000 |
| 104 | Diana | Prince | HR | 68000 |
Example 2: Updating Multiple Columns for a Specific Row
Now, let's say Alice Smith (EmployeeID 101) gets promoted. Her department changes to 'Sales Management', and her salary increases to 65000.
UPDATE Employees
SET Department = 'Sales Management',
Salary = 65000
WHERE EmployeeID = 101;
SET clause, separated by a comma.WHERE clause still precisely targets Alice's record using her unique EmployeeID.The table now becomes:
| EmployeeID | FirstName | LastName | Department | Salary |
|---|---|---|---|---|
| 101 | Alice | Smith | Sales Management | 65000 |
| 102 | Bob | Jones | Engineering | 75000 |
| 103 | Charlie | Brown | Sales | 62000 |
| 104 | Diana | Prince | HR | 68000 |
Example 3: Updating Multiple Rows
Imagine the company decides to give a small raise of 1000toeveryonecurrentlyearninglessthan65000.
UPDATE Employees
SET Salary = Salary + 1000
WHERE Salary < 65000;
SET clause uses an expression: Salary = Salary + 1000. This takes the current value in the Salary column and adds 1000 to it.WHERE Salary < 65000 clause identifies all employees whose current salary meets this condition. In our current table, this would affect Charlie Brown (Salary 62000).After this update:
| EmployeeID | FirstName | LastName | Department | Salary |
|---|---|---|---|---|
| 101 | Alice | Smith | Sales Management | 65000 |
| 102 | Bob | Jones | Engineering | 75000 |
| 103 | Charlie | Brown | Sales | 63000 |
| 104 | Diana | Prince | HR | 68000 |
Example 4: The Danger Zone - Updating Without WHERE (Use With Extreme Caution!)
If you were to run the following command (and we strongly advise against doing this without careful consideration):
-- DANGEROUS EXAMPLE - DO NOT RUN UNLESS INTENDED!
UPDATE Employees
SET Department = 'General';
This command would change the Department to 'General' for every single employee in the table, losing all the previous department information. Always double-check your UPDATE statements, especially the WHERE clause, before execution.
The UPDATE statement is a powerful tool for maintaining the accuracy of your data over time. By combining it carefully with the SET and WHERE clauses, you can make precise modifications to existing records in your relational database.
Was this section helpful?
© 2026 ApX Machine LearningEngineered with