Code has been added to clipboard!

Using the SQL BETWEEN Operator

Reading time 2 min
Published Oct 11, 2019
Updated Oct 11, 2019

SQL BETWEEN Operator: Main Tips

  • The BETWEEN operator selects certain range values. The values could be text, dates or numbers.
  • The BETWEEN operator can show a different result in different databases.

Syntax of SQL BETWEEN Operator

Selects values within a range.

Example
SELECT column_name(s)
FROM table_name
WHERE column_name BETWEEN value1 AND value2;

Demo Database

This is demo example from the "Orders" table in the database:

ID Name Count Price Date
1 Apple 120 11.9 2017-08-18
2 Milk 50 9.1 2016-12-19
3 Bread 60 15.4 2017-06-21
3 Water 150 12.3 2017-07-10
3 Chocolate 85 21.9 2016-12-25

SQL BETWEEN Operator: Examples

In this example, we select all orders with a price being BETWEEN 10 and 15:

Example
SELECT * FROM Orders
WHERE Price BETWEEN 10 AND 15;

In this example, we select all orders with a price being NOT BETWEEN 10 and 15:

Example
SELECT * FROM Orders
WHERE Price NOT BETWEEN 10 AND 15;

In this example, we select all orders with a price being BETWEEN 10 and 15, but products with an OrderID of 1 or 2 are not be displayed:

Example
SELECT * FROM Orders
WHERE (Price BETWEEN 10 AND 15)
AND NOT ID IN (1,2);

In this example, we select all orders with an OrderName beginning with any of the letter BETWEEN 'A' and 'C':

Example
SELECT * FROM Orders
WHERE Name BETWEEN 'A' AND 'C';

 

In this example, we select all products with an OrderName beginning with any of the letter NOT BETWEEN 'A' and 'C':

Example
SELECT * FROM Orders
WHERE Name NOT BETWEEN 'A' AND 'C';

In this example, we select all orders with an OrderName BETWEEN 'Apple' and 'Bread':

Example
SELECT * FROM Orders
WHERE Name BETWEEN 'Apple' AND 'Bread'
ORDER BY Name;

In this example, we select all orders with an OrderName NOT BETWEEN 'Apple' and 'Bread':

Example
SELECT * FROM Orders
WHERE Name NOT BETWEEN 'Apple' AND 'Bread'
ORDER BY Name;

In this example, we select orders when an OrderDate is BETWEEN '2016-07-05' and '2017-07-10':

Example
SELECT * FROM Orders
WHERE Date BETWEEN '2016-07-05' AND '2017-07-10';
Learn SQL
Introduction
Syntax
Data Types
Server Data Types
Commands
Commands List
Wildcards
Constraints
Aggregate Functions
Date Functions
Date Format
Injection
SQL Server Hosting
Views
Auto-incrementation
SQL Operators
AS
AND & OR
IN
BETWEEN
WHERE
GROUP BY
HAVING
ORDER BY
LIKE
NOT
NOT EQUAL
UNION
NULL
NOT NULL
DEFAULT
UNIQUE
FOREIGN KEY
PRIMARY KEY
CHECK
Indexes
ALTER TABLE
CREATE DATABASE
CREATE TABLE
DELETE
DROP
INSERT INTO SELECT
INSERT INTO
SELECT
SELECT DISTINCT
SELECT INTO
SELECT TOP
UPDATE
FULL OUTER JOIN
INNER JOIN
JOIN
LEFT JOIN
RIGHT JOIN
AVG()
COUNT
FIRST
LAST
MAX
MIN()
SUM()
LEN
UCASE
MID
NOW
ROUND
FORMAT
LOWER
CONVERT
ISNULL