SQL UPDATE statement
SQL

SQL UPDATE Statement: Modify Existing Rows Safely (Examples)

SQL Tutorial · Chapter 26 of 32

The SQL UPDATE statement changes the values in existing rows of a table. You name the table, list the columns and their new values after SET, and use WHERE to say which rows to change. UPDATE without WHERE modifies every row, so the golden rule is: write the SELECT with the same WHERE first, check the rows, then run the UPDATE.

SQL UPDATE Syntax

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

Several columns can be changed in one statement, separated by commas. The new value can be a constant, an expression using the row’s current values, or a subquery.

Sample Table (Before)

Orders
OrderID CustomerID Product Amount OrderDate
101 1 Laptop 1200 2026-01-05
102 2 Mouse 25 2026-01-07
103 1 Monitor 300 2026-01-12
104 3 Keyboard 45 2026-02-02
105 6 Headset 80 2026-02-15
106 4 Laptop 1150 2026-03-01

Example 1: Update One Column in One Row

UPDATE Orders
SET Amount = 30
WHERE OrderID = 102;

SELECT OrderID, Product, Amount FROM Orders WHERE OrderID = 102;
OrderID Product Amount
102 Mouse 30

Filtering on the primary key guarantees exactly one row changes.

Example 2: Update Several Columns

UPDATE Orders
SET Product = 'Gaming Laptop', Amount = 1300
WHERE OrderID = 106;

SELECT OrderID, Product, Amount FROM Orders WHERE OrderID = 106;
OrderID Product Amount
106 Gaming Laptop 1300

Example 3: Update Many Rows with a Calculation

Apply a 10% price increase to every laptop. The expression uses each row’s own current Amount.

UPDATE Orders
SET Amount = Amount * 1.10
WHERE Product LIKE '%Laptop%';

SELECT OrderID, Product, Amount FROM Orders WHERE Product LIKE '%Laptop%';
OrderID Product Amount
101 Laptop 1320
106 Gaming Laptop 1430

Example 4: Update Using a Subquery

Set the Country of every customer who has placed an order over 1000 to a VIP marker in a Segment column. The subquery finds the customer IDs.

UPDATE Customers
SET Segment = 'VIP'
WHERE CustomerID IN (SELECT CustomerID FROM Orders WHERE Amount > 1000);

SELECT CustomerID, Name, Segment FROM Customers WHERE Segment = 'VIP';
CustomerID Name Segment
1 Anita Sharma VIP
4 Wei Chen VIP

Example 5: Update from Another Table (Join)

Copy each customer’s Country into a new Orders column called ShipCountry. The syntax differs by database; the MySQL form is shown, with the others below.

-- MySQL
UPDATE Orders o
JOIN Customers c ON c.CustomerID = o.CustomerID
SET o.ShipCountry = c.Country;

-- SQL Server
UPDATE o SET o.ShipCountry = c.Country
FROM Orders o JOIN Customers c ON c.CustomerID = o.CustomerID;

-- PostgreSQL / SQLite
UPDATE Orders SET ShipCountry = c.Country
FROM Customers c WHERE c.CustomerID = Orders.CustomerID;
OrderID CustomerID ShipCountry
101 1 India
102 2 UK
103 1 India
104 3 Spain
105 6 NULL
106 4 Singapore

Order 105 stays NULL because customer 6 has no row to copy from.

Safety Checklist

  1. Run SELECT * FROM Orders WHERE ... with the exact WHERE clause and confirm the row count.
  2. Wrap the change in a transaction (BEGIN; UPDATE ...; check ; COMMIT; or ROLLBACK;).
  3. Check the “rows affected” message the database returns.
  4. On production data, back up first or use a WHERE on the primary key.

Works in MySQL, SQL Server, PostgreSQL, SQLite

  • Simple UPDATE … SET … WHERE is identical in all four.
  • Multi-table UPDATE syntax differs, as shown in Example 5. SQLite supports UPDATE … FROM since version 3.33.
  • MySQL Workbench runs in “safe update mode” by default and rejects an UPDATE without a key in WHERE; a useful guard rail.
  • PostgreSQL and SQLite support RETURNING * to show the changed rows immediately.

Common Mistakes

  • Forgetting WHERE and updating every row in the table.
  • Writing SET Amount = 30 AND Product = 'Mouse'; multiple columns are separated by commas, not AND.
  • Updating a text column with an unquoted value.
  • Violating a constraint, e.g. setting a foreign key to a value that does not exist.

Practice Exercise

  1. Change the OrderDate of order 104 to 2026-02-03.
  2. Reduce every Amount below 50 by 5.
Show answers
-- 1
UPDATE Orders SET OrderDate = '2026-02-03' WHERE OrderID = 104;

-- 2 (check first: SELECT * FROM Orders WHERE Amount < 50;)
UPDATE Orders SET Amount = Amount - 5 WHERE Amount < 50;
-- affects orders 102 and 104

Related Chapters

FAQ

What happens if I run UPDATE without a WHERE clause?

Every row in the table receives the new value. There is no confirmation prompt, so always test the WHERE clause with a SELECT first.

Can I update multiple columns in one statement?

Yes. List each column = value pair after SET, separated by commas: SET Product = ‘X’, Amount = 100.

How do I undo an UPDATE?

Only if it ran inside a transaction that has not been committed: issue ROLLBACK. Once committed, you need a backup or a reverse UPDATE with the old values.

Chapter 26 of 32 · SQL from Scratch: all 32 chapters

PK
Meet PK, the founder of NeotechNavigators.com! With over 15 years of experience in Data Visualization, Excel Automation, and dashboard creation. PK is a Microsoft Certified Professional who has a passion for all things in Excel. PK loves to explore new and innovative ways to use Excel and is always eager to share his knowledge with others. With an eye for detail and a commitment to excellence, PK has become a go-to expert in the world of Excel. Whether you're looking to create stunning visualizations or streamline your workflow with automation, PK has the skills and expertise to help you succeed. Join the many satisfied clients who have benefited from PK's services and see how he can take your data analysis skills to the next level!
https://neotechnavigators.com