The BETWEEN operator is used to select values within a range.
The BETWEEN operator selects values within a range. The values can be numbers, text, or dates.
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 |
The following SQL statement selects the customers with "CustomerName" alphabetically between "Du monde entier" and "Old World Delicatessen" from the "Customers" table:
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!
To display the companies outside the range in the previous example, use NOT BETWEEN:
The perfect solution for professionals who need to balance work, family, and career building.
More than 10 000 certificates already issued!
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 message has been sent to W3Schools.