Lesson 10: Updating Data

20 min read Beginner Lesson 10 of 12 7.9k learners

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:

1. Which keyword is used to modify existing records?
2. What clause should you always use with UPDATE to avoid changing all rows?
3. Can an UPDATE statement use expressions?
4. What happens if you omit WHERE in an UPDATE?
5. Which clause allows setting values based on another query?