Lesson 10: Updating Data
Introduction
The UPDATE
statement is used to modify existing records in a table. You can update one row, multiple rows, or even all rows depending on the WHERE
clause.
Updating a Single Row
Use UPDATE
with WHERE
to modify a specific row.
SQL
UPDATE customers
SET email = 'new.email@email.com'
WHERE customer_id = 3;
Updating Multiple Rows
If the WHERE
condition matches multiple rows, all of them will be updated.
SQL
UPDATE customers
SET city = 'New York'
WHERE city = 'Boston';
Updating with Expressions
You can use expressions in SET
to update values dynamically.
SQL
UPDATE employees
SET salary = salary * 1.1
WHERE department = 'Sales';
Updating with Subqueries
You can set a column value based on a subquery result.
SQL
UPDATE employees
SET department_id = (
SELECT department_id
FROM departments
WHERE name = 'HR'
)
WHERE employee_id = 10;
Practice Quiz
Test your understanding of UPDATE statements: