SQL WHERE Clause
Definition
The SQL WHERE clause filters rows returned by a query, including only rows that satisfy a specified condition.
Introduction to SQL WHERE Clause
The WHERE clause is how SQL narrows down data from potentially millions of rows to exactly what you need. It evaluates a boolean expression for each row, and only rows where the condition is TRUE are included in the result. WHERE supports comparison operators, logical operators (AND, OR, NOT), range checks (BETWEEN), pattern matching (LIKE), and set membership (IN).
Syntax
SELECT columns FROM table WHERE condition1 AND condition2 OR condition3;
Examples
Basic Comparison
SELECT name, salary FROM employees WHERE salary > 70000;
Returns all employees earning more than $70,000 using the greater-than operator.
Try in PlaygroundMultiple Conditions with AND / OR
SELECT name, department, city
FROM employees
WHERE department = 'Engineering'
AND city IN ('New York', 'San Francisco');Combines two filters: must be in Engineering AND located in one of the two cities.
Try in PlaygroundRange with BETWEEN
SELECT name, hire_date FROM employees WHERE hire_date BETWEEN '2022-01-01' AND '2023-12-31';
BETWEEN is inclusive on both ends — returns employees hired in 2022 or 2023.
Try in PlaygroundPattern Matching with LIKE
SELECT name, email FROM employees WHERE email LIKE '%@gmail.com';
The % wildcard matches zero or more characters. This finds all Gmail addresses.
Try in PlaygroundCommon Mistakes
Comparing NULL with = (use IS NULL or IS NOT NULL instead)
Using LIKE '%term%' on large tables without indexes — very slow full table scans
Confusing WHERE and HAVING — WHERE filters rows before grouping, HAVING filters groups after
Operator precedence errors — AND is evaluated before OR, always use parentheses to be explicit
Frequently Asked Questions
What is the difference between WHERE and HAVING in SQL?
How do you check for NULL values in a WHERE clause?
Can WHERE use subqueries?
What does LIKE do in SQL WHERE?
Related SQL Topics
Practice This in the SQL Playground
Write real queries, see live results, and master SQL WHERE Clause hands-on. 100% free.