Skip to content

SQL UPDATE Statement

Syntax

The UPDATE statement in SQL is used to modify data in one or more rows of a table. The SET clause specifies the columns to update, and the WHERE clause limits the rows affected.

-- Update specific rows
UPDATE table_name
SET column1 = value1, column2 = value2
WHERE condition;
-- Update all rows (use cautiously)
UPDATE table_name
SET column1 = value1;

Example

-- Update a specific row
UPDATE employees
SET age = 35
WHERE name = 'John Doe';
/* Updates:
| id | name | department | age |
|----|------------|------------|-----|
| 1 | John Doe | Sales | 35 |
*/
-- Update multiple rows
UPDATE employees
SET department = 'General'
WHERE age > 30;
/* Updates:
| id | name | department | age |
|----|------------|------------|-----|
| 1 | John Doe | General | 35 |
| 3 | Alice Brown| General | 40 |
*/
-- Update all rows
UPDATE employees
SET department = 'Unknown';
/* Updates all rows:
| id | name | department | age |
|----|------------|------------|-----|
| 1 | John Doe | Unknown | 35 |
| 2 | Jane Smith | Unknown | 25 |
| 3 | Alice Brown| Unknown | 40 |
*/