MYSQL

10. MySQL IN and BETWEEN – Powerful Filtering Techniques

MySQL IN and BETWEEN – Powerful Filtering Techniques

When you need to filter data based on multiple values or a range of values, MySQL provides two incredibly handy operators: IN and BETWEEN.

In this tutorial, we’ll explore how to use IN and BETWEEN in practical scenarios using employees and departments tables.


πŸ“Œ What is MySQL IN?

The IN operator allows you to check if a value matches any value in a list of values.

βœ… Syntax

SELECT column1, column2
FROM table_name
WHERE column_name IN (value1, value2, ...);

πŸ§ͺ Examples Using IN

1️⃣ Find Employees in Department IDs 2, 4, and 7

SELECT employee_id, name, department_id
FROM employees
WHERE department_id IN (2, 4, 7);

🟒 Result: Returns all employees who belong to departments 2, 4, or 7.


2️⃣ Find Employees with Designations ‘Manager’, ‘Analyst’, ‘HR’

SELECT employee_id, name, designation
FROM employees
WHERE designation IN ('Manager', 'Analyst', 'HR');

🟒 Result: Returns employees with specific job roles.


3️⃣ Find Departments Named β€˜Sales’, β€˜IT’, or β€˜Marketing’

SELECT department_id, department_name
FROM departments
WHERE department_name IN ('Sales', 'IT', 'Marketing');

🟒 Result: Returns only those departments that match the list exactly.


4️⃣ Combine with NOT

SELECT employee_id, name
FROM employees
WHERE department_id NOT IN (3, 5);

🟒 Result: Excludes employees from departments 3 and 5.


πŸ“Œ What is MySQL BETWEEN?

The BETWEEN operator is used to filter results within a specific range. It works for numbers, dates, and even text (alphabetical order).

βœ… Syntax

SELECT column1, column2
FROM table_name
WHERE column_name BETWEEN value1 AND value2;

πŸ“ Note: BETWEEN is inclusive, meaning it includes both value1 and value2.


πŸ§ͺ Examples Using BETWEEN

1️⃣ Find Employees with Salaries Between 30,000 and 50,000

SELECT employee_id, name, salary
FROM employees
WHERE salary BETWEEN 30000 AND 50000;

🟒 Result: Returns employees whose salaries fall within that range.


2️⃣ Find Employees Who Joined Between Two Dates

SELECT employee_id, name, joining_date
FROM employees
WHERE joining_date BETWEEN '2023-01-01' AND '2023-12-31';

🟒 Result: Filters employees who joined during the year 2023.


3️⃣ Find Departments with IDs Between 1 and 5

SELECT department_id, department_name
FROM departments
WHERE department_id BETWEEN 1 AND 5;

🟒 Result: Returns department records with IDs from 1 to 5.


βœ… Combine IN and BETWEEN in One Query

SELECT employee_id, name, salary, department_id
FROM employees
WHERE department_id IN (1, 2, 3)
AND salary BETWEEN 40000 AND 60000;

🟒 Result: Returns employees from selected departments whose salary falls in the specified range.


🧠 Summary Table

Operator Use Case Example
IN Match any value in a list WHERE dept_id IN (2, 4, 5)
BETWEEN Match value within a range WHERE salary BETWEEN 30k AND 5

πŸš€ Real-World Tips (From Acesoftech Academy)

  • Prefer IN over multiple OR conditions – it’s cleaner and easier to maintain.

  • When using BETWEEN with dates, always use ISO format (YYYY-MM-DD).

  • Combine both to write powerful, concise filters for dashboards and reports.


🏁 Conclusion

Both IN and BETWEEN are must-have tools in your MySQL toolbox. Whether you’re selecting multiple specific values or filtering based on a range, these operators help make your queries shorter and more readable.

Try these queries on your own employees and departments tables β€” and build your SQL skills one query at a time!

Leave a Reply

Your email address will not be published. Required fields are marked *