Using SQL BETWEEN and IN Operators: Comprehensive Data Filtering
🚀 What Will You Learn in This Guide?
This guide describes two powerful operators that make it easier to filter data in SQL queries:
With BETWEEN, you can determine whether a value is within a certain range or not.
With IN you will find out whether a value is included in a certain cluster.
All examples are explained through a practical sales database in MySQL environment.
🧠 Technical Summary
Subject: Data filtering with BETWEEN and IN operators in SQL
Purpose: To ensure that queries return only rows in the desired ranges or clusters
Steps:
- Connecting to MySQL and creating a sample table
- Interval query with BETWEEN
- Cluster membership check with IN
- Creating a dynamic cluster with subquery
- Using NOT BETWEEN and NOT IN
⚙️ 1. Sample Database Setup
Connect to MySQL Server
mysql -u genixnode_satisci -p
➡️ Logs into MySQL terminal.
Create Database and Table
CREATE DATABASE satis_vt;
USE satis_vt;
➡️ Creates and activates a new “satis_vt” database.
CREATE TABLE satislar (
isim VARCHAR(20),
bilesen INT,
cihaz INT,
aygit INT
);
➡️ Creates the table to track sales team performance.
Add Sample Data
INSERT INTO satislar VALUES
('Tamer', 12, 22, 18),
('Burak', 19, 8, 13),
('Leyla', 7, 29, 3),
('Bora', 16, 16, 15),
('Serap', 17, 2, 31),
('Mert', 5, 9, 7),
('Hakan', 14, 2, 0);
➡️ Adds sample sales data to the table.
🎯 2. BETWEEN Operator: Range Inquiry
The BETWEEN operator checks whether a value lies between two specified limits. The range always works including the lower limit and upper limit.
Filtering on Numeric Range
SELECT isim, bilesen
FROM satislar
WHERE bilesen BETWEEN 14 AND 19;
➡️ Lists employees with component sales between 14 and 19.
Spacing Control Between Columns
SELECT *
FROM satislar
WHERE aygit BETWEEN cihaz AND bilesen;
➡️ device value returns the lines falling between the device and component values.
Filtering by Alphabetical Range
SELECT isim
FROM satislar
WHERE isim BETWEEN 'A' AND 'M';
➡️ Returns employees whose names are alphabetically between “A” and “M”.
🔗 3. IN Operator: Cluster Membership Check
The IN operator checks whether the value in a column is included in a particular set.
Search with Fixed Set
SELECT isim, cihaz
FROM satislar
WHERE cihaz IN (2, 9, 22);
➡️ returns rows with device sales of 2, 9 or 22.
🧩 4. Dynamic Set with Subquery
Subqueries provide a dynamic dataset to the IN operator.
Step A – Create the Auxiliary Table
CREATE TABLE ornek_kume (asal_sayi INT);
➡️ Creates the auxiliary table to be used in the subquery.
Step B – Add Sample Data
INSERT INTO ornek_kume VALUES
(2), (3), (5), (7), (11), (13), (17), (19), (23), (29);
➡️ Adds the first 10 prime numbers to the table.
Step C – Using IN with Subquery
SELECT isim, bilesen
FROM satislar
WHERE bilesen IN (SELECT asal_sayi FROM ornek_kume);
The ➡️ component returns rows whose value matches values in the table of prime numbers.
💬 Frequently Asked Questions (FAQ)
- Are There Limits on BETWEEN Operator?
Yes. The expression BETWEEN 10 AND 20 includes both 10 and 20.
- Is there a difference between IN and OR?
IN (1, 2, 3) = column = 1 OR column = 2 is equivalent to OR column = 3, but IN queries are more readable and shorter.
- Can I Check for NULL Values with IN?
No. For NULL you should use IS NULL or IS NOT NULL.
- What do NOT BETWEEN and NOT IN do?
These expressions return data outside the specified range or set.
- How Do BETWEEN and IN Affect Performance?
Performance is very high on indexed columns. However, using IN with subqueries can slow down on large datasets.
🧭 Result
In this guide, you learned about range queries with BETWEEN and set-based filtering with IN in SQL. Now you can write your queries more readable, more performant and more flexible.
You can immediately create your SQL database on the GenixNode platform to analyze your data on a powerful infrastructure.

