SQL WHERE
Filtering
Learn how to select exactly the rows you need using comparisons, boolean logic, and powerful filter conditions in SQL.
What Is Filtering?
A database table might have thousands or millions of rows. Most of the time, you only care about a small subset. Filtering means telling the database: "give me only the rows that match these conditions."
The WHERE Clause
WHERE is the keyword you add after the table name in a SELECT statement. It defines a condition that every returned row must satisfy.
SELECT column1, column2
FROM table_name
WHERE condition;How WHERE Processes Rows
Comparison Operators
SQL provides six comparison operators to test a column value against a literal or another column:
| Operator | Meaning | Example |
|---|---|---|
| \(=\) | Equal to | \(\text{age} = 18\) |
| \(!=\) or \(<>\) | Not equal | \(\text{status} != \text{'active'}\) |
| \(<\) | Less than | \(\text{price} < 50\) |
| \(>\) | Greater than | \(\text{score} > 90\) |
| \(\le\) | Less than or equal | \(\text{qty} \le 100\) |
| \(\ge\) | Greater than or equal | \(\text{grade} \ge \text{'B'}\) |
Boolean Logic: AND, OR, NOT
Combine multiple conditions with boolean operators to build precise filters:
AND vs OR: Visual Comparison
WHERE vs HAVING
Both filter rows, but they operate at different stages of query execution:
| Feature | WHERE | HAVING |
|---|---|---|
| Filters | Individual rows | Groups (after GROUP BY) |
| Runs | Before grouping | After grouping |
| Aggregates? | Cannot use SUM, COUNT, etc. | Can use aggregates |
| Example | \(\text{WHERE age} > 18\) | \(\text{HAVING COUNT}(*) > 5\) |
Special WHERE Operators
How to Build a WHERE Filter
Example 1: Simple Comparison
Task: Find all students who scored above 85 on their exam.
SELECT name, score
FROM students
WHERE score > 85;Example 2: Multiple Conditions
Task: Find products that cost under $50 and are in the "Electronics" category.
SELECT product_name, price, category
FROM products
WHERE price < 50
AND category = 'Electronics';Example 3: Complex Boolean Logic
Task: Find employees in the "Sales" or "Marketing" department who were hired after 2020.
SELECT name, department, hire_date
FROM employees
WHERE (department = 'Sales'
OR department = 'Marketing')
AND hire_date > '2020-01-01';Example 4: IN and BETWEEN
Task: Find orders placed between January and March 2024 for customers in New York, Chicago, or Miami.
SELECT order_id, customer_city, order_date
FROM orders
WHERE order_date BETWEEN '2024-01-01' AND '2024-03-31'
AND customer_city IN ('New York', 'Chicago', 'Miami');Common Mistakes
WHERE Cheat Sheet
| Pattern | Syntax | Use When |
|---|---|---|
| Exact match | col = value | You know the exact value |
| Range | col BETWEEN a AND b | Value falls within bounds |
| Set membership | col IN (v1, v2, ...) | Value is one of several options |
| Pattern | col LIKE 'pattern' | Partial text matching |
| Missing data | col IS NULL | Checking for absent values |
| Combine | cond1 AND cond2 | All conditions must hold |
| Alternate | cond1 OR cond2 | Any condition suffices |
| Negate | NOT condition | Exclude matching rows |
Question 1 of 5
Which SQL keyword is used to filter rows based on a condition?
Question 2 of 5
What does the following query return?
SELECT * FROM users WHERE age \(\ge\) 18 AND status = 'active';
Question 3 of 5
How do you correctly check for missing (NULL) values in a column?
Question 4 of 5
Given: WHERE dept = 'HR' OR dept = 'Finance' AND salary \(>\) 50000
Without parentheses, which rows are returned?
Question 5 of 5
Which query correctly finds products priced between $10 and $50 that are not in the "Toys" category?