SQL Where
SQL WHERE Clause
The WHERE clause is used to filter records. It lets you return only the rows that match a specific condition.
WHERE Syntax
SELECT column1, column2, ... FROM table_name WHERE condition;
Example: Filter by Exact Match
Return customers from a specific country:
SELECT name, country FROM customers WHERE country = 'USA';
Comparison Operators
WHERE conditions commonly use these operators:
- = equal
- <> or != not equal
- > greater than
- < less than
- >= greater than or equal
- <= less than or equal
Example: Numeric Comparison
Return products with price greater than 100:
SELECT name, price FROM products WHERE price > 100;
Example: Date Comparison
Return orders placed after a specific date:
SELECT id, order_date FROM orders WHERE order_date > '2026-01-01';
Text Values and Quotes
Text values are usually written in single quotes. If your text contains a single quote, you must escape it (database-specific) or use safe parameter binding in your application code.
SELECT name FROM customers WHERE city = 'Istanbul';
Filtering with Multiple Conditions
You can combine conditions using AND / OR / NOT. These operators are covered in dedicated lessons, but here is the basic idea:
SELECT name, country, status FROM customers WHERE country = 'USA' AND status = 'active';
Use parentheses when combining multiple conditions to make the logic clear:
SELECT name, country, status FROM customers WHERE country = 'USA' AND (status = 'active' OR status = 'trial');
Filtering NULL Values
NULL means “no value”. You cannot compare NULL using = or !=. Use IS NULL or IS NOT NULL instead.
SELECT name, phone FROM customers WHERE phone IS NULL;
SELECT name, phone FROM customers WHERE phone IS NOT NULL;
Common WHERE Patterns
These patterns are covered in detail in later lessons, but you will see them often:
- IN for matching a list of values
- BETWEEN for matching a range
- LIKE for pattern matching
SELECT name, country
FROM customers
WHERE country IN ('USA', 'Canada');
SELECT name, price FROM products WHERE price BETWEEN 50 AND 100;
SELECT name FROM customers WHERE name LIKE 'A%';
Common Mistakes
- Forgetting quotes around text values
- Comparing NULL using = instead of IS NULL
- Mixing AND and OR without parentheses
Next Steps
Continue with SQL ORDER BY to sort your filtered results, then move on to AND / OR / NOT for more complex filtering logic.