UPDATE Statement in SQL
SQL UPDATE statement tutorial for beginners
The UPDATE statement in SQL is used to modify existing records in a table. It is one of the essential operations in any database system, allowing you to change stored data safely and efficiently.
In this beginner-friendly SQL tutorial, you will learn:
What UPDATE does
UPDATE syntax
Updating single and multiple columns
Updating multiple rows
Using WHERE clause with UPDATE
Updating with conditions
Real-world examples
Best practices
The UPDATE statement modifies existing rows in a table.
β If you do not use WHERE, all rows in the table will be updated!
UPDATE table_name
SET column1 = value1, column2 = value2
WHERE condition;
| id | name | city | |
|---|---|---|---|
| 1 | Amit | amit@example.com | Delhi |
| 2 | Neha | neha@example.com | Mumbai |
| 3 | Rahul | rahul@example.com | Bengaluru |
Change Neha's city:
UPDATE users
SET city = 'Pune'
WHERE id = 2;
UPDATE users
SET name = 'Rahul Sharma', city = 'Hyderabad'
WHERE id = 3;
Update city for all users living in Delhi:
UPDATE users
SET city = 'Noida'
WHERE city = 'Delhi';
UPDATE users
SET city = 'Unknown';
This will update all rows in the table.
Increase salary by 10%:
UPDATE employees
SET salary = salary * 1.10;
Copy value from another field:
UPDATE products
SET final_price = base_price - discount;
Update product prices to match the latest prices:
UPDATE products p
SET p.price = (
SELECT new_price
FROM latest_prices lp
WHERE lp.product_id = p.id
)
WHERE p.id IN (SELECT product_id FROM latest_prices);
UPDATE users
SET name = 'John', city = 'Chennai'
WHERE id = 10;
UPDATE orders
SET status = 'Shipped'
WHERE order_id = 1023;
UPDATE products
SET stock = stock - 1
WHERE id = 50;
Missing WHERE clause → updates whole table.
Fix: Always double-check WHERE.
UPDATE users SET age = 'abc'; -- wrong
Fix: Use correct types.
Using wrong conditions results in 0 rows updated.
β Always use a WHERE clause (unless updating whole table intentionally)
β Use transactions for bulk updates
β Backup critical tables before major updates
β Test with SELECT before UPDATE
β Log changes for auditing
β Use LIMIT (MySQL) for safer updates
In this SQL UPDATE tutorial, you learned:
What UPDATE does
How to update single/multiple columns
How to update multiple rows
Real-world update scenarios
Best practices to avoid mistakes
UPDATE is essential for modifying data in any SQL-based application.