×

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

SQL SELECT Statement

The SQL SELECT statement is used to retrieve the data from the tables. We can also retrieve the selected records from the table using the query condition. The SQL SELECT...

5 minutes read.

SQL Between Operator

SQL Between operator is a logical operator in the Structured Query Language. The Between operator is used to retrieve data within the range specified in the condition in the query. The...

7 minutes read.

How to use COUNT in SQL?

How to use COUNT in SQL Introduction COUNT( ) is an aggregate function in SQL.This function counts the number of records in a table if the condition is not specified.If the condition...

4 minutes read.

Types of SQL JOIN

The SQL JOIN combines one or more than one tables based on their relationship. The SQL JOIN involves a parent table and a child table relationship. There are different types of...

10 minutes read.

Check Constraint in SQL

The Check Constraint in SQL is the rule or set of rules used to limit the data range that can be entered in a table column. Check constraint is used...

8 minutes read.

SQL Data Types

In SQL DATA TYPES, each column holds value. Data types can be an integer type, character type, etc., and such data is stored in the database table. The data type is...

4 minutes read.

How to remove duplicates in SQL

Introduction There are some specific rules that needs to be followed while creating the database objects. To improve the performance of a database, a primary key, clustered and non-clustered indexes, and...

9 minutes read.

SQL Cast Operator

While writing SQL queries, we can see many values which are to be converted into another datatype for accessing them. In SQL this conversion is generally done by CAST function. CAST...

4 minutes read.

SQL Tutorial for Beginners

SQL tutorial provides basic and advanced concepts of Structured Query Language and how you deploy SQL to work with a relational database system. Our SQL tutorial is designed for beginners...

3 minutes read.

SQL KEYS

SQL KEYS are single or multiple attributes used to get data from the table according to the requirement or condition. They can also be used to set up relationships amongst...

3 minutes read.

SQL FULL JOIN

In this section, we will help you understand the concept of the SQL FULL Join clause with a few examples. The SQL FULL Join query is executed to display the integrated...

3 minutes read.

Trigger in SQL

In this article, we will learn about the concept of trigger in SQL and its implementation with the help of an example. A Trigger in Structured Query Language is a set...

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.

How to use INNER JOIN in SQL

In this article, we will learn about the INNER JOIN concept and how to use it in SQL with the WHERE clause. What is INNER JOIN in SQL? Inner Join is a...

6 minutes read.

SQL CONSTRAINTS

SQL Constraints specifies the rules/limitations/restrictions for data present in table. SQL Constraints are specified at the time of table creation or after table creation using ALTER command. There are two...

5 minutes read.

SQL Scalar Functions

TEACHER Table LCASE() Function The LCASE() function is used to return lower case values of specified column. Syntax: Select Lcase(column_name) from table_name; Example: Select Lcase(teacher_name) from teacher; Syntax: Select Lcase(column_name) from table_name [where condition]; Example: Select Lcase(teacher_name) from teacher where teacher_id=1; UCASE() Function The UCASE()...

1 minute read.

SQL COPY Table

In this tutorial, we will learn how to copy tables in SQL with the help of examples. Using the SELECT INTO statement in the SQL we can copy one table into...

4 minutes read.

Where vs Having

Difference Between Where and Having The WHERE and HAVING clauses in SQL are used to filter records stored in tables in the databases. The dissimilarities between the WHERE and HAVING clause...

3 minutes read.

How to use GROUP BY clause in SQL

In this SQL article, we will learn about the GROUP BY clause and how to use it in SQL. We will also discuss using the GROUP BY clause with the...

6 minutes read.

Introduction to SQL

SQL Introduction SQL is Standard Query Language. This language is used to communicate or interact with database. In other words, SQL is used to access and manage data or information...

2 minutes read.