Concept Flow - UPDATE without WHERE (danger)
Start UPDATE statement
Check for WHERE clause
Finish
The UPDATE command checks if there is a WHERE clause. If yes, it updates only matching rows. Without WHERE, it updates every row in the table.
UPDATE employees SET salary = salary * 1.1;
| Step | Action | Rows Affected | Table State Change |
|---|---|---|---|
| 1 | Start UPDATE employees SET salary = salary * 1.1 | 0 | No change yet |
| 2 | Check for WHERE clause | N/A | No WHERE clause found |
| 3 | Apply salary = salary * 1.1 to ALL rows | 5 | All 5 employee salaries increased by 10% |
| 4 | Finish UPDATE | 5 | Table updated with new salaries for all rows |
| Variable | Start | After Update | Final |
|---|---|---|---|
| salary (per employee) | Original salaries | Increased by 10% | Updated salaries |
UPDATE table_name SET column = value Without WHERE, all rows are updated. Always use WHERE to limit updates. Missing WHERE can cause unintended data changes. Check your query before running UPDATE.
What happens if you run an UPDATE statement without a WHERE clause?
WHERE clause limits which rows are updated. Without it, no filter is applied.WHERE, the update affects every row in the table.Which of the following is the correct syntax to update the salary column to 5000 for all employees in the employees table?
UPDATE employees SET salary = 5000;
UPDATE table_name SET column = value; to update all rows.Given the table products with 3 rows:
id | name | price ---+---------+------- 1 | Pen | 10 2 | Pencil | 5 3 | Eraser | 3
What will be the result after running this query?
UPDATE products SET price = price + 2;
WHERE clause, the update applies to all rows in the table.Consider this query intended to update only employees in department 10:
UPDATE employees SET salary = salary + 1000;
Why might this be a problem?
WHERE clause to filter employees by department.You want to increase the stock by 10 only for products with stock less than 5. Which query correctly avoids the danger of updating all rows?
stock < 5, so a WHERE clause is needed.UPDATE products SET stock = stock + 10 WHERE stock < 5; uses correct syntax and condition to update only matching rows. UPDATE products SET stock = stock + 10; (no WHERE) updates all rows. UPDATE products stock = stock + 10 WHERE stock < 5; (missing SET) has syntax error. UPDATE products SET stock = stock + 10 WHERE stock > 5; updates wrong rows (opposite condition).Pass the test and this pattern moves unknown