SQL BETWEEN

The BETWEEN is a logical operator in SQL, which fetches the records from the table within the range specified in the query. We can use this operator to select numbers, text, or dates.

The BETWEEN operator in SQL includes the starting and ending values.

BETWEEN operator is used in the WHERE clause with the following four SQL statements:

  1. SELECT Statement
  2. UPDATE Statement
  3. DELETE Statement
  4. INSERT Statement

Syntax of BETWEEN operator in SQL

SELECT column_Name1, column_Name2 ...., column_NameN FROM table_Name WHERE column_name BETWEEN value1 and value2 ;  

The BETWEEN operator returns True if the value of column_name is greater than or equal to the value1 and less than or equal to the value2.

Examples of BETWEEN Operator in SQL

In this article, we have taken the following different SQL examples which help you how to use BETWEEN operator:

Example 1: Let’s take the following STUDENT table, which helps you to understand the BETWEEN operator:

Roll_NoNameMarksAge
1Raman9520
2Kapil9219
3Arun8517
4Ram9218
5Suman5520
6Sanjay8818
7Sheetal6519
8Rakesh6420

Suppose, we want to fetch the record of those students from the below student table whose ‘marks’ are greater than or equal to 80 and less than or equal to 100. For this operation, we have to perform the following query in Structured Query Language:

SELECT * FROM STUDENT WHERE Marks BETWEEN 80 and 100 ;  

This query shows the following table in the SQL output:

Roll_NoNameMarksAge
1Raman9520
2Kapil9219
3Arun8517
4Ram9218
6Sanjay8818

Example 2: Let’s take the following STUDENT table, which helps you to understand the NOT BETWEEN operator:

Roll_NoNameMarksAge
1Raman9520
2Kapil9219
3Arun8517
4Ram9218
5Suman5520
6Sanjay8818
7Sheetal6519
8Rakesh6420

Suppose, we want to fetch the record of those students from the below student table whose marks are neither greater than 80 nor less than 100. For this operation, we have to perform the following query in Structured Query Language:

SELECT * FROM STUDENT WHERE Marks NOT BETWEEN 80 and 100 ;  

This query shows the following table in the SQL output:

Roll_NoNameMarksAge
5Suman5520
7Sheetal6519
8Rakesh6420

Example 3: Let’s take the following EMPLOYEE table, which helps you to understand the BETWEEN operator with dates:

Emp_IDNameEmp_SalaryEmp_Joining
1001Vivek90002021/01/02
1002Saket40002019/06/05
1003Raman30002020/02/05
1004Suraj60002020/04/01
1005Seenu50002019/12/04
1006Shubham100002021/02/05
1007Anaya40002020/12/20
1008Parul80002019/04/15

Suppose, we want to fetch the record of those employees from the above employee table whose joining data is between 2019/06/05 and 2020/12/20. For this operation, we have to type the following query in SQL:

SELECT * FROM EMPLOYEE WHERE Emp_Joining BETWEEN '2019/06/05' and '2020/12/20' ;  

This query shows the following table in the SQL output:

Emp_IDNameEmp_SalaryEmp_Joining
1002Saket40002019/06/05
1005Seenu50002019/12/04
1007Anaya40002020/12/20
1008Parul80002019/04/15

BETWEEN with UPDATE Statement

In SQL, we can also use BETWEEN operator in the WHERE clause with the UPDATE statement. The BETWEEN operator updates those records in the table which satisfy the range specified in the query.

Syntax of BETWEEN with UPDATE Statement

UPDATE table_name SET column_Name1 = value1, column_Name2 = value2, ...., column_NameN = valueN WHERE ColumnName BETWEEN value1 and value2;  

Examples of BETWEEN with UPDATE Statement

Here, we have taken the following two SQL examples which help you how to use BETWEEN operator with UPDATE statement for updating the existing records in the tables:

Example 1: Let’s take the following STUDENT table, which helps you to understand the BETWEEN operator with an UPDATE statement:

Roll_NoNameMarksAttendance
1Raman9520
2Kapil9217
3Arun8517
4Ram9218
5Suman5519
6Sanjay8817
7Sheetal6519
8Rakesh6420

Suppose, we want to update the Marks of those students whose Attendance is greater or equal to 18 and less than or equal to 20. For this operation, we have to perform the following query in Structured Query Language:

UPDATE STUDENT SET Marks = 98 WHERE Attendance BETWEEN 18 and 20;  

If you want to see the changes that occurred in the table, then you have to type the following query:

  1. SELECT * FROM STUDENT WHERE Attendance BETWEEN 18 and 20 ;  
Roll_NoNameMarksAttendance
1Raman9820
4Ram9818
5Suman9819
7Sheetal9819
8Rakesh9820

Example 2: Let’s take the following STUDENT table, which helps you to understand the NOT BETWEEN operator:

Roll_NoNameMarksAttendance
1Raman9520
2Kapil9217
3Arun8517
4Ram9218
5Suman5519
6Sanjay8817
7Sheetal6519
8Rakesh6420

Suppose, we want to update the Marks of those students whose Attendance is neither greater and equal to 18 nor less than and equal to 20. For this operation, we have to type the following query in SQL:

UPDATE STUDENT SET Marks = 50 WHERE Attendance NOT BETWEEN 18 and 20;  

If you want to see the changes that occurred in the table, you have to type the following query:

SELECT * FROM STUDENT WHERE Attendance NOT BETWEEN 18 and 20 ;  
Roll_NoNameMarksAttendance
2Kapil5017
3Arun5017
6Sanjay5017

Example 3: Let’s take the following EMPLOYEE table, which helps you to understand the BETWEEN operator with dates:

Emp_IDNameEmp_SalaryEmp_Joining
1001Vivek90002021/01/02
1002Saket40002019/06/05
1003Raman30002020/02/05
1004Suraj60002020/04/01
1005Seenu50002019/12/04
1006Shubham100002021/02/05
1007Anaya40002020/12/20
1008Parul80002019/04/15

Suppose, we want to update the Salary of those employees who join the office between 2019/06/05 and 2020/12/20. For this operation, we have to type the following query in SQL:

UPDATE EMPLOYEE SET Emp_Salary = 25000 WHERE Emp_Joining BETWEEN '2019/06/05' and '2020/12/20' ;  

If you want to see the changes that occurred in the table, then you have to type the following query:

SELECT * FROM EMPLOYEE WHERE Emp_Joining BETWEEN '2019/06/05' and '2020/12/20' ;  
Emp_IDNameEmp_SalaryEmp_Joining
1002Saket250002019/06/05
1005Seenu250002019/12/04
1007Anaya250002020/12/20
1008Parul250002019/04/15

Comments

Leave a Reply

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