×

How to use the BETWEEN operator in SQL

In this entire SQL article, we will understand and learn about the BETWEEN operator concept and how to use it in SQL.

What is the BETWEEN operator in SQL?

The Between operator fetches records which exist between the starting and ending value in the given expression query. The SELECT between operators retrieves text, number, or date data.

The BETWEEN operator includes the starting value and the ending value.

The syntax of BETWEEN operators with the SELECT statement:

SELECT Column_Name_1, Column_Name_2, Column_Name_3 FROM Table_Name WHERE Column_Name BETWEEN VALUE_1 AND VALUE_2

Here, value_1 is starting value, and value_2 is its ending value.

The syntax of BETWEEN operators with the UPDATE statement:

UPDATE Table_Name SET Column_Name = value WHERE Column_Name BETWEEN VALUE_1 AND VALUE_2; 

The syntax of BETWEEN operators with the DELETE statement:

DELETE FROM TABLE_NAME WHERE COLUMN_NAME BETWEEN VALUE_1 AND VALUE_2;

There are following steps which help to learn for how to use the BETWEEN operator in the SQL query:

1 We have to create a newly named database. If you have already created a database (existing database), then use the old database by using the USE command.

2. After selecting the database, we will create a new table or use the existing table.

3 Add records in the newly created table using the INSERT statement

4 After adding records to the newly created table, we will display the data from the table using the SELECT statement.

Step 1: New Database or use old Database.

We have an existing database. So, we will use old database name, Company.

USE Company;

We can use the below syntax to create a new database that doesn't exist in the database.

CREATE DATABASE database_name;

After creating the new database, use the newly created database using the USE command.

Step 2: New Table or use old Table.

We have an old table. So, we will use the old table named Employees.

If you don’t have the old table, then use the below syntax to create the table.

CREATE TABLE table_name(

CREATE TABLE table_name(
Column_name_1 datatype(column size),
Column_name_2 datatype(column size),
Column_name_3 datatype(column size)
);

Step 3: Add new records to the new table.

Use below syntax to insert new records in the table:

INSERT INTO Table_Name VALUES(value_1, value_2, value_3);

The below syntax is used to display the data from the table:

SELECT * FROM Table_Name;

The following query will display the records of Employees

SELECT * FROM Employees;

The output of the above SELECT query is:

EMPLOYEEIDFIRST_NAMELAST_NAMESALARYCITYDEPARTMENTMANAGERID
1001VAIBHAVIMISHRA65500PUNEORACLE1
1002VAIBHAVSHARMA60000NOIDAC#5
1003NIKHILVANI50500JAIPURFMW2
2001PRACHISHARMA55500CHANDIGARHORACLE1
2002BHAVESHJAIN65500PUNEFMW2
2003RUCHIKAJAIN50000MUMBAIC#5
3001PRANOTISHENDE55500PUNEJAVA3
3002ANUJAWANRE50500JAIPURFMW2
3003DEEPAMJAUHARI58500MUMBAIJAVA3
4001RAJESHGOUD60500MUMBAITESTING4
4002ASHWINIBAGHAT54500NOIDAJAVA3
4003RUCHIKAAGARWAL60000DELHIORACLE1
5001ARCHITSHARMA55500DELHITESTING4
5002SANKETCHAUHAN70000HYDERABADJAVA3
5003ROSHANNEHTE48500CHANDIGARHC#5
6001RAHULNIKAM54500BANGALORETESTING4
6002ATISHJADHAV60500BANGALOREC#5
6003NIKITAINGALE65000HYDERABADORACLE1

Step 4: We are ready to use the BETWEEN operator in the queries.

Let’s understand the BETWEEN operator with the help of examples.

Example 1: Execute a query to fetch employee information from the employees' table where employee salary is between 48000 and 60000.

SELECT * FROM EMPLOYEES WHERE SALARY BETWEEN 48000 AND 60000;

In the above query, we fetched all the employee records from the employee table whose employees' Salary is between 48000 and 60000.

The output of the above query is shown as:

EMPLOYEEIDFIRST_NAMELAST_NAMESALARYCITYDEPARTMENTMANAGERID
1002VAIBHAVSHARMA60000NOIDAC#5
1003NIKHILVANI50500JAIPURFMW2
2001PRACHISHARMA55500CHANDIGARHORACLE1
2003RUCHIKAJAIN50000MUMBAIC#5
3001PRANOTISHENDE55500PUNEJAVA3
3002ANUJAWANRE50500JAIPURFMW2
3003DEEPAMJAUHARI58500MUMBAIJAVA3
4002ASHWINIBAGHAT54500NOIDAJAVA3
4003RUCHIKAAGARWAL60000DELHIORACLE1
5001ARCHITSHARMA55500DELHITESTING4
5003ROSHANNEHTE48500CHANDIGARHC#5
6001RAHULNIKAM54500BANGALORETESTING4
How to use the BETWEEN operator in SQL

As we can see in the output, only those employees records are displayed whose employee salary is between 48000 and 60000.

Example 2: Execute a query to fetch employee information from the employees table where the employee city is between Chandigarh and Pune.

SELECT * FROM EMPLOYEES WHERE CITY BETWEEN 'CHANDIGARH' AND 'PUNE'; 

This query will displaye all the employee information from the employee table whose employees’ city is between ‘Chandigarh’ and ‘Pune’.

The output of the above query is shown as:

EMPLOYEEIDFIRST_NAMELAST_NAMESALARYCITYDEPARTMENTMANAGERID
1001VAIBHAVIMISHRA65500PUNEORACLE1
1002VAIBHAVSHARMA60000NOIDAC#5
1003NIKHILVANI50500JAIPURFMW2
2001PRACHISHARMA55500CHANDIGARHORACLE1
2002BHAVESHJAIN65500PUNEFMW2
2003RUCHIKAJAIN50000MUMBAIC#5
3001PRANOTISHENDE55500PUNEJAVA3
3002ANUJAWANRE50500JAIPURFMW2
3003DEEPAMJAUHARI58500MUMBAIJAVA3
4001RAJESHGOUD60500MUMBAITESTING4
4002ASHWINIBAGHAT54500NOIDAJAVA3
4003RUCHIKAAGARWAL60000DELHIORACLE1
5001ARCHITSHARMA55500DELHITESTING4
5002SANKETCHAUHAN70000HYDERABADJAVA3
5003ROSHANNEHTE48500CHANDIGARHC#5
6003NIKITAINGALE65000HYDERABADORACLE1
How to use the BETWEEN operator in SQL

As we can see in the output, only those employees' records are displayed whose employee city is between 'Chandigarh' and 'Pune'.

Example 3: Execute a query to modify the employee salary by 1.2 whose employee city is between ‘Delhi’ and ‘Noida’.

UPDATE EMPLOYEES SET SALARY = SALARY * 1.2 WHERE CITY BETWEEN ‘DELHI’ AND ‘NOIDA’;

In the above statement, we increase the salary of those employees by 1.2 whose city is between 'Delhi' and 'Noida’. 

We will execute the SELECT query to verify whether the employee’s information is successfully modified or not.

SELECT * FROM EMPLOYEES WHERE CITY BETWEEN ‘DELHI’ AND ’NOIDA’;

 The output of the above query is shown as:

EMPLOYEEIDFIRST_NAMELAST_NAMESALARYCITYDEPARTMENTMANAGERID
1002VAIBHAVSHARMA72000NOIDAC#5
1003NIKHILVANI60600JAIPURFMW2
2003RUCHIKAJAIN60000MUMBAIC#5
3002ANUJAWANRE60600JAIPURFMW2
3003DEEPAMJAUHARI70200MUMBAIJAVA3
4001RAJESHGOUD72600MUMBAITESTING4
4002ASHWINIBAGHAT65400NOIDAJAVA3
4003RUCHIKAAGARWAL72000DELHIORACLE1
5001ARCHITSHARMA66600DELHITESTING4
5002SANKETCHAUHAN84000HYDERABADJAVA3
6003NIKITAINGALE72800HYDERABADORACLE1
How to use the BETWEEN operator in SQL

As we see in the output, employee records are updated whose city is between 'Delhi' and 'Noida’.

Example 4: Execute a query to remove the employee information from the employees' table of those employees whose Salary is between 50000 and 65000.

DELETE FROM EMPLOYEES WHERE SALARY BETWEEN 45000 AND 65000;

In the above statement, we are removing the employee information of those whose Salary is between 50000 and 65000.

We will execute the SELECT query to verify whether the employee’s information is successfully deleted or not.

SELECT * FROM EMPLOYEES;

The output of the above query is shown as:

EMPLOYEEIDFIRST_NAMELAST_NAMESALARYCITYDEPARTMENTMANAGERID
1001VAIBHAVIMISHRA65500PUNEORACLE1
1002VAIBHAVSHARMA72000NOIDAC#5
2002BHAVESHJAIN65500PUNEFMW2
3003DEEPAMJAUHARI70200MUMBAIJAVA3
4001RAJESHGOUD72600MUMBAITESTING4
4002ASHWINIBAGHAT65400NOIDAJAVA3
4003RUCHIKAAGARWAL72000DELHIORACLE1
5001ARCHITSHARMA66600DELHITESTING4
5002SANKETCHAUHAN84000HYDERABADJAVA3
5003ROSHANNEHTE48500CHANDIGARHC#5
6003NIKITAINGALE78000HYDERABADORACLE1
How to use the BETWEEN operator in SQL

In the above output, the records are deleted of those employees whose Salary is between 50000 and 65000.


Related Topics

How to Add Foreign Key in SQL?

How to Add Foreign Key in SQL Foreign key is an attribute or a set of attributes that references to primary key of same table or another table (relation). Foreign key creation along...

4 minutes read.

Commit and Rollback in SQL

In Structured Query Language, we have Data Definition Language (DDL) and Data Manipulation Language (DML) commands the same way we have Transaction Control Language (TCL) commands in the Structured Query...

4 minutes read.

SQL INSERT INTO SELECT

In this tutorial, we will help you to understand and learn how to copy records from one table and add them to another table in the SQL with the help...

5 minutes read.

SQL NOT Operator

SQL NOT is a Boolean operator used with the WHERE clause. NOT operator shows the records if the expression is false. When we use the NOT operator, we fetch only...

7 minutes read.

SQL Left Join

The SQL Left Join query displays all the records from the table and displays similar records from the right table. The query displays zero records if it doesn’t find any...

4 minutes read.

How to delete a row in SQL

Introduction To delete or remove the unused/unwanted rows from a table, DELETE command is used in SQL.DELETE command removes the entire record from a table.The DELETE statement can delete one or...

6 minutes read.

SQL Syntax

In this tutorial, we will help you understand about the different SQL Syntax concepts with the help of an example. Every Structured Query Language starts with Keywords like select, insert, update,...

5 minutes read.

SQL SELECT LIKE Operator

The SQL SELECT LIKE Operator tutorial helps us understand how to use the LIKE operator in the SELECT query with examples. The SQL SELECT LIKE Operator retrieves the records from the...

4 minutes read.

SQL Auto Increment

As we all know, In SQL for unique identification, we assign a column with the primary key. But in some tables, we find difficult to differentiate a column as a...

5 minutes read.

SQL INSERT INTO Values

This article will help you in getting a better understanding of a very important SQL INSERT INTO VALUES function. INSERT INTO statement is used to insert or add a new record...

4 minutes read.

Update Query in SQL

Update is an SQL command that is used to modify the data that in already present in database. Update is a command of DML. DML means Data Manipulation Language. Basically,...

3 minutes read.

SQL Handling Duplicate

Removing Duplicates using DISTINCT Keyword: By using the DISTINCT keyword in SQL we can remove duplicate characters from tables or databases. A table contains more duplicate values and duplicate values can cause...

4 minutes read.

SQL DROP Database

This tutorial will help us understand how to drop a database from the SQL Server with a few examples. The DROP command removes all the objects stored in the database and...

4 minutes read.

SQL Primary Key

A field, which contains unique data in a table, is called a PRIMARY KEY (PK). It means, a PRIMARY KEY field must contain unique data in the table. A PRIMARY KEY...

4 minutes read.

SQL CASE

This page contains all the information about SQL CASE. The CASE is an If-Else type of logical query used in the statement. The CASE in Structured Query Language is similar...

5 minutes read.

Types of SQL Commands

The Structured Query Language is used to deal with structured data. The data which are stored in the form of tables are structured data. These SQL commands store records or...

10 minutes read.

SQL IN vs SQL EXISTS

SQL IN vs SQL EXISTS This article discusses in detail about the IN and the EXISTS operators in SQL. It is a common question between developers that what is the difference...

3 minutes read.

SQL INTERSECT

SQL intersect operator is used to combine two or more SELECT statements, but it only displays the data similar to the SELECT statement. The syntax for the INTERSECT operation: SELECT COLUMN_NAME1, COLUMN_NAME2,...

3 minutes read.

SQL DELETE

In this tutorial, you will learn about the SQL DELETE concept by using examples. In Structured Query Language (SQL), we fetch the data from the database table using SELECT Statement and...

3 minutes read.

SQL CRUD Operation

In any computer language, CRUD operation is a foundation. CRUD operation rules are applied in databases as well. These operations are the basic operations used to perform with any database...

7 minutes read.