Skip to main content
TopMiniSite

Back to all posts

How to Update Data With Case Conditional In Postgresql?

Published on
7 min read
How to Update Data With Case Conditional In Postgresql? image

Best PostgreSQL Resources to Buy in October 2025

1 PostgreSQL: Up and Running: A Practical Guide to the Advanced Open Source Database

PostgreSQL: Up and Running: A Practical Guide to the Advanced Open Source Database

BUY & SAVE
$35.23 $44.99
Save 22%
PostgreSQL: Up and Running: A Practical Guide to the Advanced Open Source Database
2 PostgreSQL 16 Administration Cookbook: Solve real-world Database Administration challenges with 180+ practical recipes and best practices

PostgreSQL 16 Administration Cookbook: Solve real-world Database Administration challenges with 180+ practical recipes and best practices

BUY & SAVE
$34.91 $54.99
Save 37%
PostgreSQL 16 Administration Cookbook: Solve real-world Database Administration challenges with 180+ practical recipes and best practices
3 PostgreSQL DBA (v17, v16, v15, v14, v13) - 2025 2nd Edition: Full PostgreSQL Database Administrator's Guide, Secret DBA skills, High Availability, ... (GitHub link provided) (PostgreSQL 17)

PostgreSQL DBA (v17, v16, v15, v14, v13) - 2025 2nd Edition: Full PostgreSQL Database Administrator's Guide, Secret DBA skills, High Availability, ... (GitHub link provided) (PostgreSQL 17)

BUY & SAVE
$51.32
PostgreSQL DBA (v17, v16, v15, v14, v13) - 2025 2nd Edition: Full PostgreSQL Database Administrator's Guide, Secret DBA skills, High Availability, ... (GitHub link provided) (PostgreSQL 17)
4 PostgreSQL Query Optimization: The Ultimate Guide to Building Efficient Queries

PostgreSQL Query Optimization: The Ultimate Guide to Building Efficient Queries

BUY & SAVE
$43.58 $49.99
Save 13%
PostgreSQL Query Optimization: The Ultimate Guide to Building Efficient Queries
5 Mastering PostgreSQL 15: Advanced techniques to build and manage scalable, reliable, and fault-tolerant database applications

Mastering PostgreSQL 15: Advanced techniques to build and manage scalable, reliable, and fault-tolerant database applications

BUY & SAVE
$35.99 $61.99
Save 42%
Mastering PostgreSQL 15: Advanced techniques to build and manage scalable, reliable, and fault-tolerant database applications
6 Introduction to PostgreSQL for the data professional.

Introduction to PostgreSQL for the data professional.

BUY & SAVE
$24.99
Introduction to PostgreSQL for the data professional.
7 Beginning Databases with PostgreSQL: From Novice to Professional (Beginning From Novice to Professional)

Beginning Databases with PostgreSQL: From Novice to Professional (Beginning From Novice to Professional)

  • AFFORDABLE PRICES FOR QUALITY USED BOOKS IN GREAT SHAPE!
  • ENVIRONMENTALLY FRIENDLY CHOICE: REDUCE WASTE, RECYCLE BOOKS!
  • FAST SHIPPING GUARANTEES QUICK ACCESS TO YOUR NEXT READ!
BUY & SAVE
$41.94 $89.99
Save 53%
Beginning Databases with PostgreSQL: From Novice to Professional (Beginning From Novice to Professional)
8 PostgreSQL Mistakes and How to Avoid Them

PostgreSQL Mistakes and How to Avoid Them

BUY & SAVE
$49.99
PostgreSQL Mistakes and How to Avoid Them
9 Learn PostgreSQL: Use, manage, and build secure and scalable databases with PostgreSQL 16

Learn PostgreSQL: Use, manage, and build secure and scalable databases with PostgreSQL 16

BUY & SAVE
$22.39
Learn PostgreSQL: Use, manage, and build secure and scalable databases with PostgreSQL 16
+
ONE MORE?

To update data with a case conditional in PostgreSQL, you can use the CASE statement within the UPDATE query. The CASE statement allows you to perform different actions based on specific conditions.

Here is an example of how you can update data with a case conditional in PostgreSQL:

UPDATE table_name SET column_name = CASE WHEN condition1 THEN new_value1 WHEN condition2 THEN new_value2 ELSE column_name END WHERE your_condition;

In this query:

  • table_name is the name of the table you want to update.
  • column_name is the name of the column you want to update.
  • condition1, condition2, etc., are the conditions that must be met for the corresponding new_value to be set.
  • new_value1, new_value2, etc., are the values that will be set if the corresponding condition is true.
  • your_condition is an optional additional condition that must be met for the update to occur.

By using the CASE statement in the UPDATE query, you can update data in a flexible and condition-based manner in PostgreSQL.

How to update data in PostgreSQL with a CASE statement that depends on the results of a subquery?

To update data in PostgreSQL with a CASE statement that depends on the results of a subquery, you can use a subquery within the UPDATE statement to retrieve the necessary information to determine the value of the CASE statement. Here's an example of how you can do this:

UPDATE your_table_name SET column_to_update = CASE WHEN (SELECT subquery_column FROM subquery_table WHERE condition) = 'value1' THEN 'new_value1' WHEN (SELECT subquery_column FROM subquery_table WHERE condition) = 'value2' THEN 'new_value2' ... ELSE 'default_value' END WHERE your_condition;

In this example, replace your_table_name with the name of the table you want to update, column_to_update with the name of the column you want to update, and your_condition with the condition that identifies the rows you want to update.

Replace subquery_column with the column from the subquery table that you need to retrieve, subquery_table with the name of the table you are using for the subquery, and condition with the condition to filter the subquery results.

You can have multiple conditions in the CASE statement based on the results of the subquery. The ELSE clause provides a default value to use if none of the conditions are met.

After updating all the required fields in the CASE statement, execute the SQL query in your PostgreSQL database to update the data in the table.

How to update data in PostgreSQL with a CASE statement that checks for ranges of values?

To update data in PostgreSQL with a CASE statement that checks for ranges of values, you can use the following syntax:

UPDATE table_name SET column_name = CASE WHEN column_name >= lower_bound AND column_name < upper_bound THEN new_value ELSE column_name END;

In this syntax:

  • table_name is the name of the table you want to update.
  • column_name is the name of the column you want to update.
  • lower_bound and upper_bound define the range of values you want to check.
  • new_value is the new value you want to set for the column if the condition is met.

For example, if you have a table called employees with a column salary and you want to increase the salary by 10% for employees with salaries between 50000 and 60000, you can use the following query:

UPDATE employees SET salary = CASE WHEN salary >= 50000 AND salary < 60000 THEN salary * 1.10 ELSE salary END;

This query will update the salary column for employees with salaries between 50000 and 60000 by increasing it by 10%, while leaving the salary unchanged for other employees.

How to update data in PostgreSQL with different values based on a condition using CASE?

To update data in PostgreSQL with different values based on a condition using CASE, you can use the following query:

UPDATE table_name SET column_name = CASE WHEN condition1 THEN value1 WHEN condition2 THEN value2 ELSE default_value END WHERE condition;

Here table_name is the name of the table you want to update, column_name is the name of the column you want to update, and condition is the condition that determines which rows will be updated.

You can specify multiple WHEN conditions with corresponding values, and an optional ELSE clause for a default value if none of the conditions are met.

For example, if you want to update a column status in a table employees with different values based on the department of the employee, you can use the following query:

UPDATE employees SET status = CASE WHEN department = 'HR' THEN 'Active' WHEN department = 'IT' THEN 'Inactive' ELSE 'Unknown' END WHERE department IN ('HR', 'IT');

This will update the status column to 'Active' for employees in HR department, 'Inactive' for employees in IT department, and 'Unknown' for employees in other departments.

How to update data in PostgreSQL using a CASE statement with multiple WHEN clauses?

To update data in PostgreSQL using a CASE statement with multiple WHEN clauses, follow these steps:

  1. Connect to your PostgreSQL database using a SQL client or command line tool.
  2. Write a SQL UPDATE statement with a CASE statement inside it. Here's an example:

UPDATE table_name SET column_name = CASE WHEN condition1 THEN value1 WHEN condition2 THEN value2 ... ELSE default_value END WHERE <optional_condition>;

  1. Replace table_name with the name of the table you want to update, column_name with the name of the column you want to update, condition1, condition2, etc. with the conditions you want to check for, value1, value2, etc. with the values you want to set when the corresponding condition is true, and default_value with the value you want to set when none of the conditions are met.
  2. Optionally, you can include a WHERE clause at the end of the statement to specify which rows should be updated. This is useful to limit the scope of the update operation.
  3. Execute the SQL UPDATE statement to update the data in the PostgreSQL table based on the conditions specified in the CASE statement.

That's it! You've successfully updated data in PostgreSQL using a CASE statement with multiple WHEN clauses.

How to update data in PostgreSQL based on multiple conditions using CASE?

To update data in PostgreSQL based on multiple conditions using CASE, you can follow these steps:

  1. First, write a UPDATE statement with the table name you want to update.
  2. Use the CASE statement in the SET clause to specify the conditions for updating the data. Here is an example:

UPDATE table_name SET column_name = CASE WHEN condition_1 THEN value_1 WHEN condition_2 THEN value_2 ELSE default_value END WHERE condition_3;

  1. In the above statement: condition_1, condition_2, etc. represent the conditions for updating the data. value_1, value_2, etc. represent the new values that will be set if the conditions are met. default_value is the value that will be set if none of the conditions are met. condition_3 is an additional condition for updating the data.
  2. Replace table_name, column_name, condition_1, value_1, etc. with your actual table and column names, conditions, and values.
  3. Execute the UPDATE statement to update the data in the table based on the specified conditions.

Here is an example to update the salary of employees in the employees table based on their department:

UPDATE employees SET salary = CASE WHEN department = 'IT' THEN salary * 1.1 WHEN department = 'HR' THEN salary * 1.05 ELSE salary END WHERE hire_date > '2020-01-01';

In this example, the salary of employees in the 'IT' department will be increased by 10%, the salary of employees in the 'HR' department will be increased by 5%, and the salaries of employees in other departments will remain unchanged. The update will only apply to employees hired after '2020-01-01'.