Search w3schools.com:

SHARE THIS PAGE

SQL BETWEEN Operator


The BETWEEN operator is used to select values within a range.


The SQL BETWEEN Operator

The BETWEEN operator selects values within a range. The values can be numbers, text, or dates.

SQL BETWEEN Syntax

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


Demo Database

In this tutorial we will use the well-known Northwind sample database.

Below is a selection from the "Customers" table:

CustomerID CustomerName ContactName Address City PostalCode Country
1

Alfreds Futterkiste Maria Anders Obere Str. 57 Berlin 12209 Germany
2 Ana Trujillo Emparedados y helados Ana Trujillo Avda. de la Constitución 2222 México D.F. 05021 Mexico
3 Antonio Moreno Taquería Antonio Moreno Mataderos 2312 México D.F. 05023 Mexico
4

Around the Horn Thomas Hardy 120 Hanover Sq. London WA1 1DP UK
5 Berglunds snabbköp Christina Berglund Berguvsvägen 8 Luleå S-958 22 Sweden


BETWEEN Operator Example

The following SQL statement selects the customers with "CustomerName" alphabetically between "Du monde entier" and "Old World Delicatessen" from the "Customers" table:

Example

SELECT * FROM Customers
WHERE CustomerName
BETWEEN 'Du monde entier' AND 'Old World Delicatessen';

Try it yourself »

Note: The BETWEEN operator is treated differently in different databases!

In some databases, customers with the CustomerName "Du monde entier" or "Old World Delicatessen" will not be listed, because the BETWEEN operator only selects fields that are between and excluding the test values.

In other databases, customers with the CustomerName "Du monde entier" or "Old World Delicatessen" will be listed, because the BETWEEN operator selects fields that are between and including the test values.

And in other databases, customers with the CustomerName "Du monde entier" will be listed, but "Old World Delicatessen" will not be listed, because the BETWEEN operator selects fields between the test values, including the first test value and excluding the last test value.

Therefore: Check how your database treats the BETWEEN operator!


Example 2

To display the companies outside the range in the previous example, use NOT BETWEEN:

Example

SELECT * FROM Customers
WHERE CustomerName
NOT BETWEEN 'Du monde entier' AND 'Old World Delicatessen';

Try it yourself »




W3Schools Certification

W3Schools' Online Certification

The perfect solution for professionals who need to balance work, family, and career building.

More than 10 000 certificates already issued!

Get Your Certificate »

The HTML Certificate documents your knowledge of HTML.

The HTML5 Certificate documents your knowledge of advanced HTML5.

The CSS Certificate documents your knowledge of advanced CSS.

The JavaScript Certificate documents your knowledge of JavaScript and HTML DOM.

The jQuery Certificate documents your knowledge of jQuery.

The XML Certificate documents your knowledge of XML, XML DOM and XSLT.

The ASP Certificate documents your knowledge of ASP, SQL, and ADO.

The PHP Certificate documents your knowledge of PHP and SQL (MySQL).

Your suggestion:

Close [X]

Thank You For Helping Us!

Your message has been sent to W3Schools.

Close [X]