SQL BETWEEN

The SQL BETWEEN operator is used to select data within a range between 2 values.

SQL BETWEEN Operator Syntax

SELECT ColumnName1, ColumnName2, ColumnName3, ...
FROM TableName
WHERE ColumnName BETWEEN Value1 AND Value2

The BETWEEN operator can be used in SQL SELECT, UPDATE, INSERT or DELETE statements; the values can be numbers, text or dates.

SQL BETWEEN Operator Example

Table: Books

BookId BookName Price
203 Mazojys 26.00
204 Jozzh 14.00
205 Syllauu 37.00
206 Gecrrcc 8.20
302 Jssme 52.00
303 Dnnaao 28.00
304 Tyoysww 17.00

Example 1
We want to select books which price between $17.00 and $28.00:

SELECT *
FROM Books
WHERE Price BETWEEN 17 AND 28

For SQL Server, Oracle, mySQL, the result will include the books which price is exactly $17.00 or $28.00, so it will look like:

BookId BookName Price
203 Mazojys 26.00
303 Dnnaao 28.00
304 Tyoysww 17.00

Example 2
We want to select books which price is out of range of $17.00 and $28.00:

SELECT *
FROM Books
WHERE Price NOT BETWEEN 17 AND 28

The result will look like:

BookId BookName Price
204 Jozzh 14.00
205 Syllauu 37.00
206 Gecrrcc 8.20
302 Jssme 52.00

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>