Open In App

SQL | BETWEEN & IN Operator

Last Updated : 20 Dec, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

In SQL, the BETWEEN and IN operators are widely used for filtering data based on specific criteria. The BETWEEN operator helps filter results within a specified range of values, such as numbers, dates, or text, while the IN operator filters results based on a specific list of values. Both operators simplify data retrieval, enhancing the efficiency of SQL queries.

In this article, we will explain the BETWEEN and IN operators, their syntax, significance, and practical use cases with examples. Understanding their differences and when to use each operator is essential for building optimized and maintainable SQL queries.

SQL Between Operator

The SQL BETWEEN operator is used to test whether a value falls within a given range of values (inclusive). The values can be text, date, or numbers. It can be used in a SELECT, INSERT, UPDATE or DELETE statement. The SQL BETWEEN Condition will return the records where the expression is within the range of value1 and value2.

Syntax

SELECT column_name(s)
FROM table_name
WHERE column_name BETWEEN value1 AND value2;

Key Features:

  • Inclusive of both boundary values (value1 and value2).
  • Simplifies queries when working with continuous ranges.

Creating a Sample Table

Let’s create a sample table named Emp to demonstrate the use of the BETWEEN and IN operators. The Emp table contains employee data, including details such as name, country, age, and salary. These columns will be used to demonstrate filtering using the BETWEEN and IN operators.

Query:

CREATE TABLE Emp(
EmpID INT PRIMARY KEY,
Name VARCHAR(50),
Country VARCHAR(50),
Age int(2),
Salary int(10)
);

INSERT INTO Emp (EmpID, Name,Country, Age, Salary)
VALUES (1, 'Shubham', 'India','23','30000'),
(2, 'Aman ', 'Australia','21','45000'),
(3, 'Naveen', 'Sri lanka','24','40000'),
(4, 'Aditya', 'Austria','21','35000'),
(5, 'Nishant', 'Spain','22','25000');

Select * from Emp;

Output

Emp Table

Examples of SQL Between Operator

The BETWEEN operator simplifies queries by filtering data within a specified range, making it easier to retrieve relevant records. Below are examples demonstrating its application with numeric and date values, as well as with the NOT operator.

Example 1: Using BETWEEN with Numeric Values

List all the Employee’s Names who is having salary between 30000 and 45000.  This query filters employees whose salary falls within the range of 30000 to 45000, inclusive. The BETWEEN operator simplifies filtering compared to writing multiple conditions with AND

Query:

SELECT Name
FROM Emp
WHERE Salary
BETWEEN 30000 AND 45000;

Output

Using BETWEEN with Numeric Values

Example 2: Using BETWEEN with Date Values

Find all the Employees an Age Between 22 to 24. This query retrieves employees whose age is between 22 and 24, including the boundary values. The BETWEEN operator efficiently filters the range without requiring complex conditions.

Query:

SELECT Name
FROM Emp
where Age
BETWEEN '22' AND '24';

Output
 

Using BETWEEN with Date Values

Example 3: Using the NOT Operator with BETWEEN 

Find all the Employee names whose salary is not in the range of 30000 and 45000. The NOT BETWEEN clause excludes employees whose salary falls within the specified range, returning only those outside the range.

Query:

SELECT Name
FROM Emp
WHERE Salary
NOT BETWEEN 30000 AND 45000;

Output

NOT Operator with BETWEEN

SQL IN Operator

IN operator allows us to easily test if the expression matches any value in the list of values. It is used to remove the need for multiple OR conditions in SELECT, INSERT, UPDATE, or DELETE. We can also use NOT IN to exclude the rows in our list. We should note that any kind of duplicate entry will be retained. 

Syntax

SELECT column_name(s)
FROM table_name
WHERE column_name IN (list_of_values);

Key Features:

  • Ideal for filtering non-sequential values.
  • Handles duplicates in the list of values.

Examples of SQL IN Operator

The IN operator allows us to filter data by matching values against a specific list, simplifying queries that involve multiple conditions. Below are examples showcasing its usage with both IN and NOT IN clauses for effective data retrieval.

Example 1: Using IN Operator

Find the Fname, and Lname of the Employees who have a Salary equal to 30000, 40000, or 25000. The query retrieves employees whose salary matches any of the specified values in the list. This approach is more concise and efficient compared to using multiple OR conditions

 Query:

SELECT Name
FROM Emp
WHERE Salary IN (30000, 40000, 25000);

Output

IN Operator

Example 2: Using the NOT Operator with IN

Find the Fname and Lname of all the Employees who has a Salary not equal to 25000 or 30000. This query excludes employees with salaries of 25000 and 30000. The NOT IN clause ensures that all other employees are included in the result set.

Query:

SELECT Name
FROM Emp
WHERE Salary NOT IN (25000, 30000);

Output

NOT operator with IN

Difference Between BETWEEN and IN

Feature BETWEEN IN
Purpose Tests if a value falls within a specified range (inclusive). Tests if a value matches any value in a specified list.
Syntax column BETWEEN value1 AND value2 column IN (value1, value2, ...)
Data Types Supported Numbers, dates, or text values. Numbers, dates, text, or a list of values.
Range vs. List Used for checking values within a range (e.g., 1 to 10). Used for checking if a value is in a list (e.g., 1, 5, 10).
Example Usage Salary BETWEEN 30000 AND 45000 Salary IN (30000, 40000, 25000)
Exclusion Example Salary NOT BETWEEN 30000 AND 45000 Salary NOT IN (25000, 30000)
Efficiency Efficient for continuous ranges. Efficient for non-sequential values or when checking multiple conditions.

Conclusion

Both BETWEEN and IN are powerful SQL operators for filtering data effectively. The BETWEEN operator is ideal for queries involving continuous ranges, such as dates or salary bands, while the IN operator is better suited for discrete values or fixed conditions. By understanding their strengths, we can choose the right operator for your use case, ensuring optimal query performance and clear results

FAQs

Can the BETWEEN operator be used with text values?

Yes, the BETWEEN operator can be used with text values to filter results alphabetically within a range of strings, such as finding names that fall alphabetically between “A” and “D”.

What is the difference between NOT BETWEEN and NOT IN?

NOT BETWEEN is used to exclude results within a certain range, while NOT IN excludes specific values from a list.

Can the IN operator handle duplicate values?

Yes, the IN operator can handle duplicate values in the list, but it will treat them as the same and will not return duplicates in the result set.



Next Article

Similar Reads

three90RightbarBannerImg