PostgreSQL OR ConditionIn 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 ConditionIn 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 SyntaxThe syntax of the PostgreSQL OR condition is as follows: In the above syntax, we have used the following parameter:
Examples of PostgreSQL OR ConditionLet us see different examples to understand how the PostgreSQL OR Condition works. Example of PostgreSQL OR with SELECT CommandIn 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: 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: 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 CommandIn 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. To check whether the records have been inserted into the department table or not, we will use the SELECT command as follows: Output 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 CommandIn 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. 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: Example of PostgreSQL OR: with DELETE CommandIn 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. 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: OverviewIn the PostgreSQL OR Condition section, we have learned the following topics:
Next TopicPostgreSQL AND & OR condition |