🚨 Get Your Free NFT Certificate Mint by Completing the Web3 Exam! START NOW

Code has been added to clipboard!

Using the SQL BETWEEN Operator

Reading time 2 min
Published Oct 11, 2019
Updated Oct 11, 2019

SQL BETWEEN Operator: Main Tips

  • The BETWEEN operator selects certain range values. The values could be text, dates or numbers.
  • The BETWEEN operator can show a different result in different databases.

Syntax of SQL BETWEEN Operator

Selects values within a range.

Example
SELECT column_name(s)
FROM table_name
WHERE column_name BETWEEN value1 AND value2;
DataCamp
Pros
  • Easy to use with a learn-by-doing approach
  • Offers quality content
  • Gamified in-browser coding experience
  • The price matches the quality
  • Suitable for learners ranging from beginner to advanced
Main Features
  • Free certificates of completion
  • Focused on data science skills
  • Flexible learning timetable
Udacity
Pros
  • Simplistic design (no unnecessary information)
  • High-quality courses (even the free ones)
  • Variety of features
Main Features
  • Nanodegree programs
  • Suitable for enterprises
  • Paid Certificates of completion
Udemy
Pros
  • Easy to navigate
  • No technical issues
  • Seems to care about its users
Main Features
  • Huge variety of courses
  • 30-day refund policy
  • Free certificates of completion

Demo Database

This is demo example from the "Orders" table in the database:

ID Name Count Price Date
1 Apple 120 11.9 2017-08-18
2 Milk 50 9.1 2016-12-19
3 Bread 60 15.4 2017-06-21
3 Water 150 12.3 2017-07-10
3 Chocolate 85 21.9 2016-12-25

SQL BETWEEN Operator: Examples

In this example, we select all orders with a price being BETWEEN 10 and 15:

Example
SELECT * FROM Orders
WHERE Price BETWEEN 10 AND 15;

In this example, we select all orders with a price being NOT BETWEEN 10 and 15:

Example
SELECT * FROM Orders
WHERE Price NOT BETWEEN 10 AND 15;

In this example, we select all orders with a price being BETWEEN 10 and 15, but products with an OrderID of 1 or 2 are not be displayed:

Example
SELECT * FROM Orders
WHERE (Price BETWEEN 10 AND 15)
AND NOT ID IN (1,2);

In this example, we select all orders with an OrderName beginning with any of the letter BETWEEN 'A' and 'C':

Example
SELECT * FROM Orders
WHERE Name BETWEEN 'A' AND 'C';

 

In this example, we select all products with an OrderName beginning with any of the letter NOT BETWEEN 'A' and 'C':

Example
SELECT * FROM Orders
WHERE Name NOT BETWEEN 'A' AND 'C';

In this example, we select all orders with an OrderName BETWEEN 'Apple' and 'Bread':

Example
SELECT * FROM Orders
WHERE Name BETWEEN 'Apple' AND 'Bread'
ORDER BY Name;

In this example, we select all orders with an OrderName NOT BETWEEN 'Apple' and 'Bread':

Example
SELECT * FROM Orders
WHERE Name NOT BETWEEN 'Apple' AND 'Bread'
ORDER BY Name;

In this example, we select orders when an OrderDate is BETWEEN '2016-07-05' and '2017-07-10':

Example
SELECT * FROM Orders
WHERE Date BETWEEN '2016-07-05' AND '2017-07-10';