Module 02 · lesson 1
WHERE: keeping only the rows you need
The electronics-department manager wants to see only their own products, not the whole store. You need to keep only the rows in the 'Electronics' category.
Keeping only the rows you need
So far we've taken every row in a table. But more often you need not all of them, only those matching a condition. That's what WHERE is for — a filter: only the rows for which the condition is true make it into the result.
For example, let's find one specific product by name:
SELECT name, price
FROM products
WHERE name = 'Clean Code';
The DBMS goes through every row, checks the condition name = 'Clean Code', and keeps only the one where the answer is "yes".
Text — in single quotes
Text values in a condition go in single quotes: 'Clean Code'. (Double quotes are a column name, remember?) Text comparison is case-sensitive: 'electronics' is not the same as 'Electronics'.
WHERE goes after FROM (and before ORDER BY, if there is one).
Your turn
The manager needs only the products in the 'Electronics' category. Output their name and price.
Summary
- WHERE keeps only the rows where the condition is true.
- Text in WHERE goes in single quotes:
'Clean Code'. - Case matters:
'a'≠'A'.