WHERE Clause

Beginner

⏱️ 10 mins read

What You'll Learn

WHERE filters rows before aggregation. It supports operators: =, !=, <, >, <=, >=, LIKE, IN, BETWEEN, IS NULL, IS NOT NULL. Combine conditions with AND (both must be true), OR (either must be true), NOT (inverts condition).

Syntax

SELECT cols FROM table WHERE condition;
SELECT cols FROM table WHERE c1 AND c2;
SELECT cols FROM table WHERE col IN (v1, v2);
SELECT cols FROM table WHERE col BETWEEN x AND y;

Example

-- AND / OR
SELECT * FROM employees
WHERE salary > 80000 AND department = 'Engineering';

-- IN and BETWEEN
SELECT * FROM employees
WHERE department IN ('HR', 'Finance', 'Engineering');

SELECT * FROM orders
WHERE order_date BETWEEN '2024-01-01' AND '2024-12-31';

-- LIKE pattern matching
SELECT * FROM customers WHERE name LIKE 'A%';

Common Mistakes

Using '= NULL' instead of 'IS NULL'. NULL comparisons with = always return UNKNOWN (not TRUE), so rows are never matched. Also, LIKE '%text' cannot use indexes — prefer 'text%' for performance.

Interview Tips

BETWEEN is inclusive on both ends. Know that OR conditions can prevent index usage — sometimes rewriting as UNION of two queries is faster. Explain precedence: AND is evaluated before OR.

Practice

Find all orders placed in 2024 with a total between $100 and $500, for customers whose names start with 'J'.