beginnerCore SQL100% Free

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

SQL
SELECT columns
FROM table
WHERE condition1
  AND condition2
  OR condition3;

Examples

1

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 Playground
2

Multiple 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 Playground
3

Range 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 Playground
4

Pattern 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 Playground

Common 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?
WHERE filters individual rows before any GROUP BY aggregation. HAVING filters groups after GROUP BY. Use WHERE to filter raw rows, HAVING to filter aggregated results like COUNT or SUM.
How do you check for NULL values in a WHERE clause?
Use IS NULL or IS NOT NULL: SELECT * FROM orders WHERE shipped_date IS NULL; You cannot use = NULL because NULL is not equal to anything, including itself.
Can WHERE use subqueries?
Yes. For example: SELECT name FROM employees WHERE dept_id IN (SELECT id FROM departments WHERE budget > 1000000); This is called a subquery or inner query.
What does LIKE do in SQL WHERE?
LIKE performs pattern matching on string columns. % matches any sequence of characters, _ matches exactly one character. Example: WHERE name LIKE 'J%' finds all names starting with J.

Related SQL Topics

Practice This in the SQL Playground

Write real queries, see live results, and master SQL WHERE Clause hands-on. 100% free.