The Wayback Machine - https://web.archive.org/web/20241001094858/https://www.geeksforgeeks.org/sql-between/
Open In App

SQL BETWEEN Operator

Last Updated : 13 Sep, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

The BETWEEN operator in SQL is a powerful tool for filtering records within a specific range. Whether applied to numeric, text or date columns it simplifies the process of retrieving data that falls within a particular boundary. In this article, we will explore the SQL BETWEEN operator with examples.

SQL BETWEEN Operator

  • The SQL BETWEEN operator is used to filter the result set within a specified range.
  • It can be applied to numeric, date and text columns.
  • The BETWEEN operator is inclusive, meaning it includes the start and end values in the result set.

Syntax:

The basic syntax of the ‘BETWEEN' the operator is as follows:

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

Parameters:

  • column_name(s): The name of the column(s) you want to retrieve data from.
  • table_name: The name of the table containing the data.
  • column_name: The name of the column you want to apply the BETWEEN filter to.
  • value1: The starting value of the range.
  • value2: The ending value of the range.

Examples of BETWEEN Operator

To understand the SQL BETWEEN Operator we will use the below below employees table with the various examples and their output.

EmployeeID FirstName LastName Age Salary HireDate
1 John Doe 28 50000 2021-01-15
2 Jane Smith 34 60000 2020-03-22
3 Sam Brown 45 75000 2018-07-10
4 Sue Wilson 25 48000 2021-10-01
5 Tom Harris 38 68000 2019-05-12

Example 1: NOT BETWEEN Text Values

Query:

Find employees whose last names are not alphabetically between ‘B’ and ‘S’.

SELECT FirstName, LastName 
FROM Employees
WHERE LastName NOT BETWEEN 'B' AND 'S';

Output:

FirstName LastName
Tom Harris

Explanation:

The query retrieves employees whose last names fall outside the alphabetical range of ‘B’ to ‘S’. Only Tom Harris qualifies, as ‘Harris’ comes after ‘B’ but before ‘S’.

Example 2: BETWEEN Dates

Query:

Find employees hired between January 1, 2020 and December 31, 2021.

SELECT FirstName, LastName, HireDate 
FROM Employees
WHERE HireDate BETWEEN '2020-01-01' AND '2021-12-31';

Output:

FirstName LastName HireDate
Jane Smith 2020-03-22
John Doe 2021-01-15
Sue Wilson 2021-10-01

Explanation:

This query returns employees who were hired during the years 2020 and 2021. The BETWEEN operator is used to filter the HireDate field, returning records within the specified date range.

Example 3: NOT BETWEEN

Query:

Find employees whose age is not between 30 and 40.

SELECT FirstName, LastName, Age 
FROM Employees
WHERE Age NOT BETWEEN 30 AND 40;

Output:

FirstName LastName Age
John Doe 28
Sam Brown 45
Sue Wilson 25

Explanation:

This query retrieves employees whose age does not fall between 30 and 40. The NOT BETWEEN operator is used here to exclude employees within that age range. John, Sam, and Sue meet this condition, as their ages are outside the 30-40 range.

Example 4: BETWEEN with IN

Query:

Find employees whose salaries are between 50,000 and 70,000 and whose first names are either ‘John’, ‘Sue’, or ‘Tom’.

SELECT FirstName, LastName, Salary 
FROM Employees
WHERE Salary BETWEEN 50000 AND 70000
AND FirstName IN ('John', 'Sue', 'Tom');

Output:

FirstName LastName Salary
John Doe 50000
Sue Wilson 48000

Explanation:

This query filters employees with salaries between 50,000 and 70,000 and also checks if their first names are in the list (‘John’, ‘Sue’, ‘Tom’). Only John and Sue satisfy both conditions.

Conclusion

The SQL BETWEEN operator is an efficient and easy-to-use tool for filtering data across a range. Whether used for text values, numbers, or dates, its inclusiveness and flexibility make it an essential tool for SQL queries. With its capability to combine with other operators like NOT and IN, it allows for complex data retrieval while maintaining simplicity.

FAQs

What is the SQL BETWEEN operator used for?

The SQL BETWEEN operator is used to filter the result set to include records within a specified range of values. It is inclusive, meaning both the start and end values are included.

Can the BETWEEN operator be used with text values?

Yes, the BETWEEN operator can be applied to text values to filter records within an alphabetical range.

What is the difference between BETWEEN and NOT BETWEEN?

The BETWEEN operator includes values that fall within the specified range, while NOT BETWEEN excludes values within that range.



Previous Article
Next Article

Similar Reads

Difference between Structured Query Language (SQL) and Transact-SQL (T-SQL)
Structured Query Language (SQL): Structured Query Language (SQL) has a specific design motive for defining, accessing and changement of data. It is considered as non-procedural, In that case the important elements and its results are first specified without taking care of the how they are computed. It is implemented over the database which is drive
2 min read
Configure SQL Jobs in SQL Server using T-SQL
In this article, we will learn how to configure SQL jobs in SQL Server using T-SQL. Also, we will discuss the parameters of SQL jobs in SQL Server using T-SQL in detail. Let's discuss it one by one. Introduction :SQL Server Agent is a component used for database task automation. For Example, If we need to perform index maintenance on Production ser
7 min read
SQL Server Between Operator
In SQL Server, BETWEEN Operator is used to filter data within a specified range. It allows you to retrieve records where a column's value falls within a certain range of values. for example, let's say you have a list of ages, and you want to find people who are between 20 and 30 years old. You can use the BETWEEN Operator to make Query very easy an
5 min read
Difference between (Equal) = and IN operator in SQL
Equal (=) and IN operators are both comparison operators in SQL, the only difference between = and IN operator is that '=' operator compares two values, but IN operator compares a value against set of values. Here, we will learn the difference between the SQL '=' and IN operator by understanding each of them. Difference between = and IN Operator= O
2 min read
PL/SQL BETWEEN Operator
When working with databases, filtering data based on certain criteria is a common requirement. PL/SQL offers various operators to facilitate this, with one of the most effective being the BETWEEN operator. This operator enables us to select values that lie in a specific range whether those values are numbers, dates or text. In this article, We will
4 min read
SQL | BETWEEN & IN Operator
In SQL, the BETWEEN and IN operators are used for filtering data based on specific criteria. The BETWEEN operator is used to filter results that fall within a specified range of values such as numbers, dates or text while the IN operator is used to filter results based on a list of specific values. In this article, we will see  BETWEEN & IN Ope
4 min read
SQL | Concatenation Operator
Prerequisite: Basic Select statement, Insert into clause, SQL Create Clause, SQL Aliases || or concatenation operator is use to link columns or character strings. We can also use a literal. A literal is a character, number or date that is included in the SELECT statement. Let's demonstrate it through an example: Syntax: SELECT id, first_name, last_
2 min read
SQL | Alternative Quote Operator
This post is a continuation of the SQL Concatenation Operator. Now, suppose we want to use apostrophe in our literal value but we can't use it directly. See Incorrect code: SELECT id, first_name, last_name, salary, first_name||' has salary's '||salary AS "new" FROM one So above we are getting error, because Oracle server thinking that the first apo
3 min read
Inequality Operator in SQL
Pre-requisite - Operators in SQL In this article, we will discuss the overview of operators in SQL, and mainly our focus will be on Inequality Operators in SQL. Let's discuss them one by one.SQL Operators are widely used to access information on the basis of conditions. In the Inequality operator, we will check the condition on a specific column. O
3 min read
How to Use the IN Operator with a SubQuery in SQL?
In this article, we will see the use of IN Operator with a SubQuery in SQL. IN operator is used to compare the column values with the list of values. The list of values is defined after IN query in SQL. If we don't know the exact list of values to be compared, we can generate the list of values using the query. Syntax: Without subquery: SELECT colu
2 min read
SQL Server IN Operator
IN clause in SQL Server is a logical operator to check a list of values, if available in a specific table column. This keyword helps in filtering data from the query results, based on a set of values. Using this IN clause we can specify multiple values in a WHERE clause, making your queries more readable and efficient. The IN clause can be used as
3 min read
SQL Server LIKE Operator
The SQL Server LIKE operator is similar to the SQL LIKE Operator. It retrieves the rows by matching a string or character-specified pattern. A pattern can include regular characters wildcard characters or both. It is often used in the WHERE clause of the SELECT, UPDATE, and DELETE statements to filter rows based on pattern matching. It is a flexibl
4 min read
SQL Server AND Operator
Logical operators are used for combining multiple boolean expressions which are combinations of results of multiple conditions which are formed by the comparators. Some of the logical operators are AND, OR, EXISTS, IN, LIKE, BETWEEN, etc, Logical operators are frequently used and very handy for testing multiple conditions. It is also used for combi
3 min read
SQL Server OR Operator
Logical operators are used for combining multiple boolean expressions which are a combination of results of multiple conditions which are formed by the comparators. Some of the logical operators are AND, OR, EXISTS, IN, LIKE, BETWEEN, etc, Logical operators are very frequently used and are very handy for testing multiple conditions. The logical ope
3 min read
SQL Server ANY Operator
In SQL Server there are many different types of Logical operators like ANY, AND, LIKE, EXISTS, NOT, IN, and BETWEEN. The logical operators are used to perform conditional checks in SQL Queries. These operators are very useful to filter and compare single or multiple data in SQL Queries. In this article let us discuss the 'ANY' operator available in
3 min read
SQL Server ALL Operator
In SQL Server the logical operators are used to perform conditional checks in SQL Queries. There are many different types of Logical operators like ANY, AND, LIKE, EXISTS, NOT, IN, and BETWEEN in SQL Server. These operators are very useful to filter and compare single or multiple data in SQL Queries. In this article let us discuss the 'ALL' operato
3 min read
SQL Server EXCEPT Operator
Structured Query Language also known as SQL is a tool for storing, managing, and manipulating relational databases. SQL Server is a popular relational database management system (RDBMS) developed by Microsoft, providing a variety of operators to perform different operations on given datasets. One such operator is the EXCEPT operator, which allows u
4 min read
SQL IS NOT NULL Operator
In SQL, the IS NOT NULL Operator is a logical operator. Its application is just the opposite of IS NULL Operator. Unlike IS NULL Operator, this operator returns True, if the value is not a NULL value. It is usually used with a WHERE clause to filter the table data. It filters out the data and separates it from records where specified columns have N
5 min read
SQL Server INTERSECT Operator
In SQL Server, the INTERSECT operator is a kind of set operator that is used to combine the results of two SELECT statements and return rows which is common between them. In this article, We will explore the syntax, key concepts, and practical examples of using the INTERSECT operator. Whether you are managing customer data, tracking orders, and han
5 min read
SQL Server INTERSECT and EXCEPT Operator
Multiple SQL Queries may return duplicate values in recordsets from each query result. There could be situations when a single resultset is needed to be returned by combining multiple SQL queries. In SQL Server, there are many SET operators like UNION, EXCEPT, and INTERSECT to merge multiple results of select statements to a single result set. The
4 min read
SQL IN Operator
The SQL IN operator filters data based on a list of specific values. In general, we can only use one condition in the WHEN clause, but the IN operator allows us to specify multiple values. In this article, we will learn about the IN operator in SQL by understanding its syntax and examples. IN OperatorThe IN Operator in SQL is used to specify multip
4 min read
SQL LIKE Operator
SQL LIKE operator is used with the WHERE clause to search for a specified pattern in a column. LIKE operator finds and returns the rows that fit in the given pattern. LIKE operator is case-insensitive by default in most database systems. This means that if you search for "apple" using the LIKE operator, it will return results that include "Apple",
4 min read
SQL NOT Operator
SQL NOT Operator is used to return the opposite result or negative result. It is a logical operator in SQL, that negates the boolean expression in the WHERE clause. It is mostly used to specify what should not be included in the results table. NOT SyntaxSELECT column1, colomn2, â€Ķ FROM table_name WHERE NOT condition; Demo SQL DatabaseBelow is a sele
2 min read
SQL NOT EQUAL Operator
SQL NOT EQUAL Operator is a comparison operator used to check whether two expressions are equal. This operator is represented by " != " or "<>". NOT EQUAL Operator in SQLNOT EQUAL Operator in SQL is used to compare two values and return if they are not equal. This operator returns boolean values. If given expressions are equal, the operator r
4 min read
SQL UNION Operator
SQL UNION operator combines result sets of two or more SELECT statements into one results set. UNION Operator in SQL The UNION operator in SQL is used to combine the result set of multiple SELECT statements and return one result set. There are some rules for using the SQL UNION operator. Rules for SQL UNION Each table used within UNION must have th
3 min read
PL/SQL UNION Operator
In PL/SQL (Procedural Language/Structured Query Language), the UNION operator is one of the most commonly used set operators. It combines the result sets of two or more SELECT statements into a single result set while removing any duplicate rows. In this article, We will learn about PL/SQL UNION Operator with the help of examples and so on. PL/SQL
3 min read
PL/SQL MINUS Operator
The MINUS operator in PL/SQL (and Oracle SQL) is a powerful tool used to compare two datasets and identify differences. It effectively subtracts the result set of the second query from the result set of the first query, returning all rows from the first query that do not appear in the second. In this article, We will learn about PL/SQL MINUS Operat
4 min read
PL/SQL NOT Operator
PL/SQL, an extension of SQL in Oracle, offers various operators that allow us to perform logical operations on data. One such operator is the NOT operator, which is used to negate a condition, meaning it will return true if the condition is false and vice versa. The NOT operator is commonly used in conjunction with other logical operators to filter
6 min read
PL/SQL ALL, ANY Operator
The ALL and ANY operators in PL/SQL are powerful tools used to compare a given value against a set of values returned by a subquery. These operators allow for more dynamic and flexible queries by evaluating conditions against multiple results. The ALL operator checks if a condition holds true for every value in the result set while the ANY operator
4 min read
PL/SQL UNION ALL Operator
In PL/SQL, the UNION ALL operator is a powerful tool that allows us to combine the results of two or more SELECT queries into a single result set. Unlike the UNION operator, which eliminates duplicate rows, UNION ALL includes all rows, including duplicates. This makes it faster and more efficient when we need to retain all records from the combined
4 min read
Article Tags :