PostgreSQL AND & OR ConditionIn this section, we are going to understand the working of PostgreSQL AND & OR Condition, which provides the advantages of AND & OR Condition both in just a single command. Here, we will also see the examples of AND & OR operator with different statements, such as INSERT, SELECT, UPDATE, and DELETE. Introduction of PostgreSQL AND & OR ConditionIn PostgreSQL, the AND & OR Condition can be combined with the INSERT, SELECT, UPDATE and DELETE commands. While combining these conditions, we must be alert of where to use the parentheses so that the database knows the order to assess every condition. The PostgreSQL AND & OR conditions allow us to test several conditions. And we never forget the order of operation round brackets. PostgreSQL AND & OR Condition SyntaxThe syntax of the PostgreSQL AND & OR condition are as follows: In the above syntax, we have used the following parameters:
Examples of PostgreSQL AND & OR ConditionLet us see different examples to understand how the PostgreSQL AND & OR Condition works. Example of PostgreSQL AND & OR with INSERT CommandIn the below example, we will display how to use the AND & 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 into the department table. To check whether the records have been inserted into the department table or not, we need to use the SELECT command as follows: Output As we can see in the below screenshot, the PostgreSQL AND & OR condition inserted the records into the department table. All phone and address records from the employee table whose employee_name is either 'Nia Davis OR Kat Taylor' AND the emp_id is less than 5. Example of PostgreSQL AND & OR with SELECT CommandIn the below example, we will display how to use the AND & OR condition with the SELECT command. For this, we are taking the Customer table from the Javatpoint database to all the records from the table. We are using the AND & OR condition with WHERE clause as we can see the following command: Output After executing the above command, we will get the following output: In the above example, we can see that the AND & OR condition will return all those customers who live in Florida, AND Cust_name is 'Harvey', OR all customers whose cust_age is greater than or equal to 26. And the parentheses () are used to specify the order that the AND & OR conditions analyzed. Let us see one more example of SELECT command with AND & 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 AND & 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 whose body_style is coupe OR whose body_style is Wagon, and the car_color is blue, OR whose body_styleis sedan the car_color is black, and the car_price is 63890. Example of PostgreSQL AND & OR with UPDATE CommandIn the below example, we will display how to use the AND & OR condition with the UPDATE command. For this, we are taking the department table from the Javatpoint database. In the following command, the PostgreSQL AND & OR condition updates the below values: The dept_name values to RESEARCH in the department table where either the emp_fname is 'Flora' OR dept_id is 4, and the emp_id is less than 5. Output After executing the above command, we will get the below output, where we can see that the department 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 department table: Output On executing the above command, we will get the below result: Example of PostgreSQL AND & OR with DELETE CommandIn the below example, we will display how to use the AND & OR Condition with the DELETE command. Here, we will take the customer table from Javatpoint Database, where we are deleting the particular records from the table. In the following command, the PostgreSQL AND & OR Condition is used to delete records from the customer table where we have the following values. The cust_address was 'Florida', and cust_name was 'Harvey' or the cust_age is greater than or equal to 26. Output After executing the above command, we will get the below message window displaying that the records have been deleted successfully. We will now use the Select command to check whether the particular records have been deleted or not in the Customer table: Output On executing the above command, we will get the below result: OverviewIn the PostgreSQL AND & OR Condition section, we have learned the following topics:
Next TopicPostgreSQL NOT Condition |