Tutorialsteacher

Follow Us

Articles
  • C#
  • C# OOP
  • ASP.NET Core
  • ASP.NET MVC
  • LINQ
  • Inversion of Control (IoC)
  • Web API
  • JavaScript
  • TypeScript
  • jQuery
  • Angular 11
  • Node.js
  • D3.js
  • Sass
  • Python
  • Go lang
  • HTTPS (SSL)
  • Regex
  • SQL
  • SQL Server
  • PostgreSQL
  • MongoDB
  • SQL - Getting Started
  • What is SQL
  • Create Table
  • ALTER TABLE Statements
  • Rename Columns
  • Modify Column Type
  • Drop Columns
  • Rename Tables
  • Drop Tables
  • Insert Statement
  • Update Statement
  • Delete Statement
  • Truncate Statement
  • Merge Statement
  • Null Value
  • Select Query
  • WHERE Clause
  • GROUP BY Clause
  • HAVING Clause
  • ORDER BY Clause
  • SQL - Inner Join
  • SQL - Left Join
  • SQL - Right Join
  • SQL - Full Join
  • SQL - BETWEEN
  • SQL - IN
  • SQL - LIKE
  • SQL - INTERSECT
  • SQL - MINUS
  • SQL - UNION
  • SQL - UNION ALL
  • SQL - DISTINCT
  • SQL - ANY, SOME
  • SQL - ALL
  • SQL - AVG()
  • SQL - COUNT()
  • SQL - MAX()
  • SQL - MIN()
  • SQL - SUM()
Entity Framework Extensions - Boost EF Core 9
  Bulk Insert
  Bulk Delete
  Bulk Update
  Bulk Merge

SQL - BETWEEN Operator

The BETWEEN operator is used in the WHERE conditions to filter records within the specified range. The range of values can be strings, numbers, or dates. The range of values must be specified with the AND operator, as shown below.

Syntax:

SELECT column1, column2,.. FROM table WHERE column BETWEEN begin_value AND end_value

For the demo purpose, we will use the following Employee tables in all examples.

EmpIdFirstNameLastNameEmailSalaryHireDate
1'John''King''[email protected]'330002018-07-25
2'James''Bond'2018-07-29
3'Neena''Kochhar''[email protected]'170002018-08-22
4'Lex''De Haan''[email protected]'150002018-09-8
5'Amit''Patel'180002019-01-25
6'Abdul''Kalam''[email protected]'250002020-07-14

Consider the following query.

SQL Script: BETWEEN Operator
SELECT EmpId, FirstName, LastName, Salary FROM Employee WHERE Salary BETWEEN 10000 AND 20000;

Above, the Salary column is used with the BETWEEN operator to filter records. The Salary BETWEEN 10000 AND 20000; specifies that the values in the Salary column should be between 10000 and 20000 (inclusive of both values). The above query will display the following result.

EmpIdFirstNameLastNameSalary
3'Neena''Kochhar'17000
4'Lex''De Haan'15000
5'Amit''Patel'18000

Note that you must use the AND operator with the BETWEEN operator; otherwise, it will raise an error.

BETWEEN Date Range

The following query uses the BETWEEN operator to specify the date range.

SQL Script: Date Range with BETWEEN
SELECT EmpId, FirstName, LastName, HireDate FROM Employee WHERE HireDate BETWEEN '2018-07-01' and '2018-8-31';
EmpIdFirstNameLastNameHireDate
1'John''King'2018-07-25
2'James''Bond'2018-07-29
3'Neena''Kochhar'2018-08-22

BETWEEN String Range

The following query uses the string range with the BETWEEN operator.

SQL Script: BETWEEN Operator
SELECT * FROM Employee WHERE FirstName BETWEEN 'a%' AND 'j%';

Above, string range FirstName BETWEEN 'a%' AND 'j%' fetches records where the FirstName value should start from either 'a%' or any character before 'j%' (but not 'j'). It will display the following result.

EmpIdFirstNameLastNameEmailSalaryHireDate
5'Amit''Patel'180002019-01-25
6'Abdul''Kalam''[email protected]'250002020-07-14

NOT BETWEEN

Use the NOT operator with the BETWEEN operator to filter records that do not fall in the specified range.

SQL Script: BETWEEN Operator
SELECT EmpId, FirstName, LastName, Salary FROM Employee WHERE Salary NOT BETWEEN 10000 AND 20000;

Above, the Salary column is used with the BETWEEN operator to filter records. The Salary NOT BETWEEN 10000 AND 20000; specifies that the value in the Salary column should not be between 10000 and 20000. The above query will display the following result.

EmpIdFirstNameLastNameSalary
1'John''King'33000
6'Abdul''Kalam'25000
TUTORIALSTEACHER.COM

TutorialsTeacher.com is your authoritative source for comprehensive technologies tutorials, tailored to guide you through mastering various web and other technologies through a step-by-step approach.

Our content helps you to learn technologies easily and quickly for learners of all levels. By accessing this platform, you acknowledge that you have reviewed and consented to abide by our Terms of Use and Privacy Policy, designed to safeguard your experience and privacy rights.

[email protected]

ABOUT USTERMS OF USEPRIVACY POLICY
copywrite-symbol

2024 TutorialsTeacher.com. (v 1.2) All Rights Reserved.