In this PostgreSQL post explains how to use the PostgreSQL OR condition with syntax and examples.
The PostgreSQL OR condition is used to test two or more conditions where records are returned when any one of the conditions are met. It can be used in a SELECT, INSERT, UPDATE, or DELETE statement.
The syntax for the OR condition in PostgreSQL is:
condition1, condition2, condition_n are any of the conditions that must be met for the records to be selected.
The first PostgreSQL OR condition example that we'll take a look at involves a SELECT statement with 2 conditions:
This PostgreSQL OR condition example would return all products whose product_type is 'Hardware' or have a product_id greater than 400. Because the * is used in the SELECT statement, all fields from the products table would appear in the result set.
The next PostgreSQL OR example looks at a SELECT statement with 3 conditions. If any of these conditions is met, the record will be included in the result set.
This PostgreSQL OR condition example would return all product_id and product_name values from the products table where the product_type is 'Hardware', the product_type is 'Software', r the product_id is greater than 1000.
The PostgreSQL OR condition can be used in the INSERT statement.
For example:
This PostgreSQL OR example would insert into the products table, all inventory_id and product_name records from the inventory table whose quantity is greater than 0 or product_name is 'Memory'.
The PostgreSQL OR condition can be used in the UPDATE statement.
For example:
This PostgreSQL OR condition example would update all product_type values in the products table to 'Hardware' where the product_name is 'Memory' or the product_name is 'SSD'.
The PostgreSQL OR condition can be used in the DELETE statement.
For example:
This PostgreSQL OR condition example would delete all contacts from the contacts table whose last_name was 'Mark' or first_name was 'Sarah'.