SQL UPDATE

Article Summary

The UPDATE statement is used to change data in existing rows of a table. πŸ”Ή Basic Syntax ⚠️ Always use a WHERE clause unless you want to update every row. πŸ”Ή Example πŸ”Ή Update All Rows (Be Careful!) πŸ”Ή With Subqueries (Advanced) 🧠 Quick Recap Use Case Example Syntax Update specific row UPDATE table SET […]

The UPDATE statement is used to change data in existing rows of a table.

πŸ”Ή Basic Syntax

UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;

⚠️ Always use a WHERE clause unless you want to update every row.

πŸ”Ή Example

-- Increase salary of employee with ID 101
UPDATE employees
SET salary = salary + 5000
WHERE employee_id = 101;

-- Update department for multiple employees
UPDATE employees
SET department = 'Finance'
WHERE department = 'Accounts';

πŸ”Ή Update All Rows (Be Careful!)

-- Set a default value for all rows
UPDATE employees
SET bonus = 0;

πŸ”Ή With Subqueries (Advanced)

-- Set manager name from another table
UPDATE employees e
SET manager_name = (
  SELECT name FROM managers m WHERE m.id = e.manager_id
)
WHERE EXISTS (
  SELECT 1 FROM managers m WHERE m.id = e.manager_id
);

🧠 Quick Recap

Use CaseExample Syntax
Update specific rowUPDATE table SET col = val WHERE ...
Update multiple rowsUse conditions in WHERE clause
No WHERE = all rowsUpdates all rows (⚠️ risky!)
Use expressionsSET col = col + 1000
Use subqueriesSET col = (SELECT ...)

βœ… Use UPDATE to modify your data precisely

Was this helpful?