Browse SQL Topics โ–ผ

WHERE

๐Ÿ“‚ Basic Filtering ๐Ÿ“˜ Lesson 16 of 81

Filter rows conditionally with WHERE using comparison operators, logical operators (AND, OR, NOT), NULL checks, and pattern matching.

While select acts like a vertical cookie cutterโ€”choosing which columns to showโ€”the WHERE clause acts like a horizontal filter or a bouncer at the door. It evaluates every row in the table against one or more rules and discards any row that does not pass the test.

Without WHERE, every query would dump the entire contents of your database into memory.


Basic Filtering Syntax

The WHERE clause always comes immediately after the FROM clause:

SELECT name, price, category
FROM products
WHERE price < 50;

Given this table:

               TABLE: products
โ”Œโ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ id โ”‚ name             โ”‚ price    โ”‚ category  โ”‚
โ”œโ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚ 1  โ”‚ Wireless Mouse   โ”‚ 24.99    โ”‚ Tech      โ”‚
โ”‚ 2  โ”‚ Ergonomic Desk   โ”‚ 299.00   โ”‚ Furniture โ”‚
โ”‚ 3  โ”‚ Coffee Mug       โ”‚ 12.50    โ”‚ Kitchen   โ”‚
โ”‚ 4  โ”‚ Mechanical Board โ”‚ 89.99    โ”‚ Tech      โ”‚
โ””โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

The query evaluates each rowโ€™s price. Rows 1 and 3 pass; rows 2 and 4 are discarded:

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ name           โ”‚ price โ”‚ category โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚ Wireless Mouse โ”‚ 24.99 โ”‚ Tech     โ”‚
โ”‚ Coffee Mug     โ”‚ 12.50 โ”‚ Kitchen  โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Comparison Operators

SQL provides standard mathematical and relational comparisons:

OperatorMeaningExample
=Equals (Notice single equals, not ==!)WHERE status = 'active'
!= or <>Not equalWHERE status != 'archived'
>, <Greater than, Less thanWHERE age >= 21
BETWEEN a AND bValue falls within range (inclusive)WHERE price BETWEEN 10 AND 50
IN (...)Matches any item in a listWHERE city IN ('London', 'Tokyo', 'Paris')
-- Finding users in target cities
SELECT name, city
FROM customers
WHERE city IN ('London', 'Madrid');

Combining Conditions: AND, OR, and NOT

You can chain multiple criteria together using logical operators:

  • AND: Returns the row only if both conditions are true.
  • OR: Returns the row if either condition is true.
  • NOT: Inverts the condition.
SELECT name, department, salary
FROM employees
WHERE department = 'Engineering' AND salary > 80000;

The Parentheses Trap (Operator Precedence)

In SQL, AND has higher precedence than OR. The database evaluates AND operations first unless you group them with parentheses ():

-- DANGEROUS / MISLEADING:
SELECT name, city, is_active
FROM customers
WHERE city = 'London' OR city = 'Paris' AND is_active = TRUE;
-- Evaluated as: (city = 'London') OR (city = 'Paris' AND is_active = TRUE)
-- Inactive London customers will be mistakenly included!

-- SAFE AND EXPLICIT:
SELECT name, city, is_active
FROM customers
WHERE (city = 'London' OR city = 'Paris') AND is_active = TRUE;

Always wrap OR conditions in parentheses when combining them with AND.


Pattern Matching with LIKE

When searching for text without knowing the exact string, use LIKE with wildcards:

  • %: Matches zero, one, or multiple characters.
  • _: Matches exactly one single character.
-- Find any customer whose email ends in '@gmail.com'
SELECT name, email
FROM customers
WHERE email LIKE '%@gmail.com';

-- Find phone codes with pattern 'US-01', 'US-09', etc.
SELECT code
FROM regions
WHERE code LIKE 'US-0_';

The NULL Trap: Why = NULL Fails

In SQL, NULL does not mean zero or empty text "". It means value unknown or missing.

Because NULL is unknown, SQL follows three-valued logic: TRUE, FALSE, or UNKNOWN. If you ask SQL: โ€œIs an unknown value equal to another unknown value?โ€, SQL responds: โ€œI donโ€™t know (UNKNOWN)โ€.

-- CATASTROPHIC ERROR: Returns 0 rows every single time!
SELECT * FROM users WHERE deleted_at = NULL;

-- THE CORRECT SQL WAY:
SELECT * FROM users WHERE deleted_at IS NULL;
SELECT * FROM users WHERE email IS NOT NULL;

[!IMPORTANT] Never use = NULL or != NULL. Always use IS NULL and IS NOT NULL.


Common Traps to Avoid

  1. Using Double Quotes for Text: In SQL, single quotes ('New York') represent text strings. Double quotes ("New York") are reserved in standard SQL for identifiers like table or column names with spaces.
  2. Writing == for Equality: Programming languages like Python use ==. SQL uses a single = for comparison.
  3. Searching for Case-Sensitive Text with =: In systems like PostgreSQL, 'Alice' does not match 'alice'. Use lower-casing (LOWER(name) = 'alice') or ILIKE for case-insensitive comparisons.

Next Steps

Master this concept to build a rock-solid foundation. Next, proceed to comparison-operators.

Test your understanding of WHERE

Quick interactive practice questions with instant feedback.

Take Quiz โ†’