Before you start, you need
Keep the rows that meet a condition
To see the products in stock, you do not need the whole catalogue, only the ones with units available. WHERE chooses which rows appear using a condition such as stock > 0.
The condition is checked for each row. If it is true, the row goes into the result. Mouse has 18 units, so it passes; Pro Laptop has 0, so it is left out. The excluded product is still stored in the table.
Write WHERE after FROM. To compare with text, use single quotes: category = 'Accessories'. The numbers in these examples are written without quotes.
SELECT chooses columns; WHERE chooses rows. You can filter on stock even if you only show name. The condition decides which products you see, and the SELECT list decides what information you see about them.
SELECT columns
FROM table_name
WHERE condition;See it in action
We want to see only the products that are in stock.
Before running: Predict whether Pro Laptop, with a stock of 0, will appear when you ask for stock > 0.
SELECT name, stock
FROM products
WHERE stock > 0;| name | stock |
|---|---|
| Mouse | 18 |
| Keyboard | 7 |
| Monitor 24 | 4 |
| Headphones | 11 |
| Monitor 27 | 2 |
| SSD Drive | 9 |
| Cable USB-C | 30 |
- Each row is checked separately. Mouse, with a stock of 18, meets stock > 0; Pro Laptop, with a stock of 0, does not.
- The result keeps seven products. SELECT shows the name and stock of the rows that got through.
- An excluded product stays in the table. If you query products without WHERE, it appears again.
- To filter by category, put the text value in single quotes: category = 'Accessories'.
Practice with help
Getting things wrong is part of learning: you can run your query as many times as you like and nothing is lost. If you get stuck, open a hint (each one reveals a bit more) and save the solution for last.
Guided practice
Find one category
Write a query that returns the name and stock of the products in the Accessories category.
Expected result: 3 rows with the columns name, stock, in that order. Row order does not matter.
View tables and data
Open this panel to see the full data.
The question picks a category, not a stock quantity.
The condition is category = a text value. Text values go in single quotes.
Select name and stock from products and add WHERE category = 'Accessories'.
Solution explained
SELECT name, stock FROM products WHERE category = 'Accessories';The condition is checked separately for each product.
Your turn
Solve this variation. Spot the part that has changed before you look for help.
Practice
Change the criterion
Show the name and stock of out-of-stock products, that is, those with a stock of 0.
Expected result: 2 rows with the columns name, stock, in that order. Row order does not matter.
View tables and data
Open this panel to see the full data.
Out of stock describes the stock level, not the category.
The condition is applied to each row before its columns are shown.
Compare stock with the number 0.
Solution explained
SELECT name, stock FROM products WHERE stock = 0;Pro Laptop and Speaker meet the condition. Filtering shows them; it does not delete them.
Key takeaways
- WHERE filters rows.
- The condition must be true for a row to be kept.
- Text values go in single quotes.