SQL WHERE: Filtering Rows
Most of the time you don't want every row, just the ones that matter.
The
WHERE
clause keeps only the rows that meet a condition, and throws the rest away
before they ever reach your results.
It goes right after
FROM, and you can compare with
=,
<,
>, and more. Conditions can be combined with
AND
and
OR, which Chapter 3 covers.
select column_one, column_two
from table_name
where number_column < 10 Check filters in small steps
When a
WHERE
clause has more than one condition, test one condition first, then add the
next one. If the result suddenly drops to zero rows, the last condition is
the place to inspect.
Text comparisons need quotes, numbers usually don't, and dates should use a clear date format. If a row is missing a value, ordinary comparisons may skip it; later lessons cover SQL NULL handling.
Schema · Garden Shop Table · products 9 columns · 24 rows
One row per product, with price, cost, and inventory levels.
Show the product name and price of every product that costs less than $10.
Run your query to inspect the result.
Expected answer
- Columns: product_name, price.
- Rows: 9 products priced under $10, in any order.
- The cheapest is Tomato Seeds 'Roma' at 2.75; Garden Trowel at 9.5 just makes it.
Related
Comparisons, AND/OR, IN, BETWEEN, and LIKE.
SELECT, WHERE, ORDER BY, and LIMIT at a glance.
Debug filters that match nothing.
Nested SELECTs, IN, EXISTS, and correlation.
UNION, UNION ALL, INTERSECT, and EXCEPT.
A stray NULL drops every row. Use NOT EXISTS.