Javatpoint Logo
Javatpoint Logo

PostgreSQL OR Condition

In this section, we are going to understand the working of PostgreSQL OR Condition, which specifies that if we take two or more conditions, then one of the conditions must be satisfied to get the resultant records. We will also see the examples of OR Condition with different queries such as INSERT, SELECT, UPDATE, and DELETE.

Introduction of PostgreSQL OR Condition

In PostgreSQL, the OR condition can be combined with the SELECT, INSERT, UPDATE, and DELETE commands to select unique data from various columns in a table. In other words, we can say that the OR condition is used to return the data if any of the conditions divided by OR is TRUE.

NOTE: The PostgreSQL OR conditions allow us to return two or more conditions. It requires any of the conditions that could be condition1, condition2 ...conditionN in the data and involved in the output.

PostgreSQL OR Condition Syntax

The syntax of the PostgreSQL OR condition is as follows:

In the above syntax, we have used the following parameter:

Parameter Description
condition1, condition2, ... condition_n: It specifies the conditions that are evaluated to determine when the records are selected.

Examples of PostgreSQL OR Condition

Let us see different examples to understand how the PostgreSQL OR Condition works.

Example of PostgreSQL OR with SELECT Command

In the below example, we will see the use of OR condition with the SELECT command with two conditions.

For this, we are taking the employee table from the Javatpoint database to all the records from the table.

We are using the OR condition with WHERE clause as we can see the following command:

Output

After executing the above command, we will get the following output:

PostgreSQL OR Condition

As we can see in the above screenshot, the PostgreSQL OR condition will return those employee's information whose location is 'New York' or an emp_id is greater than 8.

Example of PostgreSQL OR with SELECT Command (two or more conditions)

Let us see one more example of SELECT command with OR condition. For this, we are taking the car table from the Javatpoint database.

The following command is a more complex query as compared to the above statement:

Output

After successfully executing the above command, we will get the following output:

PostgreSQL OR Condition

As we can see in the above screenshot that the PostgreSQL OR condition will return all those records from the car_id, car_name, car_model, car_price, car_color, body_style columns values from the car table who's body_style is 'coupe' OR 'Wagon' OR the car_price is 63890.

Example of PostgreSQL OR with INSERT Command

In the below example, we will display how to use the OR condition with the INSERT command.

For this, we are taking the employee and department table from the Organization database to insert the records from one table to another table.

Output

After executing the above command, we will get the below message window displaying that the value has been inserted successfully.

PostgreSQL OR Condition

To check whether the records have been inserted into the department table or not, we will use the SELECT command as follows:

Output

PostgreSQL OR Condition

We can see in the below screenshot, the PostgreSQL OR condition inserted four records into the department table.

The phone and address columns record from the employee table where the emp_id is less than 6 OR whose employee_name is Emily Smith.

Example of PostgreSQL OR with UPDATE Command

In the below example, we will display how to use the OR condition with the UPDATE command.

For this, we are taking the customer table from the Javatpoint database.

In the following command, the PostgreSQL OR condition is used to update the following values:

The cust_name values to Mike, cust_address to London and cust_age value to 26 in the customer table where cust_id is equal to 103 Or the cust_name is emily.

Output

After executing the above command, we will get the below output, where we can see that the customer table has been updated successfully.

PostgreSQL OR Condition

We will now use the Select command to check whether the particular records have been updated or not in the customer table:

Output

On executing the above command, we will get the below result:

PostgreSQL OR Condition

Example of PostgreSQL OR: with DELETE Command

In the below example, we will see how to use the OR Condition with the DELETE command.

In the below example, we take the department table from Javatpoint Database, deleting the particular records from the table.

In the following command, the PostgreSQL OR Condition is used to delete all the records from the department table where the dept_name was 'RESEARCH', and emp_fname was 'Cena'.

Output

After executing the above command, we will get the below message window displaying that the records have been deleted successfully from the department table.

PostgreSQL OR Condition

We will now use the Select command to check whether the particular records have been deleted or not in the department table:

Output

After successfully executing the above command, we will get the following output:

PostgreSQL OR Condition

Overview

In the PostgreSQL OR Condition section, we have learned the following topics:

  • The use of PostgreSQL OR condition provides the benefits of OR condition in just a single command.
  • We used the OR Condition with the SELECT Command to get the particular table records.
  • We used the OR Condition with INSERT Command to insert the records from one table to another.
  • We used the OR Condition with the UPDATE Statement to update the particular table's records.
  • We used the OR Condition with the DELETE Statement to remove the particular table's records.






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA