The WHERE Clause
The WHERE clause filters records — it returns only the rows that meet a specified condition.
💡 WHERE is like a sieve for your data. It keeps the rows you want and discards the rest.
Basic Syntax
SELECT column1, column2
FROM table_name
WHERE condition;
WHERE with Numbers
SELECT first_name, last_name, salary
FROM employees
WHERE salary > 70000;
| first_name | last_name | salary |
|---|---|---|
| Alice | Johnson | 75000 |
| Carol | Williams | 85000 |
| Eve | Brown | 91000 |
Comparison Operators
| Operator | Meaning | Example |
|---|---|---|
= | Equal to | WHERE age = 25 |
<> or != | Not equal | WHERE age <> 25 |
> | Greater than | WHERE price > 100 |
< | Less than | WHERE price < 50 |
>= | Greater or equal | WHERE score >= 90 |
<= | Less or equal | WHERE score <= 60 |
WHERE with Text
-- Exact match
SELECT * FROM customers WHERE city = 'New York';
-- Case-insensitive (database dependent)
SELECT * FROM customers WHERE LOWER(city) = 'new york';
WHERE with Dates
SELECT * FROM orders WHERE order_date >= '2024-01-01';
SELECT * FROM orders
WHERE order_date BETWEEN '2024-01-01' AND '2024-03-31';
WHERE with NULL
-- Check for NULL (cannot use = NULL)
SELECT * FROM customers WHERE email IS NULL;
-- Check for NOT NULL
SELECT * FROM customers WHERE email IS NOT NULL;
⚠️ NULL is not the same as 0 or empty string. Use IS NULL and IS NOT NULL to check for NULL values — never use = NULL.
✏️ Exercise: Find all employees who earn more than 60000, work in Engineering, and were hired after January 1, 2023
See Solution
SELECT first_name, last_name, salary
FROM employees
WHERE salary > 60000
AND department = 'Engineering'
AND hire_date > '2023-01-01';
✅ Key Takeaways
- WHERE filters rows based on conditions
- Use comparison operators for numbers, text, and dates
- Use IS NULL / IS NOT NULL for NULL checks
- Combine multiple conditions with AND and OR
- WHERE is evaluated after FROM but before SELECT