WHERE Clause
Filters rows returned by a query using a boolean condition.
SELECT columns FROM table_name WHERE condition;This static page keeps the syntax and examples indexed for search, while the coding app handles interactive exploration and saved references.
What it does
Overview
Filters rows returned by a query using a boolean condition.
The WHERE clause acts as a logical filter that evaluates a predicate for every row provided by the FROM clause. It uses three-valued logic, where comparisons result in TRUE, FALSE, or UNKNOWN (typically due to NULL values). For optimal performance, predicates should be 'SARGable' (Search ARgumentable), meaning they should avoid applying functions to indexed columns—for instance, using `olumn >= '2023-01-01'` is significantly faster than `YEAR(olumn) = 2023` because the latter prevents the engine from utilizing B-tree indexes. Unlike the HAVING clause, which filters post-aggregation, the WHERE clause filters individual records before any grouping or summarization occurs. When handling date comparisons, it is standard practice to use ISO 8601 strings (YYYY-MM-DD) or explicit casting to ensure deterministic behavior across different database locales and engines.
Quick reference
Syntax
SELECT columns FROM table_name WHERE condition;
Inputs
Parameters
See it in practice
Examples
Basic Conditional Comparison
SELECT name, price FROM products WHERE price >= 150.00;A result set containing only products with a price of 150 or higher.
Uses a standard comparison operator to filter numeric data.
Compound Predicates and Precedence
SELECT * FROM orders WHERE status = 'Processed' AND (priority = 'High' OR total_amount > 1000);Processed orders that are either marked as high priority or have a large total.
Utilizes parentheses to force the OR condition to evaluate before the AND condition, preventing logic errors in complex filters.
Pattern Matching with Wildcards
SELECT username FROM users WHERE email LIKE '%@company.com';A list of usernames associated with the specific company domain.
Employs the LIKE operator and the '%' wildcard to perform partial string matching on column data.
Debug faster
Common Errors
LogicError
Cause: Attempting to evaluate NULL values using the equality operator (=). In SQL, NULL represents an unknown value, and any direct comparison with it (except via IS) returns UNKNOWN.
Fix: Use the IS NULL or IS NOT NULL operators instead of the equals sign.
SELECT * FROM employees WHERE supervisor_id = NULL; -- Incorrect: returns 0 rowsReferenceError
Cause: Referencing a column alias defined in the SELECT clause within the WHERE clause. SQL evaluates the WHERE clause before the SELECT projection is processed.
Fix: Repeat the original column expression in the WHERE clause or use a Subquery/CTE.
SELECT price * 0.1 AS tax FROM sales WHERE tax > 5; -- Incorrect: 'tax' is not yet definedRuntime support
Compatibility
Core filtering feature
Source: MDN Web Docs
Common questions
Frequently Asked Questions
Filters rows returned by a query using a boolean condition.
condition: Boolean expression to filter rows (e.g., age >= 18).
LogicError: Use the IS NULL or IS NOT NULL operators instead of the equals sign. ReferenceError: Repeat the original column expression in the WHERE clause or use a Subquery/CTE.