UPDATE

Updated · 8 min read
easyPostgreSQL 18sqlupdatesetwherechanging-datasafety

UPDATE changes values in rows that already exist. The SQL Basics fee goes up, a payment's method finally gets recorded, Navika completes JavaScript Basics: each is an UPDATE. The statement is short, and that's the danger: the WHERE clause decides which rows change, and leaving it out changes every row. This lesson covers the syntax, updating one or many columns, updating from a calculation, and a habit that prevents the most famous SQL accident. Every Try it run starts from a fresh copy of the Fee Tracker, so you can't damage anything here.

In One Minute

  • UPDATE table SET column = value WHERE condition; changes the matching rows.
  • SET can change several columns at once, separated by commas.
  • The new value can be a calculation using the old one: SET fee_usd = fee_usd * 1.05.
  • Without WHERE, every row changes. Run a SELECT with the same WHERE first, and check how many rows it returns.
  • PostgreSQL reports UPDATE n, the number of rows changed; RETURNING shows them.

The Basics

SQL
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
PartWhat it does
UPDATE table_nameThe table whose rows change
SET column = valueThe new value for each listed column; other columns are left alone
WHERE conditionWhich rows change. Only rows where the condition is true are updated

The SQL Basics fee goes up from $400.00 to $425.00:

SQL▶ Try it
UPDATE courses
SET fee_usd = 425.00
WHERE course_id = 4;
 
SELECT course_id, title, fee_usd
FROM courses
WHERE course_id = 4;
Output
UPDATE 1
 
 course_id |   title    | fee_usd
-----------+------------+---------
         4 | SQL Basics |  425.00
(1 row)

UPDATE 1 means one row changed. The SELECT afterwards confirms the new fee. With RETURNING (PostgreSQL only, like in INSERT INTO), the UPDATE can show the changed rows itself:

SQL▶ Try it
UPDATE courses
SET fee_usd = 425.00
WHERE course_id = 4
RETURNING course_id, title, fee_usd;
Output
 course_id |   title    | fee_usd
-----------+------------+---------
         4 | SQL Basics |  425.00
(1 row)
WHERE picks the row; SET changes only the listed column

Several Columns at Once

List several column = value pairs after SET, separated by commas. Payment 5 had no method recorded; the accountant learns it was paid in cash:

SQL▶ Try it
UPDATE payments
SET method = 'cash'
WHERE payment_id = 5
RETURNING payment_id, amount_usd, method;
Output
 payment_id | amount_usd | method
------------+------------+--------
          5 |     200.00 | cash
(1 row)

And here two columns change in one statement: Navika's enrollment in JavaScript Basics is marked completed, and its enrollment date is corrected:

SQL▶ Try it
UPDATE enrollments
SET status = 'completed', enrolled_on = '2025-11-11'
WHERE student_id = 103 AND course_id = 5
RETURNING *;
Output
 student_id | course_id | enrolled_on |  status
------------+-----------+-------------+-----------
        103 |         5 | 2025-11-11  | completed
(1 row)

The WHERE uses both parts of the enrollment's key, because the pair student_id and course_id identifies one enrollment.

Updating from a Calculation

The new value can be worked out from the old one. Every Java course goes up 5%:

SQL▶ Try it
UPDATE courses
SET fee_usd = fee_usd * 1.05
WHERE technology = 'Java'
RETURNING title, fee_usd;
Output
      title       | fee_usd
------------------+---------
 Java Basics      |  472.50
 Java OOP         |  525.00
 Java Collections |  577.50
(3 rows)

fee_usd on the right of = means the value before the update, for each row. Notice the results have two decimals: 450.00 * 1.05 is 472.5000, and the column's numeric(8,2) type stores it rounded to cents, as you saw in Basic Data Types. Writing round(fee_usd * 1.05, 2) makes that rounding visible in the statement itself.

Each row's new fee is worked out from its own old fee

The Missing-WHERE Disaster

An UPDATE without WHERE changes every row in the table, and it runs without a warning:

SQL▶ Try it
UPDATE courses
SET fee_usd = 999.00;
 
SELECT title, fee_usd
FROM courses
ORDER BY course_id;
Output
UPDATE 6
 
       title       | fee_usd
-------------------+---------
 Java Basics       |  999.00
 Java OOP          |  999.00
 Java Collections  |  999.00
 SQL Basics        |  999.00
 JavaScript Basics |  999.00
 Git Basics        |  999.00
(6 rows)

UPDATE 6: every course now costs $999.00. In Try it, the next run brings the real fees back; on a real database, they're gone unless you have a backup or the change is inside a transaction you can undo.

Here's the habit in practice. First look:

SQL▶ Try it
SELECT course_id, title, fee_usd
FROM courses
WHERE technology = 'Java'
ORDER BY course_id;
Output
 course_id |      title       | fee_usd
-----------+------------------+---------
         1 | Java Basics      |  450.00
         2 | Java OOP         |  500.00
         3 | Java Collections |  550.00
(3 rows)

Three rows, the ones you meant. Now the same WHERE goes into the UPDATE, and UPDATE 3 confirms the count matches.

An Undo Button: Transactions

A transaction groups statements so you can keep them all or undo them all. BEGIN starts one; ROLLBACK throws away every change since BEGIN, and COMMIT keeps them. Here the disastrous update is undone:

SQL▶ Try it
BEGIN;
 
UPDATE courses
SET fee_usd = 999.00;
 
ROLLBACK;
 
SELECT title, fee_usd
FROM courses
ORDER BY course_id
LIMIT 2;
Output
BEGIN
 
UPDATE 6
 
ROLLBACK
 
    title    | fee_usd
-------------+---------
 Java Basics |  450.00
 Java OOP    |  500.00
(2 rows)

UPDATE 6 happened, then ROLLBACK reversed it, and the fees are back. On a real database, start risky changes with BEGIN, check the result, and only then COMMIT. Transactions get a full lesson later, Transactions.

SELECT first, then BEGIN, UPDATE, check, and COMMIT or ROLLBACK

Rules Still Apply

An UPDATE must keep every rule on the table, just like an INSERT. A fee must be greater than zero:

SQL▶ Try it
UPDATE courses
SET fee_usd = 0
WHERE course_id = 6;
Output
ERROR:  new row for relation "courses" violates check constraint "courses_fee_usd_check"
DETAIL:  Failing row contains (6, Git Basics, Git, Beginner, 0.00, null).

The row isn't changed. And an UPDATE whose WHERE matches nothing isn't an error: it reports UPDATE 0, which is worth noticing when you expected a change.

Common Mistakes

Forgetting WHERE. Every row changes. Run the SELECT first, and use a transaction for anything risky.

A WHERE that matches more than you think. WHERE first_name = 'Navika' would also match a second Navika one day. Update by the primary key when you mean one row.

Separating SET pairs with AND. SET status = 'completed' AND enrolled_on = '2025-11-11' is read as one value for status: the whole expression 'completed' AND enrolled_on = ..., a true/false test. PostgreSQL fails with invalid input syntax for type boolean: "completed". Use commas between pairs.

Missing an UPDATE 0. If the WHERE matched nothing, nothing changed. Check the count.

Practice

Challenge: Advitha's SQL Basics enrollment is finished. Mark it completed, choosing the row by its full key, and use RETURNING to show the changed row. Before you write the UPDATE, write the SELECT with the same WHERE and confirm it returns one row.

Show solution
SQL▶ Try it
SELECT student_id, course_id, status
FROM enrollments
WHERE student_id = 102 AND course_id = 4;
 
UPDATE enrollments
SET status = 'completed'
WHERE student_id = 102 AND course_id = 4
RETURNING student_id, course_id, status;
Output
 student_id | course_id | status
------------+-----------+--------
        102 |         4 | active
(1 row)
 
 student_id | course_id |  status
------------+-----------+-----------
        102 |         4 | completed
(1 row)

The SELECT returned exactly one row, so the same WHERE was safe to use in the UPDATE.

Quick check: UPDATE

3 questions
1What does UPDATE courses SET fee_usd = 999.00; do?
2Which is the safest routine before an UPDATE?
3In SET fee_usd = fee_usd * 1.05, what does fee_usd on the right mean?

Key Takeaways

You want toWrite
Change one rowUPDATE courses SET fee_usd = 425.00 WHERE course_id = 4;
Change several columnsSET status = 'completed', enrolled_on = '2025-11-11'
Use the old valueSET fee_usd = fee_usd * 1.05
See what changed... RETURNING *;
Be able to undoBEGIN; UPDATE ...; ROLLBACK; (or COMMIT;)

Was this tutorial helpful?

Lesson 22 of 115