UPDATE
An SQL statement that updates existing rows in a table. Be careful: if you omit the WHERE clause, all rows will be updated.
Syntax
-- Updates a column in rows that match the condition. UPDATE table_name SET column1 = value1 WHERE condition; -- Updates multiple columns at once. UPDATE table_name SET column1 = value1, column2 = value2 WHERE condition; -- Updates a column by calculating from its current value. UPDATE table_name SET column = column + increment WHERE condition;
Syntax Overview
| Syntax | Description |
|---|---|
| UPDATE table_name SET column = value | Sets a new value for the specified column. If the WHERE clause is omitted, all rows are updated. |
| SET column1 = value1, column2 = value2 | Updates multiple columns at once. Separate each column-value pair with a comma. |
| WHERE condition | Filters which rows to update. If omitted, all rows are affected. |
| SET column = column + value | Updates a column based on its current value. Useful for incrementing or decrementing quantities such as stock counts. |
Sample Code
-- Updates the department of the employee with ID 5. UPDATE employees SET department = 'Engineering' WHERE id = 5; -- Updates both the department and salary of the employee with ID 3. UPDATE employees SET department = 'Sales', salary = 320000 WHERE id = 3; -- Gives all Sales department employees a 5% salary raise. UPDATE employees SET salary = salary * 1.05 WHERE department = 'Sales'; -- Sets the retirement date to today's date. UPDATE employees SET retire_date = CURDATE() WHERE id = 7; -- Decrements the stock count for a product by 1. UPDATE products SET stock = stock - 1 WHERE product_id = 42 AND stock > 0;
Output
-- After UPDATE runs, it returns the number of rows affected. -- Example: when 3 rows are updated -- Query OK, 3 rows affected (0.01 sec) -- Rows matched: 3 Changed: 3 Warnings: 0
Database-Specific Syntax
The basic UPDATE ... SET ... WHERE syntax is supported across all major databases.
UPDATE employees SET department = 'Engineering' WHERE id = 5;
The function used to get the current date differs by database. MySQL uses CURDATE(), PostgreSQL uses CURRENT_DATE, Oracle uses SYSDATE, SQL Server uses GETDATE(), and SQLite uses DATE('now').
-- MySQL
UPDATE employees SET retire_date = CURDATE() WHERE id = 7;
-- PostgreSQL / Oracle (standard SQL)
UPDATE employees SET retire_date = CURRENT_DATE WHERE id = 7;
-- SQL Server
UPDATE employees SET retire_date = CAST(GETDATE() AS DATE) WHERE id = 7;
-- SQLite
UPDATE employees SET retire_date = DATE('now') WHERE id = 7;
The syntax for updating rows using a JOIN to reference another table also varies by database. MySQL uses UPDATE ... JOIN, PostgreSQL uses UPDATE ... FROM, and SQL Server uses UPDATE ... FROM ... JOIN.
-- MySQL UPDATE employees e JOIN departments d ON e.department = d.name SET e.salary = e.salary * 1.05 WHERE d.category = 'Technology'; -- PostgreSQL UPDATE employees SET salary = salary * 1.05 FROM departments d WHERE employees.department = d.name AND d.category = 'Technology';
Notes
UPDATE is used to modify existing data. Always use a WHERE clause to limit which rows are affected before running the statement.
If you omit the WHERE clause, every row in the table will be updated. In production environments especially, verify the target rows with a SELECT first, and consider wrapping the statement in a BEGIN transaction for safety.
MySQL also supports updating across multiple tables using a JOIN with UPDATE — for example, to update rows based on values from another table. See also PDO Transactions for information on transactions.
If you find any errors or copyright issues, please contact us.