SQL Update

The UPDATE statement changes the values stored in existing rows of a table.

Changing existing data with UPDATE

The UPDATE statement modifies data that is already in a table. You name the table, set one or more columns to new values, and use a WHERE clause to describe exactly which rows should change. Unlike INSERT, which creates new rows, UPDATE rewrites the values inside rows that already exist.

The basic shape of an UPDATE

UPDATE Employees
SET department = 'Operations'
WHERE employee_id = 101;

This statement finds the single employee whose id is 101 and changes their department. The WHERE clause is what keeps the change limited to that one row.

Updating more than one column

You can change several columns in the same statement by separating the assignments with commas in the SET clause. Each assignment names a column and the new value it should hold.

Setting multiple columns together

UPDATE Employees
SET department = 'Engineering',
    salary = 75000
WHERE last_name = 'Okafor';
Note: If you leave out the WHERE clause, UPDATE changes every row in the table. Running UPDATE Employees SET salary = 50000; would overwrite the salary of every employee. Always double-check that a WHERE clause is present before you run an update.

Basing new values on existing ones

The value on the right side of an assignment can be a calculation, and it may refer to the column's own current value. This is how you apply a raise, a discount, or any adjustment relative to what is already stored.

Giving one department a 10% raise

UPDATE Employees
SET salary = salary * 1.10
WHERE department = 'Sales';

How many rows will change?

The WHERE clause decides how many rows an update touches. A condition on a unique column changes one row, a broader condition changes many, and no condition at all changes them all. The table below shows the pattern.

WHERE clauseRows affected
WHERE employee_id = 101One specific row
WHERE department = 'Sales'Every row in that department
WHERE salary < 40000Every row matching the condition
(no WHERE clause)All rows in the table
  • UPDATE changes values in rows that already exist.
  • The SET clause can assign one or many columns.
  • New values can be calculated from current values, such as salary * 1.10.
  • Always include a WHERE clause unless you truly intend to change every row.

Exercise: SQL Update

What is the purpose of the UPDATE statement?