Introduction
The UPDATE statement modifies the values of existing rows in a table. Unlike INSERT, which creates new records, UPDATE changes data that is already there, such as correcting a customer's address, applying a salary raise, or marking an order as shipped. Because UPDATE can silently rewrite large amounts of data, it must be used carefully and always paired with a precise filter.
Cricket analogy: UPDATE is like correcting a scorer's error on an existing scorecard entry -- fixing Rohit Sharma's recorded runs from 98 to 99 -- rather than adding a brand-new innings row, and it must target only that one entry precisely.
Syntax
UPDATE table_name
SET column1 = value1,
column2 = value2
WHERE condition;
-- Example: give a specific employee a raise
UPDATE employees
SET salary = salary * 1.10
WHERE employee_id = 104;
-- Example: update multiple columns based on a condition
UPDATE orders
SET status = 'shipped',
shipped_date = CURRENT_DATE
WHERE order_id = 5001;Explanation
The SET clause lists one or more column = expression pairs; the expression can reference the current value of the row itself, as in salary * 1.10, letting you compute new values relative to old ones. The WHERE clause determines which rows are affected. Every row that satisfies the WHERE condition is updated, and every row that does not is left untouched. If the WHERE clause is omitted entirely, the database updates every single row in the table, which is almost never the intended behavior.
Cricket analogy: SET can boost every batsman's rating by referencing their current rating (rating * 1.10), and WHERE narrows it to only players in the top order -- forgetting WHERE would inflate every player in the entire squad database, even the twelfth man.
Always double-check your WHERE clause before running an UPDATE. Running UPDATE without a WHERE clause changes every row in the table, and in production systems this can silently corrupt data with no built-in undo. A safe habit is to first run the equivalent SELECT with the same WHERE condition to confirm exactly which rows will be affected before switching it to an UPDATE.
Example
-- Preview affected rows first
SELECT employee_id, department, salary FROM employees WHERE department = 'Sales';
-- Apply a 5% raise to everyone in Sales
UPDATE employees
SET salary = salary * 1.05
WHERE department = 'Sales';
SELECT employee_id, department, salary FROM employees WHERE department = 'Sales';Output
Only rows where department equals 'Sales' have their salary increased by 5%; rows in other departments are untouched. The database typically reports the number of rows modified, e.g. '2 rows updated', so you can verify the change matched your expectation before committing.
Cricket analogy: Only the top-order batsmen's strike-rate bonus was raised by 5%, tail-enders untouched, and the scoring system reports "3 rows updated," letting the analyst confirm exactly three players were affected before finalizing the report.
Key Takeaways
- UPDATE changes existing rows; it never creates new ones.
- The SET clause can reference a column's current value to compute the new value, such as salary * 1.10.
- The WHERE clause controls exactly which rows are affected; omitting it updates every row in the table.
- Previewing the target rows with an equivalent SELECT before running UPDATE is a strong safety habit.
- Wrapping an UPDATE in a transaction lets you roll back if the result is not what you expected, on engines that support transactional DDL/DML.
Practice what you learned
1. What is the primary purpose of the UPDATE statement?
2. What happens if an UPDATE statement is run without a WHERE clause?
3. In `UPDATE employees SET salary = salary * 1.10 WHERE employee_id = 104;`, what does `salary * 1.10` refer to?
4. What is a recommended safety practice before executing an UPDATE with a complex WHERE clause?
Was this page helpful?
You May Also Like
The INSERT Statement
Learn how to add new rows to a table using single-row, multi-row, and INSERT...SELECT forms.
DELETE and TRUNCATE
Compare the DELETE and TRUNCATE commands for removing rows, including logging, rollback, and identity reset behavior.
Transactions and COMMIT/ROLLBACK
How to group SQL statements into transactions and use COMMIT and ROLLBACK to make or undo their effects.