Examples of PostgreSQL OR Operator
Example 1: Retrieve Employees in Sales Departments or with salaries higher than 55000.
Query:
The query below selects all of the “employees” table’s items if the department is ‘Sales‘ or if the salary is more than 55000.
SELECT *
FROM employees
WHERE department = 'Sales' OR salary > 55000
Output:
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: Retrieve the name of the employees in the Sales Departments or marketing department or with salaries higher than 55000.
Query:
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.
SELECT name
FROM employees
WHERE department = 'Sales' OR department = 'Marketing' OR salary > 55000;
Output:
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.
Query:
The query below selects all of “employees” table’s items if the department is ‘Marketing‘ or if the salary is more than 55000.
SELECT *
FROM employees
WHERE department = 'Marketing' OR salary > 55000;
Output:
You can see that the query returned all information about employees who work in the ‘Marketing’ department or have a salary greater than 55000.
PostgreSQL OR Operator
PostgreSQL is an advanced relational database system that supports both relational (SQL) and non-relational (JSON) queries. It is free and open-source. PostgreSQL, one of the most used relational database management systems, has many operators supporting manipulation and querying data in various ways.
The operators stand side by side with each other and the OR operator is the most effective one when they need to create queries that are a bit detailed. The article focused on the OR operation in PostgreSQL and covered the syntax and the most appropriate ways to use the operator.