Kodewick
Lesson 2 of 21Contents

WHERE: picking rows

Lesson 2 of 21 · Part 1 · Querying data · about 15 minutes

The queries in the last lesson gave back every row in the table. Most of the time you want only some of them: the cheap products, the customers in London, the orders that haven't shipped. That's what WHERE does:

main.sql
1SELECT name, price2FROM products3WHERE price < 10;
Output
nameprice
Notebook4.50
Pens (10)6.00
USB cable8.50
Coffee mug9.90
4 rows

Of the 10 products, only these 4 cost less than 10.

How WHERE picks rows

After WHERE comes a condition. The database takes the whole table and looks at it one row at a time: if the condition is true, the row stays; if not, the row is dropped. Only then does SELECT pick the columns to show.

main.sql
1SELECT name, stock2FROM products3WHERE stock > 50;
Output
namestock
Notebook120
Pens (10)80
USB cable200
Coffee mug60
4 rows

Press Step on this one to see each stage: first the 10 rows that FROM gets, then WHERE marking each row as kept or dropped, and last SELECT keeping only two columns.

The clauses of a query always come in this order: SELECT, FROM, WHERE. By habit each clause starts its own line, which keeps the query easy to read.

Comparisons

These are the comparisons you'll use most in a condition:

WriteMeaning
=equal to
<>not equal to
< >less than, greater than
<= >=less than or equal, greater than or equal

Note that "equal" in SQL is a single =. Which product is sold out?

main.sql
1SELECT name, stock2FROM products3WHERE stock = 0;
Output
namestock
Desk lamp0
1 row

Employees who earn 4000 or more:

main.sql
1SELECT name, title, salary2FROM employees3WHERE salary >= 4000;
Output
nametitlesalary
Grace LeeManager6000
Omar HaddadSales lead4500
Lucia RomeroWarehouse lead4200
3 rows

Tip: MySQL also accepts !=, which means the same as <>. <> is the standard way to write it and works in every database.

Guess first

How many rows does the result have?

1SELECT name, price2FROM products3WHERE price >= 15;

Text needs quotes

Numbers are written as they are; text (also called a string) goes in single quotes. Which orders haven't shipped yet?

main.sql
1SELECT id, customer_id, status2FROM orders3WHERE status = 'pending';
Output
idcustomer_idstatus
72pending
91pending
124pending
3 rows

Not equal works the same way. Every order except the shipped ones:

main.sql
1SELECT id, status2FROM orders3WHERE status <> 'shipped';
Output
idstatus
4cancelled
7pending
9pending
12pending
4 rows

Forget the quotes and you get an error:

main.sql
1SELECT name2FROM customers3WHERE city = London;
Output
ERROR 1054 (42S22): Unknown column 'London' in 'where clause'

Without quotes, the database takes London for a column name, and the table has no such column, so it says it doesn't know a column called London. Write 'London' and it works.

Text has to match exactly: 'London UK' finds no rows. That isn't an error; the result is just empty. By default MySQL ignores the case of letters ('london' and 'London' count as equal); other databases may not.

Dates

Dates go in quotes too, written as 'year-month-day', like '2025-07-01'. Comparing dates means comparing earlier and later: the later date is the "bigger" one. Customers who joined on or after July 1:

main.sql
1SELECT name, joined2FROM customers3WHERE joined >= '2025-07-01';
Output
namejoined
Hana Sato2025-07-21
Ivan Petrov2025-08-30
Julia Costa2025-09-14
3 rows

Orders from before September 20:

main.sql
1SELECT id, order_date2FROM orders3WHERE order_date < '2025-09-20';
Output
idorder_date
12025-09-02
22025-09-05
32025-09-18
3 rows

The condition's columns needn't be selected

A column used in WHERE doesn't have to appear after SELECT. This query picks products by category, yet shows only their names:

main.sql
1SELECT name2FROM products3WHERE category = 'Kitchen';
Output
name
Water bottle
Coffee mug
2 rows

Practice

Write the query, then press Check. A hint costs nothing; the answer is there when you need it.

Exercise 1

Pricey products

Get the products that cost more than 20, with two columns: name and price.

It should print
nameprice
Backpack39.90
Headphones59.00
Desk lamp25.00
3 rows
exercise1.sql
1SELECT name, price2FROM products;
Output
Press Run or Ctrl+Enter
Exercise 2

Fix it: the quotes

This query should get the name and email of the customers in Paris, but it gives an error. Fix it.

It should print
nameemail
Ben Okafor[email protected]
Farah KhanNULL
2 rows
exercise2.sql
1SELECT name, email2FROM customers3WHERE city = Paris;
Output
Press Run or Ctrl+Enter
Exercise 3

October orders

Get the orders from October 1, 2025 on (that day included), with two columns: id and order_date.

It should print
idorder_date
52025-10-01
62025-10-03
72025-10-10
82025-10-12
92025-10-15
102025-10-20
112025-10-22
122025-10-25
8 rows
exercise3.sql
1SELECT id, order_date2FROM orders;
Output
Press Run or Ctrl+Enter

What you learned

  • WHERE condition keeps only the rows where the condition is true; the clauses go SELECT, FROM, WHERE.
  • Compare with =, <>, <, >, <=, >=; equal is a single =.
  • Text and dates go in single quotes; text without quotes is taken for a column name and gives ERROR 1054.
  • Dates are written 'year-month-day', and comparing them means earlier and later.
  • A column used in WHERE doesn't have to be in SELECT.