Introduction
The WHERE clause lets you filter the rows returned by a SELECT statement so that only rows meeting a specific condition are included. Without WHERE, a query returns every row in the table; with WHERE, you can narrow results to exactly what you need.
Cricket analogy: A scout reviewing every player in the IPL auction list without filters sees thousands of names, but adding a condition like 'only fast bowlers under 25' narrows it to exactly the shortlist a franchise needs.
Syntax
SELECT column1, column2, ...
FROM table_name
WHERE condition;
-- Combining conditions
SELECT column1
FROM table_name
WHERE condition1 AND condition2
OR condition3;Explanation
The WHERE clause evaluates a boolean condition for each row; only rows for which the condition is true are included in the result. Conditions can use comparison operators (=, <>, <, >, <=, >=), logical operators (AND, OR, NOT), and special operators such as BETWEEN, IN, LIKE, and IS NULL. WHERE is applied before any grouping or aggregation happens.
Cricket analogy: A selector's condition might be 'strike rate > 140 AND average > 35' evaluated per batter; only those satisfying both comparisons make the T20 squad, while BETWEEN could check 'age between 19 and 23' for youth picks.
Example
-- employees table: id, first_name, last_name, department, salary, hire_date
SELECT first_name, last_name, salary
FROM employees
WHERE department = 'Engineering'
AND salary > 80000;Output
Only rows where the department is exactly 'Engineering' and salary exceeds 80000 are returned, for example: 'Alice | Nguyen | 95000'. Employees in other departments or with lower salaries are excluded entirely from the result set.
Cricket analogy: A query for 'team = Mumbai Indians AND runs > 400' would return only 'Rohit | Sharma | 2023-final' style rows, silently dropping every other franchise's matches and lower-scoring games.
Key Takeaways
- WHERE filters individual rows before any aggregation occurs.
- Use AND/OR/NOT to combine multiple conditions; parentheses control precedence.
- Use IS NULL / IS NOT NULL to test for missing values, not = NULL.
- BETWEEN, IN, and LIKE provide concise ways to express common filter patterns.
Practice what you learned
1. What is the purpose of the WHERE clause?
2. Which operator correctly checks for a NULL value in a column?
3. What does the following filter return: WHERE salary BETWEEN 50000 AND 70000?
4. In WHERE department = 'Sales' AND salary > 60000, which rows are returned?
Was this page helpful?
You May Also Like
SELECT Statement Basics
Learn how to retrieve data from a table using the SELECT statement, the foundation of every SQL query.
Sorting with ORDER BY
Control the order of query results using the ORDER BY clause with ascending or descending sorts.
DISTINCT and Basic Operators
Remove duplicate rows with DISTINCT and use comparison, logical, and pattern-matching operators in queries.