3. WHERE: only the rows you want

Filter a table's rows with a condition: comparisons, text, numbers and dates.

Filtering rows with WHERE

SELECT on its own gives every row of a table. Most of the time you want only some of them: products over 50 baht, customers in Bangkok, orders from November. That is what WHERE is for. It comes after FROM, followed by a condition (a test each row either passes or not):

SELECT name, price FROM product WHERE price > 50;
nameprice
Coffee55.0
Banana Bread60.0
Cheesecake120.0
Dried Mango90.0
Cashew Nuts150.0
5 rows

The shop has 12 products; 5 of them cost more than 50 baht, and only those 5 come out.

How does it work? The database takes the rows one by one and works out the condition for each. A row for which the condition is true stays; any other row is dropped. You can see the condition's answer for yourself by putting it after SELECT: true is 1, false is 0:

SELECT name, price, price > 50 FROM product;
namepriceprice > 50
Coffee55.01
Green Tea45.00
Orange Juice40.00
Croissant35.00
Banana Bread60.01
Cheesecake120.01
Potato Chips25.00
Dried Mango90.01
Cashew Nuts150.01
Notebook30.00
Pen15.00
Highlighter25.00
12 rows

The rows with 1 in the last column are exactly the 5 rows of the first example.

The order of the parts is fixed: SELECT ... FROM ... WHERE .... The condition may use any column of the table, even one that isn't in the SELECT list.

Comparison operators

OperatorMeansExample
=equal tostock = 0
<> or !=not equal tostatus <> 'paid'
<less thanprice < 50
>greater thanprice > 50
<=less than or equal toprice <= 30
>=greater than or equal tostock >= 100

Products that are out of stock:

SELECT name, stock FROM product WHERE stock = 0;
namestock
Orange Juice0
Highlighter0
2 rows

Orders that are not paid (here: shipped or cancelled). <> and != mean the same; <> is the standard SQL spelling, != the one many programming languages use:

SELECT id, customer_id, status FROM orders WHERE status <> 'paid';
idcustomer_idstatus
22shipped
31shipped
54cancelled
65shipped
86shipped
118shipped
143cancelled
7 rows
SELECT id, customer_id, status FROM orders WHERE status != 'paid';
idcustomer_idstatus
22shipped
31shipped
54cancelled
65shipped
86shipped
118shipped
143cancelled
7 rows

Both give the same 7 orders. "Less than or equal to" keeps the value itself: price <= 30 includes the 30-baht notebook:

SELECT name, price FROM product WHERE price <= 30;
nameprice
Potato Chips25.0
Notebook30.0
Pen15.0
Highlighter25.0
4 rows

The column you test needn't be in the result. Here we test category_id but show only the names (category 4 is Stationery):

SELECT name FROM product WHERE category_id = 4;
name
Notebook
Pen
Highlighter
3 rows

Text goes in single quotes

A number is written as it is: 50, 0, 12.5. Text is written between single quotes: 'Bangkok', 'paid'. The quotes tell the database "this is a value, not a name":

SELECT name, city FROM customer WHERE city = 'Bangkok';
namecity
Somchai JaideeBangkok
Anan WongsaBangkok
Wichai SaelimBangkok
Lalita MeesukBangkok
4 rows

Leave the quotes out and the database takes the word for the name of a column, which doesn't exist:

SELECT name, city FROM customer WHERE city = Bangkok;
Error: no such column: Bangkok

Comparing text with = is exact, capitals included. 'bangkok' with a small b is not 'Bangkok', so this finds nobody:

SELECT name, city FROM customer WHERE city = 'bangkok';
namecity
no rows

Spaces count too: 'Bangkok ' with a space at the end isn't 'Bangkok' either. Chapter 5 shows LIKE, which ignores capitals.

What about double quotes? In SQL, double quotes are for names (of columns and tables), not for text. SQLite and Phai SQL forgive "Bangkok" when there is no column of that name, but that only hides the mistake until the day the word is also a column's name. Here "city" is the column city, compared with itself, so every row passes:

SELECT name, city FROM customer WHERE city = "city";
namecity
Somchai JaideeBangkok
Malee SrisukChiang Mai
Anan WongsaBangkok
Pim ChaiyaPhuket
Narin ThongdeeKhon Kaen
Kanya BoonmeeChiang Mai
Wichai SaelimBangkok
Siriporn KaewdeePhuket
Tawan RungruangKhon Kaen
Lalita MeesukBangkok
10 rows

All 10 customers come out, not one whose city is the word "city".

Text always goes in single quotes: 'Bangkok'. Double quotes are for names only.

Comparing dates

The shop keeps its dates as text in the form 'YYYY-MM-DD' (year, month, day, with leading zeros): '2026-09-02'. Written this way, dates sort and compare correctly as plain text, because the year comes first, then the month, then the day. So <, > and = work on them just as on numbers.

Orders from 1 November 2026 on:

SELECT id, ordered, status FROM orders WHERE ordered >= '2026-11-01';
idorderedstatus
112026-11-02shipped
122026-11-05paid
132026-11-11paid
142026-11-18cancelled
152026-11-25paid
5 rows

Customers who joined before 2026:

SELECT name, joined FROM customer WHERE joined < '2026-01-01';
namejoined
Somchai Jaidee2025-01-15
Malee Srisuk2025-03-02
Anan Wongsa2025-04-20
Pim Chaiya2025-06-11
Narin Thongdee2025-08-30
Kanya Boonmee2025-10-05
6 rows

That gives 5 orders and 6 customers. Chapter 8 tells the whole story of dates.

Numbers and text are not the same

The number 10 and the text '10' look alike, but the database compares them differently. Numbers are compared by size. Text is compared character by character from the left, the way a dictionary orders words:

SELECT 10 < 9, '10' < '9', '2026-09-02' < '2026-10-01', '2/9/2026' < '10/1/2026';
10 < 9'10' < '9''2026-09-02' < '2026-10-01''2/9/2026' < '10/1/2026'
0110
1 row

A number and a piece of text are never equal by themselves, even when they look the same:

SELECT 100 = 100, 100 = '100', 100 = 100.0;
100 = 100100 = '100'100 = 100.0
101
1 row

100 = '100' is false. 100 = 100.0 is true: both are numbers, with the same value.

A column is different. price is declared as a number (REAL), so when you compare it with text, the database first turns the text into a number. This works, but it's better to write numbers without quotes:

SELECT name, price FROM product WHERE price > '100';
nameprice
Cheesecake120.0
Cashew Nuts150.0
2 rows

Write a number as a number (100), text in single quotes ('Bangkok'), and a date as text in 'YYYY-MM-DD' form. Keep a column of numbers as a number type: a price kept as text would sort '100' before '9'.

  1. Show the products with fewer than 20 left in stock (stock).
  2. Show the name and phone of the customers who live in Chiang Mai.
  3. Show the orders that were cancelled.
  4. Show the customers who joined in 2026 or later.
Show the answer
SELECT name, stock FROM product WHERE stock < 20;
SELECT name, phone FROM customer WHERE city = 'Chiang Mai';
SELECT * FROM orders WHERE status = 'cancelled';
SELECT name, joined FROM customer WHERE joined >= '2026-01-01';
namestock
Orange Juice0
Banana Bread12
Cheesecake8
Highlighter0
4 rows
namephone
Malee Srisuk089-111-2222
Kanya Boonmee084-246-1357
2 rows
idcustomer_idorderedstatus
542026-10-01cancelled
1432026-11-18cancelled
2 rows
namejoined
Wichai Saelim2026-01-09
Siriporn Kaewdee2026-02-14
Tawan Rungruang2026-05-21
Lalita Meesuk2026-08-03
4 rows

Phai (bamboo), given as a gift of knowledge: free to use, learn from and pass on · every example on this page was run on Phai SQL and checked against SQLite when the page was made