PostgreSQL OR Operator
PostgreSQL OR operator is a logical operator that lets you connect several conditions in the WHERE clause to get the rows that are related to at least one of the specified ones. It returns true if any of the given conditions found by the 'OR' operator has the truth as its result.
Let us get a better understanding of the PostgreSQL OR operator from this article.
Syntax
SELECT column1, column2, ... FROM table_name WHERE condition1 OR condition2 OR condition3 ...;
Here, 'condition1', 'condition2', etc., are the conditions you want to evaluate using the OR operator. You can have as many conditions as needed, separated by OR.
PostgreSQL OR Operator Examples
Let us take a look at some of the examples of OR Operator in PostgreSQL to better understand the concept.
First, let's create a table named as 'employees' in PostgreSQL insert some data into it and then perform some queries using the OR operator.
CREATE TABLE employees (
id SERIAL PRIMARY KEY,
name VARCHAR(100),
department VARCHAR(100),
salary NUMERIC(10, 2)
);
INSERT INTO employees (name, department, salary)
VALUES
('Minal Pandey', 'Sales', 60000),
('Vivek Sharma', 'Marketing', 55000),
('Priyanshi Sharma', 'Sales', 48000),
('Vardhana Sharma', 'Engineering', 70000);
SELECT * FROM employees;
.webp)
Example 1: Retrieve Employees in Sales Departments or with salaries higher than 55000.
The query below selects all of the "employees" table's items if the department is 'Sales' or if the salary is more than 55000.
Query:
SELECT * FROM employees WHERE department = 'Sales' OR salary > 55000
Output:
.webp)
Explanation: You can see that query returns up every row in the "employees" table, whether the department name is "Sales" or if the salary is more than 55000.
Example 2: Retrieving employees in Sales or Marketing Departments or with salaries higher than 55000.
This query selects the names of employees from the "employees" table where the department is either 'Sales' or 'Marketing', or where the salary is greater than 55000.
Query:
SELECT name
FROM employees
WHERE department = 'Sales' OR department = 'Marketing' OR salary > 55000;
Output:
.webp)
Explanation: You can see that the query returned the names of employees who work in the 'Sales' or 'Marketing' department, or whose salary is greater than 55000.
Example 3: Retrieve Employees in Marketing Departments or with salaries higher than 55000.
The query below selects all of "employees" table's items if the department is 'Marketing' or if the salary is more than 55000.
Query:
SELECT * FROM employees WHERE department = 'Marketing' OR salary > 55000;
Output:
.webp)
Explanation: You can see that the query returned all information about employees who work in the 'Marketing' department or have a salary greater than 55000.
Important Points About PostgreSQL OR Operator
- The OR operator allows you to connect several conditions in the WHERE clause to retrieve rows that satisfy at least one of the conditions.
- You can use as many conditions as needed, separated by the OR keyword, making it highly flexible for complex queries.
- Sometimes, using the IN operator or restructuring the query logic can be more efficient and easier to understand than using multiple OR conditions.
- If any of the conditions specified using OR evaluate to true, the overall condition returns true.