Introduction to the PostgreSQL OR operator
The OR operator is a logical operator that combines two boolean expressions. Like AND, it works with the three-valued boolean logic of PostgreSQL: TRUE, FALSE, and NULL.
Basic syntax:
expression1 OR expression2
The truth table for OR:
TRUE OR TRUE→TRUETRUE OR FALSE→TRUETRUE OR NULL→TRUE(one side is definitively true)FALSE OR FALSE→FALSEFALSE OR NULL→NULLNULL OR NULL→NULL
The key rule: OR returns TRUE if at least one expression is true. It only returns FALSE when both are definitively false.
Basic OR examples
SELECT true OR true AS result; -- t
SELECT true OR false AS result; -- t
SELECT true OR null AS result; -- t
SELECT false OR false AS result; -- f
SELECT false OR null AS result; -- null
SELECT null OR null AS result; -- null
Using OR in a WHERE clause
In practice, OR lets you match rows that satisfy any of several conditions. This example finds films from the DVD rental database with a rental rate of $0.99 or $2.99:
SELECT title, rental_rate
FROM film
WHERE rental_rate = 0.99
OR rental_rate = 2.99;
Partial result:
title | rental_rate
-----------------------------+-------------
Academy Dinosaur | 0.99
Adaptation Holes | 2.99
Affair Prejudice | 2.99
African Egg | 2.99
...
OR vs. IN: choosing the right tool
When you are testing the same column against multiple values, IN is a cleaner alternative to chaining OR:
-- Equivalent queries:
WHERE rental_rate = 0.99 OR rental_rate = 2.99 OR rental_rate = 4.99
WHERE rental_rate IN (0.99, 2.99, 4.99)
IN is easier to read as the list grows and is treated identically by the planner for small value lists.
AND with OR: precedence and parentheses
AND has higher precedence than OR, so mixed expressions without parentheses may not behave as expected:
-- This is evaluated as: (rating = 'G' AND length > 100) OR rental_rate < 1
WHERE rating = 'G' AND length > 100 OR rental_rate < 1;
-- To require length > 100 for both cases, use parentheses:
WHERE (rating = 'G' OR rental_rate < 1) AND length > 100;
Always add parentheses when mixing AND and OR to make your intent unambiguous.
NULL behavior with OR
Because TRUE OR NULL is TRUE, filtering with OR can sometimes include more rows than expected when nulls are involved. When both sides are indeterminate — FALSE OR NULL — the result is NULL, and the row is excluded from the result set:
-- Rows where either status is unknown (NULL) or rating is 'G'
-- will be included only if rating = 'G' is TRUE
SELECT title, rating
FROM film
WHERE rating = 'G'
OR some_nullable_column = 'value';
Reference: PostgreSQL documentation — Logical Operators.