✏️ Modifying DatabeginnerCommonly used

How to update rows in SQL

Modify existing data in one or more rows.

Update a single columnAll databases
UPDATE users
SET country = 'Nigeria'
WHERE id = 42;

Updates one column for the row where id = 42.

Always include a WHERE clause unless you intentionally want to update every row.

Update multiple columnsAll databases
UPDATE users
SET
  name    = 'Alice Smith',
  email   = 'alice.smith@example.com',
  country = 'Kenya'
WHERE id = 42;

Update several columns at once by separating them with commas.

Update with a calculationAll databases
UPDATE products
SET price = price * 1.1
WHERE category = 'electronics';

You can reference the current column value in the SET expression.

When to use: Applying percentage increases, incrementing counters.

Update and return changed rows (PostgreSQL)PostgreSQL
UPDATE users
SET active = false
WHERE last_login < NOW() - INTERVAL '1 year'
RETURNING id, name, email;

Returns the updated rows. Useful to know exactly what changed.

Update from another table (PostgreSQL)PostgreSQL
UPDATE orders o
SET status = 'shipped'
FROM shipments s
WHERE o.id = s.order_id
  AND s.shipped_at IS NOT NULL;

Updates rows in orders based on data from the shipments table.

Common Mistakes

  • Forgetting the WHERE clause — updates EVERY row in the table
  • Test your WHERE condition with SELECT first before running UPDATE
← Back to SQL Reference