0
0
SQLquery~5 mins

UPDATE with WHERE condition in SQL

Choose your learning style9 modes available
Introduction

We use UPDATE with WHERE to change specific data in a table, not everything. It helps us fix or change only the rows we want.

You want to fix a wrong phone number for one customer in your contacts list.
You need to change the status of orders that are shipped to 'delivered'.
You want to update the price of a specific product in your store.
You need to mark certain employees as inactive based on their ID.
You want to correct the address of a single user without affecting others.
Syntax
SQL
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;

The WHERE clause limits which rows get updated.

Without WHERE, all rows in the table will be updated!

Examples
Updates salary only for the employee with ID 101.
SQL
UPDATE employees
SET salary = 5000
WHERE employee_id = 101;
Increases price by 10% for all products in the 'Books' category.
SQL
UPDATE products
SET price = price * 1.1
WHERE category = 'Books';
Marks orders before 2024 as delivered.
SQL
UPDATE orders
SET status = 'delivered'
WHERE order_date < '2024-01-01';
Sample Program

This example creates a users table, adds three users, then updates Bob's email using WHERE to target only his row.

SQL
CREATE TABLE users (
  id INT PRIMARY KEY,
  name VARCHAR(50),
  email VARCHAR(50)
);

INSERT INTO users (id, name, email) VALUES
(1, 'Alice', 'alice@example.com'),
(2, 'Bob', 'bob@example.com'),
(3, 'Charlie', 'charlie@example.com');

UPDATE users
SET email = 'bob.new@example.com'
WHERE id = 2;

SELECT * FROM users ORDER BY id;
OutputSuccess
Important Notes

Always double-check your WHERE condition to avoid unwanted updates.

You can update multiple columns by separating them with commas.

Use ORDER BY in SELECT to see results in a clear order after update.

Summary

UPDATE with WHERE changes only the rows you want.

Without WHERE, all rows get updated--be careful!

You can update one or many columns at once.