Chapter 20: PostgreSQL WHERE – Filter Data

Part 1: What is the WHERE Clause?

The WHERE clause filters rows from a table based on specified conditions. Only rows that satisfy the condition are included in the result set.

The Basic Syntax

sql

The condition can be simple (like price > 100) or incredibly complex with multiple parts combined using logical operators.

A Visual Understanding

Imagine a table as a filing cabinet with many drawers (rows). The WHERE clause is like telling your assistant: “Only show me the files where the customer is from Austin AND they spent more than $50.” Your assistant efficiently scans through, checking each file against your criteria, and returns only the matching ones.

Part 2: Setting Up Our Example Data

Let’s create a rich dataset for a bookstore so we can explore all the WHERE clause features:

sql

Part 3: Basic Comparison Operators

The foundation of WHERE clauses is comparison operators. Let’s explore each one with examples.

1. Equal To (=)

Find books by a specific author:

sql

Result:

title author price in_stock
1984 George Orwell 13.99 true

2. Not Equal To (!= or <>)

Find all books NOT by Jane Austen:

sql

Note: Both != and <> work the same way. I prefer != for readability.

3. Greater Than (>)

Find books priced above $25:

sql

Result:

title price author
Becoming 32.50 Michelle Obama
Educated 27.99 Tara Westover
Where the Crawdads Sing 26.99 Delia Owens
The Midnight Library 25.99 Matt Haig

4. Less Than (<)

Find books under $10:

sql

Result:

title price
Pride and Prejudice 9.99
Incomplete Records 9.99

5. Greater Than or Equal To (>=)

Find books with 400+ pages:

sql

Result:

title pages author
Sapiens 464 Yuval Noah Harari
Becoming 448 Michelle Obama
The Help 464 Kathryn Stockett
Pride and Prejudice 432 Jane Austen
Gone Girl 432 Gillian Flynn
Dune 688 Frank Herbert

6. Less Than or Equal To (<=)

Find books published in 1950 or earlier:

sql

Result:

title publication_date
Pride and Prejudice 1813-01-28
The Hobbit 1937-09-21
Brave New World 1932-01-01
1984 1949-06-08
Le Petit Prince 1943-04-06

Part 4: Logical Operators (AND, OR, NOT)

Real-world queries rarely use just one condition. Logical operators let you combine multiple conditions.

1. AND – Both Conditions Must Be True

Find affordable fiction books in stock:

sql

Result:

title genre price in_stock
The Great Gatsby Fiction 11.99 true
The Catcher in the Rye Fiction 12.99 false
The Alchemist Fiction 15.99 true

Notice that “The Catcher in the Rye” is excluded because it’s out of stock (in_stock = false).

2. OR – At Least One Condition Must Be True

Find books that are either by George Orwell OR published before 1950:

sql

Result:

title author publication_date
Pride and Prejudice Jane Austen 1813-01-28
The Hobbit J.R.R. Tolkien 1937-09-21
Brave New World Aldous Huxley 1932-01-01
1984 George Orwell 1949-06-08
Le Petit Prince Antoine de Saint-Exupéry 1943-04-06

3. NOT – Negate a Condition

Find books that are NOT in the Fiction genre:

sql

Result (partial):

title genre
Becoming Biography
Educated Biography
The Hobbit Fantasy
Sapiens History
Mystery Book Mystery
Pride and Prejudice Romance

You can also write this as WHERE genre != 'Fiction'. I find NOT more readable for complex conditions.

4. Combining AND, OR, and NOT with Parentheses

This is crucial! Without parentheses, AND has precedence over OR, which can lead to unexpected results.

sql

Result:

title author price
1984 George Orwell 13.99
Pride and Prejudice Jane Austen 9.99

Without parentheses, this happens:

sql

This would include ALL books by Jane Austen regardless of price!

Part 5: Special Operators for More Expressive Conditions

1. BETWEEN – Range Checking

Find books published between 2000 and 2020:

sql

Result:

title publication_date
The Da Vinci Code 2003-03-18
The Hunger Games 2008-09-14
The Help 2009-02-10
Gone Girl 2012-05-24
The Silent Patient 2019-02-05
Where the Crawdads Sing 2018-08-14
Becoming 2018-11-13
Atomic Habits 2018-10-16
Educated 2018-02-20
The Midnight Library 2020-08-13

BETWEEN is inclusive – it includes both boundary values.

Find books with page counts between 300 and 400:

sql

2. IN – Match Any Value in a List

Find books in specific genres:

sql

Result:

title genre
The Hobbit Fantasy
1984 Science Fiction
Brave New World Science Fiction
Dune Science Fiction
Gone Girl Thriller
The Da Vinci Code Thriller
The Silent Patient Thriller

This is much cleaner than writing multiple OR conditions:

sql

3. LIKE – Pattern Matching with Wildcards

% matches any sequence of characters (including zero characters)
_ matches exactly one character

Find books with titles starting with “The”:

sql

Result:

title author
The Alchemist Paulo Coelho
The Catcher in the Rye J.D. Salinger
The Da Vinci Code Dan Brown
The Great Gatsby F. Scott Fitzgerald
The Help Kathryn Stockett
The Hobbit J.R.R. Tolkien
The Hunger Games Suzanne Collins
The Midnight Library Matt Haig
The Silent Patient Alex Michaelides

Find books with “of” anywhere in the title:

sql

Result:

title
Cien años de soledad
Pride and Prejudice
The House of Mirth
The Lord of the Rings

Find authors with 5-letter first names (using _ for single characters):

sql

4. ILIKE – Case-Insensitive Pattern Matching

Standard LIKE is case-sensitive. ILIKE ignores case:

sql

5. Regular Expressions with ~ and ~*

For even more powerful pattern matching:

sql

Part 6: NULL Handling

NULL represents missing or unknown data. Working with NULL requires special operators.

1. IS NULL – Find Missing Values

Find books with missing prices:

sql

Result:

title price author
Mystery Book NULL Unknown Author

2. IS NOT NULL – Find Values That Exist

Find books with all information present:

sql

3. The NULL Trap – Common Mistake

sql

Why? NULL is not a value – it’s the absence of a value. You can’t compare with equals; you must use IS NULL.

4. NULL in Comparisons

When NULL appears in comparisons, the result is always NULL (which is treated as false in WHERE):

sql

NULL values are excluded from all comparison conditions unless explicitly included with IS NULL.

Part 7: Date and Time Filters

Working with dates is incredibly common in real applications.

1. Exact Date Matching

sql

2. Date Ranges

sql

3. Date Functions in WHERE

Find books published in the last 50 years:

sql

Find books published in the 21st century:

sql

Find books published in specific months:

sql

Part 8: Complex Real-World Examples

Example 1: Advanced Book Search

Find highly-rated, moderately-priced fiction books in stock:

sql

Example 2: Publisher Analysis

Find publishers with specific patterns and conditions:

sql

Example 3: Inventory Management

Find books that need reordering (low stock, but popular):

sql

Example 4: Multi-Criteria Customer Search

Imagine we have a customers table (let’s create one quickly):

sql

Part 9: Combining WHERE with Other Clauses

WHERE with ORDER BY

sql

WHERE with GROUP BY and HAVING

The WHERE clause filters rows BEFORE grouping, while HAVING filters groups AFTER grouping:

sql

WHERE with LIMIT

sql

Part 10: Performance Considerations

1. Indexes for WHERE Clauses

Indexes dramatically speed up WHERE conditions. Create indexes on columns frequently used in WHERE:

sql

2. Use EXPLAIN to Understand Query Execution

sql

This shows whether PostgreSQL uses your indexes and how it executes the query.

3. Be Careful with Functions on Indexed Columns

sql

4. LIKE Performance

  • LIKE 'pattern%' can use an index (anchored at start)

  • LIKE '%pattern' cannot use a standard index (wildcard at start)

  • Consider pg_trgm extension for text search with leading wildcards

Part 11: Common Mistakes and How to Avoid Them

Mistake 1: Using = with NULL

sql

Mistake 2: Forgetting Quotes Around Strings

sql

Mistake 3: Mixing AND/OR Without Parentheses

sql

Mistake 4: Using IN with an Empty List

sql

Mistake 5: Not Handling NULL in NOT IN

sql

Mistake 6: Assuming Boolean Comparisons

sql

I recommend using explicit = true and = false for clarity.

Summary: The WHERE Clause Philosophy

The WHERE clause is your primary tool for asking precise questions of your data. Master these concepts in order:

  1. Basic comparisons (=, !=, >, <, >=, <=)

  2. Logical combinations (AND, OR, NOT with parentheses)

  3. Special operators (BETWEEN, IN, LIKE, ILIKE)

  4. NULL handling (IS NULL, IS NOT NULL)

  5. Date and time filtering

  6. Performance optimization (indexes, EXPLAIN)

Remember the execution order:

  • FROM (identify table)

  • WHERE (filter rows) – happens HERE!

  • GROUP BY (group filtered rows)

  • HAVING (filter groups)

  • SELECT (choose columns)

  • ORDER BY (sort results)

  • LIMIT (limit output)

Every time you write a WHERE clause, you’re refining your question to the database. Start broad, then narrow down step by step. Test with small result sets first, then add complexity.

The WHERE clause transforms you from someone who just looks at data into someone who can interrogate it, find patterns, answer questions, and make data-driven decisions. It’s the difference between “Here’s our entire customer list” and “Here are our high-value customers in Texas who haven’t ordered in 6 months.”

Practice with these examples, experiment with your own data, and soon filtering with WHERE will become second nature!

Would you like me to elaborate on any specific aspect of the WHERE clause, such as more complex conditions, performance tuning, or integration with joins and subqueries?

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *