Skip to main content
⚑ OpenDevDocs
PostgreSQL
databasepostgresqlGuideBeginner

PostgreSQL WHERE Clause & Filtering

Filter query results using WHERE operators, logical combinations (AND, OR, NOT), pattern matching (LIKE, ILIKE), and NULL checks.

Verified: 2026-09-30
Tested on:postgres 16.x
Platforms:
linuxmacoswindows
#postgresql#where#filtering#sql#operators

Overview

The WHERE clause specifies search conditions to filter rows returned by SELECT, or modified by UPDATE and DELETE statements.


Comparison Operators

OperatorDescriptionExample
=Equal toWHERE status = 'active'
<> or !=Not equal toWHERE status != 'archived'
>, <Greater than, Less thanWHERE price > 50.00
>=, <=Greater or equal, Less or equalWHERE age >= 18

Logical Operators & Ranges

1. AND, OR, NOT

SELECT * FROM orders
WHERE status = 'pending' AND (total_amount > 100 OR is_priority = true);

2. IN and NOT IN

SELECT * FROM users
WHERE role IN ('admin', 'moderator', 'editor');

3. BETWEEN

SELECT * FROM products
WHERE price BETWEEN 10.00 AND 50.00;

Pattern Matching (LIKE vs ILIKE)

  • LIKE: Case-sensitive wildcard search.
  • ILIKE: Case-insensitive wildcard search (PostgreSQL extension).
  • % matches zero or more characters; _ matches exactly one character.
-- Case-insensitive search for emails ending in @gmail.com
SELECT * FROM users 
WHERE email ILIKE '%@gmail.com';

-- Names starting with 'J' and 4 letters total
SELECT * FROM users
WHERE username LIKE 'J___';

Handling NULL Values

In SQL, NULL represents an unknown value. You cannot compare NULL with = (e.g. WHERE deleted_at = NULL always evaluates to False/Unknown).

-- βœ… Correct
SELECT * FROM articles WHERE deleted_at IS NULL;
SELECT * FROM articles WHERE published_at IS NOT NULL;

On this page