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;
namepricestock
Green Tea45.080
Croissant35.030
Potato Chips25.0200
Notebook30.075
Pen15.0300
5 rows

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';
namecity
Pim ChaiyaPhuket
Narin ThongdeeKhon Kaen
Siriporn KaewdeePhuket
Tawan RungruangKhon Kaen
4 rows

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';
idstatus
2shipped
3shipped
5cancelled
6shipped
8shipped
11shipped
14cancelled
7 rows

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');
namecityphone
Pim ChaiyaPhuket086-555-0101
Narin ThongdeeKhon Kaen082-777-8888
Siriporn KaewdeePhuketNULL
Tawan RungruangKhon Kaen085-123-4567
4 rows

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;
namecategory_idprice
Coffee155.0
Green Tea145.0
Orange Juice140.0
Banana Bread260.0
Cheesecake2120.0
5 rows

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;
namecategory_idprice
Coffee155.0
Banana Bread260.0
Cheesecake2120.0
3 rows

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');
namecity
Pim ChaiyaPhuket
Narin ThongdeeKhon Kaen
Siriporn KaewdeePhuket
Tawan RungruangKhon Kaen
4 rows

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);
idnameprice
1Coffee55.0
4Croissant35.0
7Potato Chips25.0
3 rows

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');
idorderedstatus
52026-10-01cancelled
142026-11-18cancelled
2 rows

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;
nameprice
Coffee55.0
Green Tea45.0
Orange Juice40.0
Croissant35.0
Banana Bread60.0
Notebook30.0
6 rows

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;
nameprice
no rows

NOT BETWEEN gives what lies outside the range, the other 6 products:

SELECT name, price FROM product WHERE price NOT BETWEEN 30 AND 60;
nameprice
Cheesecake120.0
Potato Chips25.0
Dried Mango90.0
Cashew Nuts150.0
Pen15.0
Highlighter25.0
6 rows

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';
idorderedstatus
52026-10-01cancelled
62026-10-03shipped
72026-10-10paid
82026-10-15shipped
92026-10-22paid
102026-10-28paid
6 rows

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'
101
1 row

The first is 1, the second 0 (missed), and the third, written as "before 1 November", catches it: 1.

  1. Show the customers who live in Bangkok and joined in 2026 or later.
  2. Show the products of category 3 or 4 (use IN) that are out of stock.
  3. Show the orders of November 2026 that were not cancelled.
  4. 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;
namecityjoined
Wichai SaelimBangkok2026-01-09
Lalita MeesukBangkok2026-08-03
2 rows
namecategory_idstock
Highlighter40
1 row
idorderedstatus
112026-11-02shipped
122026-11-05paid
132026-11-11paid
152026-11-25paid
4 rows
nameprice
Cheesecake120.0
Potato Chips25.0
Cashew Nuts150.0
Pen15.0
Highlighter25.0
5 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