Definition of PostgreSQL OR condition
PostgreSQL OR condition is used to test one or more conditions in a single query, after checking the records using OR condition it will return the result of which condition met. We can use OR condition in the insert, update, delete, and select statement, OR condition is used in all the statements of select, update, delete, and insert query in PostgreSQL.
Syntax:
Below is the syntax of OR condition.
- OR condition by using select statement
Select name_of_column1, name_of_column2, name_of_column3, …, name_of_columnN, from name_of_table where [condition] OR [condition1;
Select name_of_column1, name_of_column2, name_of_column3, …, name_of_columnN, from name_of_table where [condition] OR [condition1], OR [condition2], OR [condition3], …, OR [conditionN];
- OR condition by using insert statement
Insert into name_of_table (name_of_column1, name_of_column2, name_of_column3, …, name_of_columnN) Select name_of_column1, name_of_column2, name_of_column3, …, name_of_columnN, from name_of_tablewhere [condition] OR [condition1], OR [condition2], OR [condition3], …, OR [conditionN];
- OR condition by using an update statement
Update name_of_table SET [update condition using column name] where [condition] OR [condition1], OR [condition2], OR [condition3], …, OR [conditionN];
- OR condition by using the delete statement
Delete from name_of_table where [condition] OR [condition1], OR [condition2], OR [condition3], …, OR [conditionN];
Below is the parameter description syntax of OR condition.
- Select – This statement is used to retrieve the rows from the table by using the OR condition.
- Update – This statement is used to update the rows from the table by using the OR condition.
- Insert – This statement is used to insert the rows into the table by using the OR condition.
- Delete – This statement is used to delete the rows from the table by using the OR condition.
- Name of the column –This is defined as the name of the column from which we have performing insert, select, update and delete operations by using the OR condition.
- Name of the table – This is defined as the name of the table from which column we have performing insert, select, update, and delete operations by using the OR condition in PostgreSQL.
- Where condition – This condition is used to select the specified condition values from the table. Where the condition is very important while using OR condition in PostgreSQL.
- OR condition – OR condition is very useful in PostgreSQL, to use multiple conditions in a single query.
How OR condition works in PostgreSQL?
- Below is the working of OR condition in PostgreSQL.
- Basically, we have used OR condition in PostgreSQL to select unique data from the table by using single as well as multiple conditions.
- We have also used OR condition with insert, update and delete statements to select unique data using single as well as multiple conditions.
- PostgreSQL IN condition is better than OR condition because query performance of IN condition is better than OR condition.
- While we have retrieved data using OR condition it will scan all rows from the table, so the performance of OR condition will be less as compared to IN condition in PostgreSQL.
- OR condition in PostgreSQL will allow us to test two or more conditions, we can test OR condition by using two or more conditions.
- We have to meet anyone’s condition to use OR condition in PostgreSQL. The below example shows that we need to met anyone’s condition to retrieve rows from the table using the OR condition.
SELECT * FROM stud1 WHERE name = 'PQR' OR id = 15 OR id = 15 OR id = 17;
SELECT * FROM stud1 WHERE name = 'PQR' OR id = 8 OR id = 9 OR id = 10;
- In the above first example, we have used the name as PQR in where condition and id as 15, 16, and 17 in OR condition, this data is not met the condition so it will return the empty set.
- In the second example we have used the name as PQR in where condition and id as 8, 9, and 10 in OR condition, it will meet the OR condition so it will return 3 rows from the table.
Examples
- Below is an example of an OR condition in PostgreSQL. We have using or_test1 and or_test2 table to describe an example of OR condition in PostgreSQL.
- Below is the data and table description table of or_test1.
select * from or_test1;
\d+ or_test1;
- Below is the data and table description table of or_test2.
select * from or_test2;
\d+ or_test2;
1. Single OR condition using select statement
- Below example shows that single OR condition using a select statement.
SELECT * FROM or_test1 WHERE name = 'PQR' OR id = 10;
2. Multiple OR condition using select statement
- The below example shows that multiple OR conditions using a select statement.
SELECT * FROM or_test1 WHERE name = 'PQR' OR id = 10 OR id = 9 OR id = 1;
3. OR condition using insert statement
- The below example shows that OR condition using insert statement.
INSERT INTO or_test1 (id, name) SELECT id, name FROM or_test2 WHERE id > 0 OR name = 'PQR';
select * from or_test1;
4. OR condition using delete statement
- The below example shows that OR condition using the delete statement.
DELETE FROM or_test1 WHERE name = 'ABC' OR name = 'PQR' OR id = 10 OR id = 12;
select * from or_test1;
5. OR condition using update statement
- Below example shows that OR condition using update statement.
UPDATE or_test2 SET name = 'XYZ' WHERE name = 'ABC' OR name = 'PQR';
select * from or_test2;
Recommended Articles
This is a guide to PostgreSQL OR. Here we discuss the introduction, How OR condition works in PostgreSQL? and examples respectively. You may also have a look at the following articles to learn more –