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;| name | price |
|---|---|
| Coffee | 55.0 |
| Banana Bread | 60.0 |
| Cheesecake | 120.0 |
| Dried Mango | 90.0 |
| Cashew Nuts | 150.0 |
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;| name | price | price > 50 |
|---|---|---|
| Coffee | 55.0 | 1 |
| Green Tea | 45.0 | 0 |
| Orange Juice | 40.0 | 0 |
| Croissant | 35.0 | 0 |
| Banana Bread | 60.0 | 1 |
| Cheesecake | 120.0 | 1 |
| Potato Chips | 25.0 | 0 |
| Dried Mango | 90.0 | 1 |
| Cashew Nuts | 150.0 | 1 |
| Notebook | 30.0 | 0 |
| Pen | 15.0 | 0 |
| Highlighter | 25.0 | 0 |
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
| Operator | Means | Example |
|---|---|---|
= | equal to | stock = 0 |
<> or != | not equal to | status <> 'paid' |
< | less than | price < 50 |
> | greater than | price > 50 |
<= | less than or equal to | price <= 30 |
>= | greater than or equal to | stock >= 100 |
Products that are out of stock:
SELECT name, stock FROM product WHERE stock = 0;| name | stock |
|---|---|
| Orange Juice | 0 |
| Highlighter | 0 |
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';| id | customer_id | status |
|---|---|---|
| 2 | 2 | shipped |
| 3 | 1 | shipped |
| 5 | 4 | cancelled |
| 6 | 5 | shipped |
| 8 | 6 | shipped |
| 11 | 8 | shipped |
| 14 | 3 | cancelled |
SELECT id, customer_id, status FROM orders WHERE status != 'paid';| id | customer_id | status |
|---|---|---|
| 2 | 2 | shipped |
| 3 | 1 | shipped |
| 5 | 4 | cancelled |
| 6 | 5 | shipped |
| 8 | 6 | shipped |
| 11 | 8 | shipped |
| 14 | 3 | cancelled |
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;| name | price |
|---|---|
| Potato Chips | 25.0 |
| Notebook | 30.0 |
| Pen | 15.0 |
| Highlighter | 25.0 |
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 |
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';| name | city |
|---|---|
| Somchai Jaidee | Bangkok |
| Anan Wongsa | Bangkok |
| Wichai Saelim | Bangkok |
| Lalita Meesuk | Bangkok |
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;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';| name | city |
|---|
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";| name | city |
|---|---|
| Somchai Jaidee | Bangkok |
| Malee Srisuk | Chiang Mai |
| Anan Wongsa | Bangkok |
| Pim Chaiya | Phuket |
| Narin Thongdee | Khon Kaen |
| Kanya Boonmee | Chiang Mai |
| Wichai Saelim | Bangkok |
| Siriporn Kaewdee | Phuket |
| Tawan Rungruang | Khon Kaen |
| Lalita Meesuk | Bangkok |
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';| id | ordered | status |
|---|---|---|
| 11 | 2026-11-02 | shipped |
| 12 | 2026-11-05 | paid |
| 13 | 2026-11-11 | paid |
| 14 | 2026-11-18 | cancelled |
| 15 | 2026-11-25 | paid |
Customers who joined before 2026:
SELECT name, joined FROM customer WHERE joined < '2026-01-01';| name | joined |
|---|---|
| Somchai Jaidee | 2025-01-15 |
| Malee Srisuk | 2025-03-02 |
| Anan Wongsa | 2025-04-20 |
| Pim Chaiya | 2025-06-11 |
| Narin Thongdee | 2025-08-30 |
| Kanya Boonmee | 2025-10-05 |
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' |
|---|---|---|---|
| 0 | 1 | 1 | 0 |
10 < 9is false (0), as you would expect.'10' < '9'is true (1): as text,'1'comes before'9', so'10'is "smaller", just as "apple" comes before "banana".'2026-09-02' < '2026-10-01'is true: inYYYY-MM-DDform, text order is date order.'2/9/2026' < '10/1/2026'is false, though 2 September comes before 1 October: text order looks at'2'and'1'first. That is why dates are kept asYYYY-MM-DD.
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 = 100 | 100 = '100' | 100 = 100.0 |
|---|---|---|
| 1 | 0 | 1 |
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';| name | price |
|---|---|
| Cheesecake | 120.0 |
| Cashew Nuts | 150.0 |
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'.
- Show the products with fewer than 20 left in stock (
stock). - Show the name and phone of the customers who live in
Chiang Mai. - Show the orders that were
cancelled. - 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';| name | stock |
|---|---|
| Orange Juice | 0 |
| Banana Bread | 12 |
| Cheesecake | 8 |
| Highlighter | 0 |
| name | phone |
|---|---|
| Malee Srisuk | 089-111-2222 |
| Kanya Boonmee | 084-246-1357 |
| id | customer_id | ordered | status |
|---|---|---|---|
| 5 | 4 | 2026-10-01 | cancelled |
| 14 | 3 | 2026-11-18 | cancelled |
| name | joined |
|---|---|
| Wichai Saelim | 2026-01-09 |
| Siriporn Kaewdee | 2026-02-14 |
| Tawan Rungruang | 2026-05-21 |
| Lalita Meesuk | 2026-08-03 |
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