ShareYourGroupShareYourGroupSQL

Lesson 4 of 26 · see the practice database

AND, OR, NOT, IN and BETWEEN

One condition is often not enough. You want the stationery that costs under 100 rupees, or the customers from Delhi or Mumbai. This lesson joins conditions together and then shows two shortcuts, IN and BETWEEN.

AND: both must be true

SELECT name, category, price
FROM products
WHERE category = 'Stationery' AND price < 100;

Result

namecategoryprice
Notebook 200 pagesStationery60
Ball Pen pack of 10Stationery90

2 rows

A row is returned only if it passes both tests. Each AND you add makes the result smaller or leaves it the same.

OR: at least one must be true

SELECT name, city
FROM customers
WHERE city = 'Delhi' OR city = 'Mumbai';

Result

namecity
Ravi KumarDelhi
Kabir KhanMumbai
Imran AliDelhi
Joy ThomasMumbai

4 rows

A row is returned if it passes either test. Each OR you add makes the result larger or leaves it the same.

Each side of an OR must be a complete condition. WHERE city = 'Delhi' OR 'Mumbai' is wrong, though it may not give an error. Write the column name again on each side.

NOT: the opposite

NOT turns a condition around.

SELECT name, category
FROM products
WHERE NOT category = 'Grocery';

Result

namecategory
Notebook 200 pagesStationery
Ball Pen pack of 10Stationery
Geometry BoxStationery
School BagStationery
LED Bulb 9 WElectronics
Extension BoardElectronics
EarphonesElectronics
Power BankElectronics
Steel Bottle 1 LHome
Pressure Cooker 3 LHome
Bedsheet DoubleHome
Wall ClockHome

12 rows

This is the same as category <> 'Grocery'. NOT is most useful together with IN, BETWEEN and LIKE, as you will see below.

Mixing AND and OR: use brackets

When a condition has both AND and OR, the database does the AND parts first, the same way multiplication comes before addition in arithmetic. That can give a result you did not intend.

Suppose you want products in Grocery or Home that cost more than 400 rupees.

SELECT name, category, price
FROM products
WHERE category = 'Grocery' OR category = 'Home' AND price > 400;

Result

namecategoryprice
Basmati Rice 5 kgGrocery650
Toor Dal 1 kgGrocery160
Sunflower Oil 1 LGrocery145
Tea 500 gGrocery280
Steel Bottle 1 LHome450
Pressure Cooker 3 LHome1450
Bedsheet DoubleHome899

7 rows

Look at the result. Cheap groceries are in it. The database read the condition as "Grocery, or (Home and over 400)". Brackets fix it.

SELECT name, category, price
FROM products
WHERE (category = 'Grocery' OR category = 'Home') AND price > 400;

Result

namecategoryprice
Basmati Rice 5 kgGrocery650
Steel Bottle 1 LHome450
Pressure Cooker 3 LHome1450
Bedsheet DoubleHome899

4 rows

Make it a rule: whenever AND and OR appear together, add brackets, even where they are not strictly needed. The reader, and you in a month, will not have to think about it.

IN: one of a list

A chain of OR tests on the same column is long to write. IN does the same with a list.

SELECT name, city
FROM customers
WHERE city IN ('Delhi', 'Mumbai', 'Pune');

Result

namecity
Ravi KumarDelhi
Kabir KhanMumbai
Arjun MehtaPune
Imran AliDelhi
Joy ThomasMumbai

5 rows

It works with numbers too.

SELECT id, customer_id, status
FROM orders
WHERE customer_id IN (1, 2);

Result

idcustomer_idstatus
11delivered
22delivered
41delivered
72delivered
121delivered
162delivered
201pending

7 rows

NOT IN gives the rows whose value is not in the list.

SELECT name, department
FROM employees
WHERE department NOT IN ('Sales', 'Support');

Result

namedepartment
Anita KapoorManagement
Farah SheikhWarehouse
Gopal ReddyWarehouse

3 rows

BETWEEN: inside a range

BETWEEN tests whether a value lies in a range. Both ends are included.

SELECT name, price
FROM products
WHERE price BETWEEN 100 AND 300;

Result

nameprice
Toor Dal 1 kg160
Sunflower Oil 1 L145
Tea 500 g280
Geometry Box120
LED Bulb 9 W110

5 rows

This is the same as price >= 100 AND price <= 300. The smaller value must come first. BETWEEN 300 AND 100 returns nothing.

It works well with dates. These are the orders placed in the first three months of 2026.

SELECT id, order_date
FROM orders
WHERE order_date BETWEEN '2026-01-01' AND '2026-03-31';

Result

idorder_date
12026-01-05
22026-01-12
32026-01-20
42026-02-02
52026-02-14
62026-02-28
72026-03-03
82026-03-15
92026-03-22

9 rows

A warning for later: if a column stores a time of day as well as a date, BETWEEN '2026-01-01' AND '2026-03-31' misses almost all of the last day, because 31 March at 10 in the morning is later than 31 March at midnight. For such columns, write >= '2026-01-01' AND < '2026-04-01'. Our practice database stores plain dates, so BETWEEN is safe here.

NOT BETWEEN gives the rows outside the range.

SELECT name, salary
FROM employees
WHERE salary NOT BETWEEN 35000 AND 60000;

Result

namesalary
Anita Kapoor95000
Gopal Reddy30000
Nikhil Jain33000

3 rows

Putting it together

Here is a realistic question: which delivered or shipped orders were placed from April to June 2026?

SELECT id, order_date, status
FROM orders
WHERE status IN ('delivered', 'shipped')
  AND order_date BETWEEN '2026-04-01' AND '2026-06-30';

Result

idorder_datestatus
102026-04-01delivered
112026-04-18delivered
122026-05-06delivered
132026-05-19shipped
142026-06-02delivered
152026-06-21delivered

6 rows

When a condition gets long, put each part on its own line, starting with AND or OR. It reads like a checklist.

Try it yourself

Exercise 1. Show the name, category and price of Electronics products that cost less than 450 rupees.

Solve it in the practice box

Show one answer
SELECT name, category, price FROM products WHERE category = 'Electronics' AND price < 450;
namecategoryprice
LED Bulb 9 WElectronics110
Extension BoardElectronics399

2 rows

Exercise 2. Show the name and state of the customers who live in Punjab or Kerala. Use IN.

Solve it in the practice box

Show one answer
SELECT name, state FROM customers WHERE state IN ('Punjab', 'Kerala');
namestate
Asha VermaPunjab
Meena GillPunjab
Priya NairKerala
Tara SinghPunjab

4 rows

Exercise 3. Show the name and stock of the products whose stock is between 20 and 50, both included.

Solve it in the practice box

Show one answer
SELECT name, stock FROM products WHERE stock BETWEEN 20 AND 50;
namestock
Basmati Rice 5 kg40
Geometry Box45
School Bag20
Extension Board35
Bedsheet Double25
Wall Clock30

6 rows

Examples on this page were run with SQLite 3.50.4 on 12 November 2026. The results shown are the real results.

This is an independent learning site. It is not connected with the makers of any database product. MySQL, PostgreSQL, SQL Server, Oracle and SQLite are named only so that you know where a piece of SQL works. The shop, its customers and its staff in the practice database are made up.