UPDATE
| Since: | SQL-92(1992) |
|---|
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
The following examples use the employees table.
Updates the department of the employee with ID 3.
sample_update.sql
UPDATE employees SET department = 'Analyst' WHERE id = 3;
Query OK, 1 row affected (0.01 sec) Rows matched: 1 Changed: 1 Warnings: 0
Verifies the update result.
SELECT * FROM employees WHERE id = 3;
+----+------------------+------------+--------+ | id | name | department | salary | +----+------------------+------------+--------+ | 3 | Ginoza Nobuchika | Analyst | 260000 | +----+------------------+------------+--------+ 1 row in set
Updates both the department and salary of the employee with ID 2.
UPDATE employees SET department = 'Enforcer', salary = 300000 WHERE id = 2;
Query OK, 1 row affected (0.01 sec) Rows matched: 1 Changed: 1 Warnings: 0
Verifies the update result.
SELECT * FROM employees WHERE id = 2;
+----+-----------------+------------+--------+ | id | name | department | salary | +----+-----------------+------------+--------+ | 2 | Tsunemori Akane | Enforcer | 300000 | +----+-----------------+------------+--------+ 1 row in set
Gives all Enforcers a 5% salary raise (this example is run against the initial data).
UPDATE employees SET salary = salary * 1.05 WHERE department = 'Enforcer';
Query OK, 2 rows affected (0.01 sec) Rows matched: 2 Changed: 2 Warnings: 0
Verifies the update result. The salaries of the 2 Enforcers in the initial data (Kogami Shinya and Masaoka Tomomi) have increased by 5%.
SELECT * FROM employees WHERE department = 'Enforcer';
+----+----------------+------------+--------+ | id | name | department | salary | +----+----------------+------------+--------+ | 1 | Kogami Shinya | Enforcer | 304500 | | 4 | Masaoka Tomomi | Enforcer | 325500 | +----+----------------+------------+--------+ 2 rows in set
Sets the department of Masaoka Tomomi back to the value it already holds (no change expected).
UPDATE employees SET department = 'Enforcer' WHERE id = 4;
Query OK, 0 rows affected (0.01 sec) Rows matched: 1 Changed: 0 Warnings: 0
Verifies the update result.
SELECT id, name, department FROM employees WHERE id = 4;
+----+----------------+------------+ | id | name | department | +----+----------------+------------+ | 4 | Masaoka Tomomi | Enforcer | +----+----------------+------------+ 1 row in set
Decreases Kogami Shinya's salary by 10,000.
UPDATE employees SET salary = salary - 10000 WHERE id = 1;
Query OK, 1 row affected (0.01 sec) Rows matched: 1 Changed: 1 Warnings: 0
Verifies the update result.
SELECT * FROM employees WHERE id = 1;
+----+---------------+------------+--------+ | id | name | department | salary | +----+---------------+------------+--------+ | 1 | Kogami Shinya | Enforcer | 294500 | +----+---------------+------------+--------+ 1 row in set
Database-Specific Syntax
The basic UPDATE ... SET ... WHERE syntax is supported across all major databases.
UPDATE employees SET department = 'Analyst' WHERE id = 3;
The function used to get the current date differs by database. MySQL uses CURDATE(), PostgreSQL uses CURRENT_DATE, and SQLite uses DATE('now').
-- MySQL
UPDATE logs SET updated_at = CURDATE() WHERE id = 1;
-- PostgreSQL(standard SQL)
UPDATE logs SET updated_at = CURRENT_DATE WHERE id = 1;
-- SQLite
UPDATE logs SET updated_at = DATE('now') WHERE id = 1;
The syntax for updating rows using a JOIN to reference another table also varies by database. MySQL uses UPDATE ... JOIN, PostgreSQL uses UPDATE ... FROM.
-- MySQL UPDATE employees e JOIN departments d ON e.department = d.name SET e.salary = e.salary * 1.05 WHERE d.category = 'Technical'; -- PostgreSQL UPDATE employees SET salary = salary * 1.05 FROM departments d WHERE employees.department = d.name AND d.category = 'Technical';
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.