DevLearningTools

LEARN · SQL

SQL UPDATE

UPDATE changes values in rows that already exist. Learn the syntax, and why WHERE is non-negotiable.

UPDATE changes the values in existing rows, a raise for one employee, a corrected email address, a status flip. It's one of the simplest statements in SQL to write, and one of the easiest to run accidentally against the entire table if you're not careful.

Learning Objectives

  • Write an UPDATE statement that changes one or more columns.
  • Understand why WHERE is effectively mandatory on every UPDATE.

Target Rows

Matched by WHERE

SET Values

New column values applied

Rows Updated

Only the matched rows change

Basic UPDATE

SET lists the column (or columns, comma-separated) to change and their new values. WHERE narrows down which rows are affected, using the exact same conditions covered in the WHERE lesson.

MySQL / MariaDB / PostgreSQL / SQLite / SQL Server / Oracle
UPDATE employees
SET salary = 99000
WHERE first_name = 'Grace' AND last_name = 'Hopper';

Updating Multiple Columns

Multiple columns in one SET, comma-separated. salary = salary * 1.05 shows the new value can reference the column's own current value, a 5% raise applied to whatever each matched row's salary already was.

MySQL / MariaDB / PostgreSQL / SQLite / SQL Server / Oracle
UPDATE employees
SET department = 'Research', salary = salary * 1.05
WHERE department = 'Engineering' AND salary < 90000;

Why WHERE Is Effectively Mandatory

UPDATE employees SET salary = 99000 with no WHERE at all is perfectly valid SQL, and it sets every single employee's salary to 99000. There's no confirmation prompt, no automatic undo once committed. Write the WHERE clause, or run the same condition as a SELECT first to preview the affected rows, before running any UPDATE.

NOTE

Many GUI tools (like MySQL Workbench) have a "safe update mode" that blocks UPDATE/DELETE without a WHERE on a key column by default. It's a useful safety net, but don't rely on it instead of actually checking your WHERE clause, scripts and command-line tools won't have it.

Try It Yourself

Write an UPDATE that moves every Research employee into the Engineering department.

Answer
UPDATE employees
SET department = 'Engineering'
WHERE department = 'Research';

Frequently Asked Questions

What happens if my WHERE clause matches zero rows?

Nothing fails, the UPDATE runs successfully and reports 0 rows affected. This is often a silent bug, your code assumed something was updated when nothing actually was.

Can I update a column to NULL?

Yes, SET column_name = NULL works exactly like any other value, as long as the column allows NULL.

Common Beginner Mistakes

Running UPDATE without a WHERE clause

This updates every row in the table, with no confirmation. Always write and double-check the WHERE clause, or preview it as a SELECT first.

Assuming a 0-rows-affected result means an error occurred

It doesn't, it just means the WHERE clause matched nothing, which is valid and silent.

Best Practices

  • Run your WHERE condition as a SELECT first to preview exactly which rows will be affected, before running the UPDATE.
  • Check the affected-row count your database driver returns, a 0 when you expected a change is worth investigating.

Interview Questions

Write an UPDATE that gives every Engineering employee a 10% raise.

UPDATE employees SET salary = salary * 1.10 WHERE department = 'Engineering'; —WHERE scopes it to the right rows, and salary * 1.10 computes each row's new value from its own current salary.

Why is UPDATE without WHERE considered one of the most dangerous mistakes in SQL?

It's syntactically valid, runs without any warning, and applies to every row in the table in one statement. There's nothing in the language itself that stops it, it's purely a matter of discipline and double-checking before running it.

Summary

This lesson covered changing one or more columns with UPDATE ... SET, and why a WHERE clause is effectively mandatory.

What's Next?

The next lesson covers DELETE, removing rows entirely.

</>

Ready to Practice SQL?

Write and run real SQL queries online, right in your browser, no install or signup required. Pick a playground based on which database you want to practice on.

SQLite / PostgreSQL

Fast, no server, no ads.

Open DB Fiddle ↗

MySQL / SQL Server

Switch databases with one dropdown.

Open SQL Fiddle ↗

Both open a third-party SQL playground in a new tab. External site terms and privacy policy apply.