Search This Blog

SQL: BETWEEN Condition

 

SQL: BETWEEN Condition

This SQL tutorial explains how to use the SQL BETWEEN condition with syntax and examples.

Description

The SQL BETWEEN condition allows you to easily test if an expression is within a range of values (inclusive). It can be used in a SELECT, INSERT, UPDATE, or DELETE statement.


Syntax

The syntax for the BETWEEN condition in SQL is:

expression BETWEEN value1 AND value2;

Parameters or Arguments

expression
A column or calculation.
value1 and value2
These values create an inclusive range that expression is compared to.

Note

  • The SQL BETWEEN Condition will return the records where expression is within the range of value1 and value2 (inclusive).

DDL/DML for Examples

If you want to follow along with this tutorial, get the DDL to create the tables and the DML to populate the data. Then try the examples in your own database!

Get DDL/DML

Example - Using BETWEEN Condition with Numeric Values

Let's look at an example of how to use the BETWEEN condition to retrieve values within a numeric range.

In this example, we have a table called suppliers with the following data:

supplier_idsupplier_namecitystate
100MicrosoftRedmondWashington
200GoogleMountain ViewCalifornia
300OracleRedwood CityCalifornia
400Kimberly-ClarkIrvingTexas
500Tyson FoodsSpringdaleArkansas
600SC JohnsonRacineWisconsin
700Dole Food CompanyWestlake VillageCalifornia
800Flowers FoodsThomasvilleGeorgia
900Electronic ArtsRedwood CityCalifornia

Enter the following SELECT statement:


SELECT *
FROM suppliers
WHERE supplier_id BETWEEN 300 AND 600;

There will be 4 records selected. These are the results that you should see:

supplier_idsupplier_namecitystate
300OracleRedwood CityCalifornia
400Kimberly-ClarkIrvingTexas
500Tyson FoodsSpringdaleArkansas
600SC JohnsonRacineWisconsin

This example would return all rows from the suppliers table where the supplier_id is between 300 and 600 (inclusive). It is equivalent to the following SELECT statement:


SELECT *
FROM suppliers
WHERE supplier_id >= 300
AND supplier_id <= 600;

Example - Using BETWEEN Condition with Date Values

Dates can be somewhat tricky in SQL and how you use the BETWEEN condition with dates depends on the database you are running (ie: Oracle, SQL Server, MySQL, etc). We will show you an example for each of the major database technologies. So let's get started.

In this example, we have a table called orders with the following data:

order_idcustomer_idorder_date
170002016/04/18
250002016/04/18
380002016/04/19
440002016/04/20
5NULL2016/05/01

Enter one of the following SQL statements, depending on the database you are running.

For SQL Server, PostgreSQL and SQLite:


SELECT *
FROM orders
WHERE order_date BETWEEN '2016/04/19' AND '2016/05/01';

For Oracle (use the TO_DATE function):

SELECT *
FROM orders
WHERE order_date BETWEEN TO_DATE ('2016/04/19', 'yyyy/mm/dd')
AND TO_DATE ('2016/05/01', 'yyyy/mm/dd');

For MySQL and MariaDB (use the CAST function):

SELECT *
FROM orders
WHERE order_date BETWEEN CAST('2016/04/19' AS DATE) AND CAST('2016/05/01' AS DATE);

There will be 3 records selected. These are the results that you should see:

order_idcustomer_idorder_date
380002016/04/19
440002016/04/20
5NULL2016/05/01

This example would return all records from the orders table where the order_date is between Apr 19, 2016 and May 1, 2016 (inclusive).

]Example - Using NOT Operator with the BETWEEN Condition

The BETWEEN condition can be used with the NOT operator to create a NOT BETWEEN condition. Let's explore an example that shows how to use the NOT BETWEEN condition in a query.

In this example, we have a table called customers with the following data:

customer_idlast_namefirst_namefavorite_website
4000JacksonJoegoogle.com
5000SmithJanedigminecraft.com
6000FergusonSamanthabigactivities.com
7000ReynoldsAllencheckyourmath.com
8000AndersonPaigeNULL
9000JohnsonDerekgoogle.com

Enter the following SQL statement:


SELECT *
FROM customers
WHERE customer_id NOT BETWEEN 5000 AND 8000;

There will be 2 records selected. These are the results that you should see:

customer_idlast_namefirst_namefavorite_website
4000JacksonJoegoogle.com
9000JohnsonDerekgoogle.com

This would return all rows where the customer_id was NOT between 5000 and 8000, inclusive. It would be equivalent to the following SELECT statement:


SELECT *
FROM customers
WHERE customer_id < 5000
OR customer_id > 8000;

No comments:

Post a Comment

Using SQL*Plus to Unlock Accounts and Reset Passwords

  Using SQL*Plus to Unlock Accounts and Reset Passwords Use this SQL*Plus procedure to unlock and reset user account passwords. Log in as th...