Introduction
We use UPDATE to change data in a table. Updating a single column means changing just one piece of information for some rows.
We use UPDATE to change data in a table. Updating a single column means changing just one piece of information for some rows.
UPDATE table_name SET column_name = new_value WHERE condition;
The WHERE clause decides which rows get updated. Without it, all rows change!
You only change one column by listing just that column in SET.
UPDATE employees SET salary = 5000 WHERE employee_id = 3;
UPDATE products SET price = 19.99 WHERE product_name = 'Notebook';
UPDATE tasks SET status = 'done' WHERE task_id = 10;
This creates a users table, adds three users, then updates Bob's email address. Finally, it shows all users to see the change.
CREATE TABLE users ( id INT, name VARCHAR(50), email VARCHAR(100) ); 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;
Always use WHERE to avoid updating every row by mistake.
You can update multiple rows if the WHERE condition matches more than one.
UPDATE changes data in a table.
You can update just one column by specifying it in SET.
Use WHERE to pick which rows to update.
UPDATE employees SET salary = 5000 WHERE id = 3; do?price column to 20 in the products table for product with id 10?students with columns id, name, and grade, what will be the result of this query?UPDATE students SET grade = 90 WHERE name = 'Alice';
id | name | grade 1 | Alice | 85 2 | Bob | 88 3 | Alice | 92
UPDATE orders SET status 'shipped' WHERE order_id = 101;
inventory with columns item_id, quantity, and price. You want to increase the price by 10% only for items with quantity less than 5. Which SQL statement correctly updates the price column?Pass the test and this pattern moves unknown