WHERE Clause and Filtering
You'll learn how to filter database query results to retrieve only the rows that meet specific conditions.
What is the WHERE Clause?
When you query a database table, you often don't want every row—just the ones matching certain criteria. The WHERE clause acts like a filter or strainer that lets only qualifying rows pass through to your result set.
Think of it like searching your email inbox: instead of scrolling through thousands of messages, you type "from:boss" or "subject:urgent" to see only relevant emails. The WHERE clause does the same for database rows.
Basic Syntax
The WHERE clause appears after the table name in a SELECT statement:
SELECT column1, column2
FROM table_name
WHERE condition;
Comparison Operators
You filter rows using comparison operators that test conditions:
=equal to<>or!=not equal to<less than>greater than<=less than or equal to>=greater than or equal to
Example
SELECT name, age
FROM students
WHERE age >= 18;
This returns only students who are 18 or older. The database evaluates the condition age >= 18 for every row and includes only those where the condition is true.
SELECT product_name, price
FROM products
WHERE price < 50;
This retrieves products costing less than $50.
How It Works
The database engine scans each row in the table, evaluates your WHERE condition, and builds a result set containing only rows where the condition evaluates to true.
Key Takeaway: The WHERE clause filters query results by applying comparison operators to column values, returning only rows that satisfy the specified condition.