Summary: In this tutorial, you will learn how to retrieve data from database tables which its value are in a range by using SQL BETWEEN operator.

SQL BETWEEN Operator Syntax

The SQL BETWEEN operator allows you to retrieve values within a specific range. the SQL between must be used in the WHERE clause of the SQL SELECT statement. The following illustrates the SQL BETWEEN syntax:

1 SELECT column_list
2 FROM table_name
3 WHERE column_1 BETWEEN lower_range AND upper_range

MySQL returns all records in which the column_1 value is in the range of lower_rage and upper_range as well as the values lower_rage and upper_range. The query which is equivalent to SQL BETWEEN to get the same result is

1 SELECT column_list
2 FROM table_name
3 WHERE column_1 >= lower_range AND column_1 <= upper_range

Let’s practice with several examples of using SQL BETWEEN to search values in a range.

Suppose we want to find all products which buy price is in a range of 90$ and 100$, we can perform the following query to do so:

1 SELECT productCode,ProductName,buyPrice
2 FROM products
3 WHERE buyPrice BETWEEN 90 AND 100
4 ORDER BY buyPrice DESC

Here is the output

+-------------+--------------------------------------+----------+
| productCode | ProductName                          | buyPrice |
+-------------+--------------------------------------+----------+
| S10_1949    | 1952 Alpine Renault 1300             |    98.58 |
| S24_3856    | 1956 Porsche 356A Coupe              |     98.3 |
| S12_1108    | 2001 Ferrari Enzo                    |    95.59 |
| S12_1099    | 1968 Ford Mustang                    |    95.34 |
| S18_1984    | 1995 Honda Civic                     |    93.89 |
| S18_4027    | 1970 Triumph Spitfire                |    91.92 |
| S10_4698    | 2003 Harley-Davidson Eagle Drag Bike |    91.02 |
+-------------+--------------------------------------+----------+

The output contains all products in the range of 90$ and 100$, and if there is a product with buy price 90$ or 100$, it will be included in the output too.

In order to find all records which are not in a range we use NOT BETWEEN. To find all products that buy price outside the range of 20 and 100, we can operate following query:

1 SELECT productCode,ProductName,buyPrice
2 FROM products
3 WHERE buyPrice NOT BETWEEN 20 AND 100
+-------------+-------------------------------------+----------+
| productCode | ProductName                         | buyPrice |
+-------------+-------------------------------------+----------+
| S10_4962    | 1962 LanciaA Delta 16V              |   103.42 |
| S18_2238    | 1998 Chrysler Plymouth Prowler      |   101.51 |
| S24_2972    | 1982 Lamborghini Diablo             |    16.24 |
| S24_2840    | 1958 Chevy Corvette Limited Edition |    15.91 |
+-------------+-------------------------------------+----------+

The query above is equivalent to the following query

1 SELECT productCode,ProductName,buyPrice
2 FROM products
3 WHERE buyPrice < 20 OR buyPrice > 100
4 ORDER BY buyPrice DESC

In this tutorial, you’ve learned how to use SQL BETWEEN to select data from database tables in a range.

Related posts:

  1. Selecting Data with SQL IN
  2. Using MySQL SELECT Statement to Query Data
  3. How to Use MySQL Limit to Constrain Number of Returned Records
  4. Managing Databases in MySQL
  5. Working with Database Table – Part I

Leave your comment

You must be logged in to post a comment.


Link Exchange Spritual Place for Bhakti | bhaktiguru.com Business Franchise in India | businessfranchiseindia.com Free Directory Submission & Link Submission | ab-directory.com Big Dwarka Business Listing | bigdwarka.com Kolkata Online Business Listing | onlinekolkata.com Patna Online Business Listing | onlinepatna.com Dwarka Online Business Listing | onlinedwarka.com Bihar Online | First Bihar's Portal | bihar-online.com Big Bihar Business Listing | bigbihar.com Dynamic Website Development | dynamicwebsitedeveloper.com Noida Online Business Listing | onlinenoida.com SEO Service Delhi & India | seoservicedelhi.com SEO Experts Delhi & India | seoexpertsdelhi.com Big Patna Business Listing | bigpatna.com Bihar schools Listing Free Schools Ad | biharschools.com Creative Websoft Solutions | Web Development India | creativewebsoft.us Bulk SMS Providers India | bulksmsproviders.net Online cbse Guides | onlinecbseguides.com Big Kolkata Business Listing | bigkolkata.com Handicap Shaadi.com | handicapshaadi.com Handicap Marriage.com | handicapmarriage.com India Home Tutors.com | indiahometutors.com Indian Property Guides.com | indianpropertyguides.com Online Stocks Community.com | onlinestockscommunity.com Free Article Submission | ab-articles.com Secret Dating Online.com | secretdatingonline.com Private DatingOnline.com | privatedatingonline.com Indian businessguides.com | indianbusinessguides.com Software Engineer Delhi | Meenu Khanna Bollywood & Hot Images | hibdy.com Free Computer & Online PHP Training | php2php.com Free Ignou Project & Assignment | ignouinfo.com PHP Programmer India | Web Developer India | bageshsingh.com Creative Websoft Solutions | Web Development IndiaBlog Link Exchange http://www.php2php.com/tutorial-blog/http://bageshsingh.com/bagesh-blog/http://www.bhaktiguru.com/bhaktigurublog/http://www.hibdy.com/desi-masala-blog/