SQL Basics · Lesson 2 of 9

Filtering Rows

WHERE filters rows. Only rows for which the condition is true are returned:

SELECT name, price
FROM products
WHERE price > 20;

Comparisons

OperatorMeaning
=equal to
<> or !=not equal to
> and <greater than, less than
>= and <=greater or equal, less or equal

Text needs quotes

Numbers are written as they are. Text values go in single quotes:

SELECT name FROM products WHERE category = 'Home';

The text inside the quotes must match exactly, including capital letters: 'home' would find nothing.

Several conditions

Combine conditions with AND (both must be true) and OR (at least one must be true):

SELECT name, price, stock
FROM products
WHERE category = 'Home' AND price < 20;

When you mix AND and OR, use brackets to make clear what belongs together:

WHERE (category = 'Home' OR category = 'Travel') AND stock > 0

Without the brackets, AND is applied first, which is rarely what you meant.

Tables in the practice database
TableColumns
customersid, name, city, joined
productsid, name, category, price, stock
ordersid, customer_id, product_id, quantity, ordered

Every run starts from a fresh copy of the database, so nothing you do can break it.

Your Task

  1. Write a query that returns the name, price and stock of all products in the Stationery category that are in stock (a stock greater than 0).
  • The query uses WHERE
  • The result has the columns name, price and stock
  • Only Stationery products that are in stock are returned