PostgreSQL Filtering Operators: BETWEEN, IN, LIKE, IS NULL
Learn PostgreSQL's most useful filtering operators — BETWEEN for ranges, IN for lists, LIKE/ILIKE for pattern matching, and IS NULL for checking missing values.
PostgreSQL Filtering Operators
These operators make WHERE clauses far more expressive than plain =/>/< comparisons.
BETWEEN — Range Check
Inclusive on both ends.
SELECT * FROM employees WHERE salary BETWEEN 40000 AND 70000;
-- equivalent to: salary >= 40000 AND salary <= 70000
SELECT * FROM employees WHERE hired_at BETWEEN '2024-01-01' AND '2024-12-31';
IN — Match Any Value in a List
SELECT * FROM employees WHERE department IN ('Sales', 'HR', 'Support');
-- equivalent to: department = 'Sales' OR department = 'HR' OR department = 'Support'
SELECT * FROM employees WHERE department NOT IN ('Sales', 'HR');
IN also accepts a subquery — covered in the Subqueries lesson.
LIKE / ILIKE — Pattern Matching
% matches any sequence of characters, _ matches exactly one character. LIKE is case-sensitive; ILIKE is case-insensitive.
SELECT * FROM employees WHERE name LIKE 'Ra%'; -- starts with "Ra"
SELECT * FROM employees WHERE name LIKE '%Gupta'; -- ends with "Gupta"
SELECT * FROM employees WHERE name LIKE '%upt%'; -- contains "upt" anywhere
SELECT * FROM employees WHERE name ILIKE '%gupta%'; -- case-insensitive
SELECT * FROM employees WHERE email LIKE '_ahul@gmail.com';
-- matches any single character followed by "ahul@gmail.com"
Advertisement728 x 90
IS NULL / IS NOT NULL
NULL represents "no value" — you can never compare it with =, only with IS.
SELECT * FROM employees WHERE phone IS NULL;
SELECT * FROM employees WHERE phone IS NOT NULL;
-- this NEVER matches, even for rows where phone actually is NULL:
SELECT * FROM employees WHERE phone = NULL;
Common Errors & Troubleshooting
WHERE phone = NULLnever matching anything — comparisons againstNULLalways evaluate to unknown, never true; useIS NULLinstead (see the NULL Semantics lesson for the full explanation).LIKEpattern not matching because of a literal%or_in the data — if you need to match those characters literally, escape them:LIKE '50\%' ESCAPE '\'.BETWEENwith a date range missing the last day's records —BETWEEN '2024-01-01' AND '2024-01-31'on aTIMESTAMPcolumn only includes up to midnight of the 31st, missing any time later that day; use>= '2024-01-01' AND < '2024-02-01'for a full-day-inclusive range.
Key Takeaways
BETWEENis inclusive on both bounds;INchecks membership in an explicit list.- Use
ILIKEinstead ofLIKEwhenever the search should ignore case. NULLcan only be tested withIS NULL/IS NOT NULL—= NULLsilently matches nothing.
Advertisement320 x 100