SQL BETWEEN keyword allows to select a range of data from the table. The syntax for the BETWEEN statement is given below :
SELECT "column_name" FROM "table_name" WHERE "column_name" BETWEEN 'value1' AND 'value2'
let us assume the below table as a price information for a book shop. Table name is price_information.
| serial_no | book_name | sales_price | Date |
| 1 | J2EE Volume II | $15 | Aug-10-2009 |
| 2 | Learn CSS 30 Days | $12 | Aug-12-2009 |
| 3 | Core Java Volume II | $10 | Aug-15-2009 |
| 4 | J2EE Volume III | $17 | Aug-16-2009 |
| 5 | Learn Photoshop in 24 hours | $20 | Aug-18-2009 |
Executed Query:
SELECT * FROM price_information WHERE Date BETWEEN 'AUG-10-2009' AND 'Aug-16-2009'
Result:
| serial_no | book_name | sales_price | Date |
| 1 | J2EE Volume II | $15 | Aug-10-2009 |
| 2 | Learn CSS 30 Days | $12 | Aug-12-2009 |
| 3 | Core Java Volume II | $10 | Aug-15-2009 |
| 4 | J2EE Volume III | $17 | Aug-16-2009 |
Description:
The query will returns the result which is comes under the given date range.
Executed Query:
SELECT * FROM price_information WHERE sales_price BETWEEN '$10' AND '$15'
Result:
| serial_no | book_name | sales_price | Date |
| 1 | J2EE Volume II | $15 | Aug-10-2009 |
| 2 | Learn CSS 30 Days | $12 | Aug-12-2009 |
| 3 | Core Java Volume II | $10 | Aug-15-2009 |
Description:
The query will returns the result which is comes under the given price range $10 to $.15.
Executed Query:
SELECT * FROM price_information WHERE serial_no BETWEEN 2 AND 5
Result:
| serial_no | book_name | sales_price | Date |
| 2 | Learn CSS 30 Days | $12 | Aug-12-2009 |
| 3 | Core Java Volume II | $10 | Aug-15-2009 |
| 4 | J2EE Volume III | $17 | Aug-16-2009 |
| 5 | Learn Photoshop in 24 hours | $20 | Aug-18-2009 |
Description:
The query will returns the result which is serial_no value comes under 2 to 5.