Introduction to the PostgreSQL AND operator
The AND operator is a logical operator that combines two boolean expressions. A boolean expression in PostgreSQL evaluates to one of three values: TRUE, FALSE, or NULL.
Basic syntax:
expression1 AND expression2
The truth table for AND:
TRUE AND TRUE→TRUETRUE AND FALSE→FALSETRUE AND NULL→NULLFALSE AND FALSE→FALSEFALSE AND NULL→FALSE(because one side is definitively false)NULL AND NULL→NULL
The key rule: AND returns TRUE only when both expressions are true. If either is definitively FALSE, the result is FALSE regardless of the other side.
Basic AND examples
SELECT true AND true AS result; -- t
SELECT true AND false AS result; -- f
SELECT true AND null AS result; -- null
SELECT false AND null AS result; -- f
SELECT null AND null AS result; -- null
Using AND in a WHERE clause
The most common use of AND is combining multiple filter conditions in a WHERE clause. All conditions joined by AND must be true for a row to be included in the result.
This query finds films from the DVD rental database that are longer than 180 minutes and have a rental rate below $1.00:
SELECT title, length, rental_rate
FROM film
WHERE length > 180
AND rental_rate < 1;
Result:
title | length | rental_rate
--------------------+--------+-------------
Catch Amistad | 183 | 0.99
Haunting Pianist | 181 | 0.99
Intrigue Worst | 181 | 0.99
Love Suicides | 181 | 0.99
Smoochy Control | 184 | 0.99
(10 rows)
Chaining multiple AND conditions
You can chain as many conditions as needed. All must evaluate to TRUE for a row to pass the filter:
SELECT title, rating, length, rental_rate
FROM film
WHERE rating = 'PG'
AND length BETWEEN 90 AND 120
AND rental_rate <= 2.99;
AND with OR: precedence and parentheses
AND has higher operator precedence than OR, meaning it is evaluated first. This can lead to unexpected results when mixing them without parentheses:
-- Without parentheses: AND binds tighter, so this means:
-- (rating = 'G' AND length > 100) OR rental_rate < 1
WHERE rating = 'G' AND length > 100 OR rental_rate < 1;
-- Use parentheses to make intent explicit:
WHERE (rating = 'G' OR rental_rate < 1) AND length > 100;
Always use parentheses when combining AND and OR to avoid subtle logic errors.
NULL behavior with AND
Because any comparison with NULL produces NULL (not TRUE or FALSE), a condition like column = NULL never returns rows. Use IS NULL or IS NOT NULL explicitly:
-- Correct: find active customers with no phone on file
SELECT customer_id, first_name
FROM customer
WHERE active = 1
AND phone IS NULL;
Reference: PostgreSQL documentation — Logical Operators.