How to Filter Data in PostgreSQL

Galaxy Glossary

How do I filter rows using the WHERE clause in PostgreSQL?

The WHERE clause filters table rows by evaluating a Boolean condition and returning only rows that match.

Sign up for the latest in SQL knowledge from the Galaxy Team!
Welcome to the Galaxy, Guardian!
Oops! Something went wrong while submitting the form.

Description

How to Filter Data in PostgreSQL

Use WHERE and FILTER clauses to return only the rows that meet your conditions, reduce scan time, and speed up queries.

What does the WHERE clause do?

The WHERE clause evaluates a Boolean expression for every row and returns only rows where the expression is true. This filters data at the row level before aggregation or ordering.

What is the syntax for filtering rows?

Start a SELECT, list the columns, add FROM table_name, then append WHERE followed by one or more conditions. Combine conditions with AND, OR, and parentheses for precedence.

Basic equality filter

Retrieve users from the USA:
SELECT * FROM users WHERE country = 'USA';

Filter with logical operators

Combine checks:
SELECT * FROM users WHERE active AND (country = 'USA' OR country = 'CAN');

Pattern matching with LIKE and ILIKE

Find names that start with S, case-insensitive:
SELECT * FROM users WHERE name ILIKE 's%';

Range filtering with BETWEEN

Return orders placed in 2024:
SELECT * FROM orders WHERE order_date BETWEEN '2024-01-01' AND '2024-12-31';

Null-safe filtering

Only rows where phone is missing:
SELECT * FROM customers WHERE phone IS NULL;

When should I use FILTER in aggregates?

Use FILTER (WHERE ...) inside aggregate functions to include only matching rows without a subquery.

Aggregate filter syntax

SELECT count(*) FILTER (WHERE status = 'open') AS open_count FROM tickets;

Best practices for filtering data

1. Create indexes on columns often used in WHERE.
2. Compare to constants or parameters, not functions, to keep indexes usable.
3. Use EXPLAIN to verify index usage.

Full example query

SELECT id, name, created_at FROM customers WHERE country = 'USA' AND active = true ORDER BY created_at DESC LIMIT 50;

How to avoid common mistakes

Use IS NULL instead of = NULL, and enclose OR conditions in parentheses when mixed with AND.

Why How to Filter Data in PostgreSQL is important

How to Filter Data in PostgreSQL Example Usage


SELECT id, name
FROM public.customers
WHERE country = 'USA' AND active = true
ORDER BY created_at DESC;

How to Filter Data in PostgreSQL Syntax


SELECT column_list
FROM table_name
WHERE condition1 [AND|OR condition2 ...]
[ORDER BY column_list]
[LIMIT n];

-- Aggregate example
SELECT agg_func(col) FILTER (WHERE condition) FROM table;

Common Mistakes

Frequently Asked Questions (FAQs)

Does WHERE run before GROUP BY?

Yes. PostgreSQL executes WHERE to filter rows first, then groups the surviving rows. Use HAVING to filter after aggregation.

Can I filter on computed values?

Yes. Use expressions in WHERE, but wrap them in parentheses and beware of functions that prevent index usage.

How do I filter JSON fields?

Use the ->> operator to extract JSON text before comparison: WHERE data->>'status' = 'open'.

Want to learn about other SQL terms?

Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie
BauHealth Logo
Truvideo Logo
Welcome to the Galaxy, Guardian!
Oops! Something went wrong while submitting the form.