Skip to content

PostgreSQL UPDATE

Overview

Overview

The UPDATE statement modifies existing records in a table. This chapter covers how to update single or multiple columns and use conditions.

Update Single Column

Update Single Column

sql
UPDATE users SET status = 'active' WHERE id = 1;

Update Multiple Columns

Update Multiple Columns

sql
UPDATE users SET 
    first_name = 'John',
    last_name = 'Doe',
    email = 'john.doe@example.com'
WHERE id = 1;

UPDATE with WHERE

UPDATE with WHERE

sql
-- Update specific row
UPDATE users SET status = 'inactive' WHERE id = 5;

-- Update multiple rows
UPDATE products SET price = price * 1.1 WHERE category = 'Electronics';

-- Update with IN
UPDATE users SET status = 'vip' WHERE id IN (1, 2, 3, 4, 5);

UPDATE with RETURNING

UPDATE with RETURNING

sql
UPDATE users SET last_login = NOW() WHERE id = 1
RETURNING id, name, last_login;

UPDATE with Subquery

UPDATE with Subquery

sql
-- Update based on subquery
UPDATE products SET price = (
    SELECT AVG(price) FROM products WHERE category_id = products.category_id
) WHERE price IS NULL;

PostgreSQL UPDATE

UPDATE statement:

  • Modify existing records
  • Use WHERE to filter rows
  • Update single or multiple columns
  • RETURNING to get updated values

Content is for learning and research only.