SQL OR Statement with PostgreSQL
Introduction
Being able to select the exact data you need is vital in all database technologies. One of the most common operators you can use is the OR operator. If you’re unfamiliar with the SQL OR operator then this article will be a great introduction to it’s use. In this article we’ll be using PostgreSQL to demonstrate.
Prerequisite
If you want to follow along you should have PostgreSQL installed and running.
What is the PostgreSQL OR operator?
The OR operator is a conjunctive operator and is used to combine multiple conditions into one. The combined condition is true when ANY of the individual conditions is true.
For the demonstration we have a demo database from a small grocery store database with a table called products
. The following data is in the table:
1 2 3 4 5 6 7 8 | id | name | price | department_id ----+----------------+-------+--------------- 1 | Almond Milk | 2.99 | 1 2 | Soy Milk | 2.49 | 1 3 | Whole Milk | 3.19 | 1 4 | Corn Chips | 1.99 | 2 5 | Tortilla Chips | 1.99 | 2 (5 rows) |
Now what if we asked you to get both the Almond Milk ( id = 1 ) and Tortilla Chips ( id = 5 ) records in one query … how would you do that? One way to do that is through the OR operator. You would create a SELECT query that will pull return the row if either the id = 1 OR the id = 5.
That query would look like this:
1 | SELECT * FROM products WHERE id = 1 OR id = 5; |
Output:
1 2 3 4 5 | id | name | price | department_id ----+----------------+-------+--------------- 1 | Almond Milk | 2.99 | 1 5 | Tortilla Chips | 1.99 | 2 (2 ROWS) |
More than two conditions
The OR operator is not limited to two clauses like the example before. We can OR together as many conditions as we need to select the data we need. For example if we again look at this table:
1 2 3 4 5 6 7 8 | id | name | price | department_id ----+----------------+-------+--------------- 1 | Almond Milk | 2.99 | 1 2 | Soy Milk | 2.49 | 1 3 | Whole Milk | 3.19 | 1 4 | Corn Chips | 1.99 | 2 5 | Tortilla Chips | 1.99 | 2 (5 ROWS) |
Now we want to select items that either have a price over 2.99, have a department_id = 2, or have the exact name “Soy Milk”. That could be accomplished by OR’ing together three conditions like so:
1 | SELECT * FROM products WHERE price > 2.99 OR department_id = 2 OR name = 'Soy Milk'; |
Output:
1 2 3 4 5 6 7 | id | name | price | department_id ----+----------------+-------+--------------- 2 | Soy Milk | 2.49 | 1 3 | Whole Milk | 3.19 | 1 4 | Corn Chips | 1.99 | 2 5 | Tortilla Chips | 1.99 | 2 (4 ROWS) |
Notice how this time we combined three conditions to get the exact data we wanted.
Conclusion
We have demonstrated how the use of the SQL OR statement using PostgreSQL. If you’re getting familiar with condtions in SQL you’ll also want to check out the use of the AND and NOT operators. These through operators used together are very powerful.
If you weren’t able to solve your issue and would like some suggestions please don’t hesitate to reach out to us at Object Rocket. We’re happy to discuss your application’s needs.
Pilot the ObjectRocket Platform Free!
Try Fully-Managed CockroachDB, Elasticsearch, MongoDB, PostgreSQL (Beta) or Redis.
Get Started