4. AND, OR, IN and BETWEEN
Several conditions at once, lists of values, and ranges, without being caught by the order SQL reads them in.
AND: every condition must hold
One condition is often not enough. "Products under 50 baht that are in stock" is two conditions, joined with AND. A row stays only when both are true:
SELECT name, price, stock FROM product WHERE price < 50 AND stock > 0;| name | price | stock |
|---|---|---|
| Green Tea | 45.0 | 80 |
| Croissant | 35.0 | 30 |
| Potato Chips | 25.0 | 200 |
| Notebook | 30.0 | 75 |
| Pen | 15.0 | 300 |
Seven products cost less than 50 baht, but Orange Juice and Highlighter have none left (stock 0), so 5 remain. Joining more conditions with AND makes the result smaller, never bigger.
OR: one of them is enough
OR keeps a row when at least one of the conditions is true. Customers who live in Phuket or in Khon Kaen:
SELECT name, city FROM customer WHERE city = 'Phuket' OR city = 'Khon Kaen';| name | city |
|---|---|
| Pim Chaiya | Phuket |
| Narin Thongdee | Khon Kaen |
| Siriporn Kaewdee | Phuket |
| Tawan Rungruang | Khon Kaen |
Each OR makes the result bigger: 2 customers in Phuket and 2 in Khon Kaen, 4 in all.
In everyday speech we say "customers in Phuket and Khon Kaen", but no customer lives in both cities at once. In SQL that is an OR: the city is Phuket, or the city is Khon Kaen.
NOT: the opposite
NOT turns true into false and false into true. Orders whose status is not paid:
SELECT id, status FROM orders WHERE NOT status = 'paid';| id | status |
|---|---|
| 2 | shipped |
| 3 | shipped |
| 5 | cancelled |
| 6 | shipped |
| 8 | shipped |
| 11 | shipped |
| 14 | cancelled |
That gives the same 7 orders as status <> 'paid' in chapter 3. NOT is most useful in front of a longer condition in brackets: customers who live neither in Bangkok nor in Chiang Mai:
SELECT name, city, phone FROM customer WHERE NOT (city = 'Bangkok' OR city = 'Chiang Mai');| name | city | phone |
|---|---|---|
| Pim Chaiya | Phuket | 086-555-0101 |
| Narin Thongdee | Khon Kaen | 082-777-8888 |
| Siriporn Kaewdee | Phuket | NULL |
| Tawan Rungruang | Khon Kaen | 085-123-4567 |
AND comes before OR: use brackets
Here is a mistake almost everyone makes once. We want the products of category 1 (Drinks) or category 2 (Bakery) that cost more than 50 baht:
SELECT name, category_id, price FROM product WHERE category_id = 1 OR category_id = 2 AND price > 50;| name | category_id | price |
|---|---|---|
| Coffee | 1 | 55.0 |
| Green Tea | 1 | 45.0 |
| Orange Juice | 1 | 40.0 |
| Banana Bread | 2 | 60.0 |
| Cheesecake | 2 | 120.0 |
Green Tea at 45 and Orange Juice at 40 shouldn't be here. Why are they? Because SQL works out AND before OR, the way arithmetic does * before + (1 + 2 * 3 is 7, not 9). So the database read our condition as:
category_id = 1 OR (category_id = 2 AND price > 50)
"Every drink, plus the bakery items over 50". Brackets say what we meant:
SELECT name, category_id, price FROM product WHERE (category_id = 1 OR category_id = 2) AND price > 50;| name | category_id | price |
|---|---|---|
| Coffee | 1 | 55.0 |
| Banana Bread | 2 | 60.0 |
| Cheesecake | 2 | 120.0 |
Now only 3 products are left, each one over 50 baht.
Whenever AND and OR meet in one condition, put brackets around the OR part. Even when the result happens to be right without them, the brackets tell the next reader what you meant.
The order is: brackets first, then comparisons (=, <...), then NOT, then AND, and OR last.
IN: one of a list
A long row of ORs on the same column reads better as IN, with the values in brackets, separated by commas:
SELECT name, city FROM customer WHERE city IN ('Phuket', 'Khon Kaen');| name | city |
|---|---|
| Pim Chaiya | Phuket |
| Narin Thongdee | Khon Kaen |
| Siriporn Kaewdee | Phuket |
| Tawan Rungruang | Khon Kaen |
The same 4 customers as the OR example above. IN works with numbers too:
SELECT id, name, price FROM product WHERE id IN (1, 4, 7);| id | name | price |
|---|---|---|
| 1 | Coffee | 55.0 |
| 4 | Croissant | 35.0 |
| 7 | Potato Chips | 25.0 |
NOT IN keeps the rows whose value is not in the list. Orders that are neither paid nor shipped:
SELECT id, ordered, status FROM orders WHERE status NOT IN ('paid', 'shipped');| id | ordered | status |
|---|---|---|
| 5 | 2026-10-01 | cancelled |
| 14 | 2026-11-18 | cancelled |
Two orders are left, both cancelled. (Watch out for NOT IN with an empty value, NULL, in its list: chapter 5 shows why.)
BETWEEN: a range, both ends included
price BETWEEN 30 AND 60 is short for price >= 30 AND price <= 60. Both ends count: a product at exactly 30 or exactly 60 is in:
SELECT name, price FROM product WHERE price BETWEEN 30 AND 60;| name | price |
|---|---|
| Coffee | 55.0 |
| Green Tea | 45.0 |
| Orange Juice | 40.0 |
| Croissant | 35.0 |
| Banana Bread | 60.0 |
| Notebook | 30.0 |
Notebook at 30 and Banana Bread at 60 are both there: 6 products. Write the smaller value first. Backwards, the range is empty and nothing matches:
SELECT name, price FROM product WHERE price BETWEEN 60 AND 30;| name | price |
|---|
NOT BETWEEN gives what lies outside the range, the other 6 products:
SELECT name, price FROM product WHERE price NOT BETWEEN 30 AND 60;| name | price |
|---|---|
| Cheesecake | 120.0 |
| Potato Chips | 25.0 |
| Dried Mango | 90.0 |
| Cashew Nuts | 150.0 |
| Pen | 15.0 |
| Highlighter | 25.0 |
BETWEEN works on dates in YYYY-MM-DD form as well. The orders of October 2026:
SELECT id, ordered, status FROM orders WHERE ordered BETWEEN '2026-10-01' AND '2026-10-31';| id | ordered | status |
|---|---|---|
| 5 | 2026-10-01 | cancelled |
| 6 | 2026-10-03 | shipped |
| 7 | 2026-10-10 | paid |
| 8 | 2026-10-15 | shipped |
| 9 | 2026-10-22 | paid |
| 10 | 2026-10-28 | paid |
If a column holds a date and a time, BETWEEN '2026-10-01' AND '2026-10-31' misses everything after midnight on the last day: as text, '2026-10-31 14:00' is greater than '2026-10-31'. For such columns, write "before the next day" instead: >= '2026-10-01' AND < '2026-11-01'.
SELECT '2026-10-31' BETWEEN '2026-10-01' AND '2026-10-31', '2026-10-31 14:00' BETWEEN '2026-10-01' AND '2026-10-31', '2026-10-31 14:00' < '2026-11-01';| '2026-10-31' BETWEEN '2026-10-01' AND '2026-10-31' | '2026-10-31 14:00' BETWEEN '2026-10-01' AND '2026-10-31' | '2026-10-31 14:00' < '2026-11-01' |
|---|---|---|
| 1 | 0 | 1 |
The first is 1, the second 0 (missed), and the third, written as "before 1 November", catches it: 1.
- Show the customers who live in
Bangkokand joined in 2026 or later. - Show the products of category 3 or 4 (use
IN) that are out of stock. - Show the orders of November 2026 that were not cancelled.
- Show the products that cost less than 30 or more than 100 baht.
Show the answer
SELECT name, city, joined FROM customer WHERE city = 'Bangkok' AND joined >= '2026-01-01';
SELECT name, category_id, stock FROM product WHERE category_id IN (3, 4) AND stock = 0;
SELECT id, ordered, status FROM orders WHERE ordered BETWEEN '2026-11-01' AND '2026-11-30' AND status <> 'cancelled';
SELECT name, price FROM product WHERE price < 30 OR price > 100;| name | city | joined |
|---|---|---|
| Wichai Saelim | Bangkok | 2026-01-09 |
| Lalita Meesuk | Bangkok | 2026-08-03 |
| name | category_id | stock |
|---|---|---|
| Highlighter | 4 | 0 |
| id | ordered | status |
|---|---|---|
| 11 | 2026-11-02 | shipped |
| 12 | 2026-11-05 | paid |
| 13 | 2026-11-11 | paid |
| 15 | 2026-11-25 | paid |
| name | price |
|---|---|
| Cheesecake | 120.0 |
| Potato Chips | 25.0 |
| Cashew Nuts | 150.0 |
| Pen | 15.0 |
| Highlighter | 25.0 |
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