Updated May 17, 2023
Introduction to 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 the OR condition:
1. OR condition by using a 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];
2. 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];
3. 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];
4. 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 the OR condition:
- Select – The SELECT statement is employed to fetch rows from a table by utilizing the OR condition.
- Update – This statement is used to update the rows from the table by using the OR condition.
- Insert – This statement uses the OR condition to insert the rows into the table.
- Delete – This statement is used to delete the rows from the table by using the OR condition.
- Name of the column – The name of the column designates the specific column where we execute insert, select, update, and delete operations using the OR condition.
- Name of the table –In PostgreSQL, the name of the table is referred to as the “table_name.” It represents the specific table where insert, select, update, and delete operations have been performed using the OR condition.
- Where condition – This condition selects 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:
- We have used OR condition in PostgreSQL to select unique data from the table by using single and multiple conditions.
- We have also used OR condition with insert, update and delete statements to select unique data using single and multiple conditions.
- PostgreSQL IN condition is better than the OR condition because the query performance of IN condition is better than the OR condition.
- While we have retrieved data using OR condition, it will scan all rows from the table so that the OR condition performance will be less 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 meet 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 PQR in where condition and id as 15, 16, and 17 in OR condition, this data has not met the condition, so it will return the empty set.
- In the second example, we have used the name PQR where condition and id as 8, 9, and 10 in OR condition 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 are using the or_test1 and or_test2 table to describe an example of an 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 an update statement.
UPDATE or_test2 SET name = 'XYZ' WHERE name = 'ABC' OR name = 'PQR';
select * from or_test2;
Recommended Articles
We hope that this EDUCBA information on “PostgreSQL OR” was beneficial to you. You can view EDUCBA’s recommended articles for more information.