Updating Data in PostUpdating Data in PostgreSQL (UPDATE)

Syntax

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

Suppose we have this table employees:

idnameemailhire_datesalary
1Alice Johnsonalice.johnson@example.com2023-01-1560000
2Bob Smithbob.smith@example.com2021-08-2255000
3Carol Leecarol.lee@example.com2024-03-1070000

Example 1: Update salary for one employee

UPDATE employees
SET salary = 65000
WHERE id = 2;

Note: This command does not produce output.

After the update, the table will look like this:

idnameemailhire_datesalary
1Alice Johnsonalice.johnson@example.com2023-01-1560000
2Bob Smithbob.smith@example.com2021-08-2265000
3Carol Leecarol.lee@example.com2024-03-1070000

Example 2: Update email for multiple employees

UPDATE employees
SET email = LOWER(email)
WHERE hire_date < '2023-01-01';

Note: This command does not produce output.

After the update, the table will look like this:

idnameemailhire_datesalary
1Alice Johnsonalice.johnson@example.com2023-01-1560000
2Bob Smithbob.smith@example.com2021-08-2265000
3Carol Leecarol.lee@example.com2024-03-1070000

Important Notes:

  • The UPDATE statement itself returns no data output, only a command status.
  • To see the changes, run a SELECT query after the update.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top