Filtering Rows with WHERE Clause
Filtering Rows with WHERE: Predicates and Boolean Logic
Without filtering, every query would dump an entire table containing millions of rows over the network. The WHERE clause specifies search conditions (predicates) that filter rows before they are returned to the client. Only rows for which the WHERE predicate evaluates to TRUE are included in the result set.
1. Syntax of the WHERE Clause
The WHERE clause appears immediately after the FROM clause:
2. How the Database Executes WHERE
Understanding the logical execution order of a SQL query explains why query writing behaves the way it does:
Because the WHERE clause executes in Step 2, it filters out unwanted rows before expressions are calculated or memory is allocated for sorting in Step 4.
3. Filtering on Text, Numbers, and Dates
4. SQL Three-Valued Logic in WHERE Clauses
SQL does not use simple binary logic (True / False); it uses Three-Valued Logic:
- 1
TRUE: The condition is met. Row is included. - 2
FALSE: The condition is not met. Row is discarded. - 3
UNKNOWN: The condition involves aNULL. Row is discarded!
5. Best Practices & Common Pitfalls
- Avoid Functions on Indexed Columns in WHERE: Wrapping an indexed column inside a function prevents MySQL from using the index (Non-Sargable Query):
- String Quoting: Always enclose string and date literals in single quotes (
'Engineering','2026-01-01'). Numbers do not use quotes (5000).
Multiple Choice Questions
1. In the logical execution order of a SQL query, when is the WHERE clause evaluated?
A. After ORDER BY B. Before the SELECT clause and immediately after the FROM clause C. At the very end of execution D. Concurrently with the LIMIT clause Answer: B Explanation: The WHERE clause evaluates immediately after FROM, filtering candidate rows before SELECT projects columns or ORDER BY sorts results.
2. How does the WHERE clause treat a row when its condition evaluates to UNKNOWN due to a NULL value?
A. The row is included in the output B. The row is discarded from the result set C. The query aborts with an error D. The NULL is replaced with 0 Answer: B Explanation: In SQL three-valued logic, a row is only returned if the predicate evaluates strictly to TRUE; rows evaluating to FALSE or UNKNOWN are discarded.
3. Why is WHERE YEAR(created_at) = 2026 slower on a large indexed table than WHERE created_at >= '2026-01-01' AND created_at < '2027-01-01'?
A. MySQL does not have a YEAR function B. Wrapping an indexed column in a function prevents index utilization, forcing an expensive full table scan C. String dates are faster than integers D. AND clauses are faster than equal signs Answer: B Explanation: Applying functions to indexed columns makes predicates non-sargable, preventing the query engine from performing efficient B-Tree index range scans.
4. Which character is the standard SQL literal delimiter for string and date values in a WHERE clause?
A. Double quotes ("...") B. Single quotes ('...') C. Backticks (...) D. Curly braces ({...}) Answer: B Explanation: Standard SQL and MySQL use single quotes ('value') to enclose character string and date literals.
5. What will the query SELECT * FROM products WHERE stock_quantity = 0; return?
A. All products with stock greater than zero B. Only products whose stock_quantity is exactly equal to 0 C. All products regardless of stock D. Nothing, because 0 cannot be queried Answer: B Explanation: The equality operator (=) filters rows where the column's value matches the specified literal (0).
Comparison Operators in SQL
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
Related Lessons
| Previous Lesson | Next Lesson |
|---|---|
| SELECT Statement & Column Aliases | Comparison Operators in SQL |
Practice Quiz
Test your understanding of this lesson with 5 questions. Each question has one correct answer.