SQL — Update
Basic UPDATE
UPDATE users
SET age = 31
WHERE name = 'Alice';
Multiple columns
UPDATE users
SET age = 31, email = 'alice_new@example.com'
WHERE name = 'Alice';
UPDATE with conditions
UPDATE users
SET status = 'inactive'
WHERE last_login < '2025-01-01';
UPDATE with JOIN
-- MySQL
UPDATE users u
JOIN orders o ON u.id = o.user_id
SET u.last_order = o.order_date
WHERE o.total > 100;
-- PostgreSQL
UPDATE users
SET last_order = orders.order_date
FROM orders
WHERE users.id = orders.user_id
AND orders.total > 100;
UPDATE with subquery
UPDATE users
SET status = 'premium'
WHERE id IN (
SELECT user_id FROM orders WHERE total > 1000
);
UPDATE with default
UPDATE users
SET phone = DEFAULT
WHERE id = 1;
Returning updated data
-- PostgreSQL
UPDATE users SET age = 31
WHERE name = 'Alice'
RETURNING id, name, age;
Mini Practice
Write SQL code that:
- Updates a single column
- Updates multiple columns
- Uses a WHERE condition
- Updates with a subquery
Up Next
In the next lesson, you'll learn about Delete — removing data.
Related Topics
Frequently Asked Questions about Update
What is Update in SQL?
Update is a fundamental concept in SQL. This lesson explains it step by step with clear examples, making it easy for beginners to understand.
How do I learn Update?
Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Update.
Why is Update important in SQL?
Update is essential for SQL development. Understanding this concept will help you write better code and solve real-world problems more effectively.