SQL UPDATE
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 Case | Example Syntax |
|---|---|
| Update specific row | UPDATE table SET col = val WHERE ... |
| Update multiple rows | Use conditions in WHERE clause |
| No WHERE = all rows | Updates all rows (β οΈ risky!) |
| Use expressions | SET col = col + 1000 |
| Use subqueries | SET col = (SELECT ...) |
β
Use UPDATE to modify your data precisely
Was this helpful?
Thanks for your feedback!