SQL Between Operator - GeeksforGeeks

SQL Between Operator

Last Updated : 30 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

The SQL BETWEEN operator defines the ranges. It is a logical operator used in SQL to retrieve data within a specified range

For example, if we want to display the names of employees whose salaries are in a given range then we can use the SQL BETWEEN operator. The ranges can be defined as numeric, date, or text.

BETWEEN Operator in SQL

The BETWEEN operator in SQL is used to easily test if an expression is within a range of values (inclusive). The values can be text, date, or numbers. The BETWEEN operator includes begin and end values.

The BETWEEN operator can be used with SELECT, INSERT, UPDATE, and DELETE commands. This operator is particularly useful for filtering records based on a range of values

Syntax

The syntax of the SQL BETWEEN operator is:

WHERE column_name BETWEEN value1 AND value2;

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.

SQL Between Operator Example

Let’s look at some examples of SQL BETWEEN Operator, to understand how to use it in SQL:

First we need to create a new database first and inside the database, we will create a table and then inside the table, we will apply the BETWEEN operator. Create new table and database using the following queries.

Demo Database

SQL
CREATE DATABASE GEEKSFORGEEKS;
USE GEEKSFORGEEKS;

CREATE TABLE STUDENTS(
     STUDENT_ID INT PRIMARY KEY,
     NAME VARCHAR(20),
     ADDRESS VARCHAR(20),
     AGE INT,
     DOB DATE);

INSERT INTO STUDENTS VALUES
    (1, 'DEV', '120', 2001, '2001-03-09'),
    (2, 'AMAN', '92', 2003, '2003-11-28'),
    (3, 'VINOD', '184', 2002, '2002-09-16'),
    (4, 'YASH', '87', 2001, '2001-06-18'),
    (5, 'NITYA', '195', 2000, '2000-01-12');

SELECT * FROM STUDENTS;

Output:

demo table created

Table’s data

Using BETWEEN Operator Example

In this example we only want to display the details of students having TOTAL_MARKS between 100-200 and DOB between 2001-01-01 and 2003-01-01.

Query:

 SELECT *
FROM STUDENTS
WHERE TOTAL_MARKS BETWEEN 100 AND 200 AND DOB BETWEEN "2001-01-01" AND "2003-01-01";
using between operator example output

Final result

Important Points About SQL BETWEEN Operator

  1. The BETWEEN operator is a logical operator used in SQL to retrieve data within a specified range. The range is inclusive, meaning it includes the start and end values.
  2. The BETWEEN operator can be used with SELECT, INSERT, UPDATE, and DELETE commands to filter records based on a range of values.
  3. The values in the range can be of textual, numeric, or date data types.
  4. The NOT BETWEEN operator can be used to retrieve data that falls outside the specified range.
  5. The BETWEEN operator can be combined with the IN operator to further refine the query and select values within a range that also match a list of values.
  6. The BETWEEN operator simplifies queries that would otherwise require multiple greater than and less than conditions.

SQL BETWEEN Operator- FAQs

Why do we use SQL BETWEEN operator?

We use the BETWEEN operator in SQL when we want to select values within a given range.

In how many ways can we define the range?

We can define ranges in as either numeric, date, text.

Can we find ranges withing a date and time using BETWEEN the ?

Yes, we can find ranges withing mentioned date & time using BETWEEN statement.

How to get data between two timestamps in SQL?

The difference between the start and end timestamps is calculated by using the following query: SELECT TIMESTAMPDIFF(SECOND, start_timestamp, end_timestamp) FROM events WHERE event_name = ‘Meeting’;


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
Difference between = and IN operator in SQL
Prerequisite - SQL Commands In this article we are going to see the difference between = and IN operator in SQL. 1. = Operator : The = operator is used with Where Clause in SQL. For Example consider the student table given below, ROLL_NO NAME ADDRESS PHONE Age 1 Ram Delhi xxxxxxxxxx 18 2 RAMESH GURGAON xxxxxxxxxx 18 3 SUJIT ROHTAK xxxxxxxxxx 20 4 S
2 min read
SQL | BETWEEN & IN Operator
Pre-requisites: SQL Operators Operators are the foundation of any programming language. We can define operators as symbols that help us to perform specific mathematical and logical computations on operands. In other words, we can say that an operator operates the operands.  In this article, we will see  BETWEEN & IN Operator of SQL.  Between Op
3 min read
Difference between T-SQL and PL-SQL
1. Transact SQL (T-SQL) : T-SQL is an abbreviation for Transact Structure Query Language. It is a product by Microsoft and is an extension of SQL Language which is used to interact with relational databases. It is considered to perform best with Microsoft SQL servers. T-SQL statements are used to perform the transactions to the databases. T-SQL has
3 min read
Difference between SQL and T-SQL
1. Structured Query language (SQL): A structured Query language is a computer language for a relational database system. Relational database system like MySQL oracle Sybase Informix uses SQL as standard database language for storing retrieving manipulating data and store in a relational database. Here are some SQL commands that are used for communi
3 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
Article Tags :