Domesticated Brain

SQL BETWEEN Operator

 

The BETWEEN Operator is used to select data from a database table based on a specified range.

In this lesson about SQL BETWEEN operator, we are working with following tblproducts table which I have created recently for this tutorial including following fields

ProductID, Category, BrandName, ProductName, Price

 

SQL Product Table

 

Syntax for SQL BETWEEN as follows

 

SELECT Name of the columns need to be selected

FROM Name of the database table to be accessed

WHERE Name of the column associated with the selected range BETWEEN Value1 AND Value2;

 

As an example for SQL BETWEEN operator we need to select products which are in the price range of 30000 to 70000.

Following SQL query example including SQL BETWEEN operator will select products with in the price range of 30000 to 70000.

 

SELECT * FROM tblproducts

WHERE Price BETWEEN 30000 AND 70000;

 

SQL BETWEEN Product Table

Now you can see the result of the above SQL query has selected the products with the given price range of 30000 to 70000

 

NOT BETWEEN Operator

NOT BETWEEN Operator is the opposite of the BETWEEN Operator

Syntax for SQL NOT BETWEEN as follows

 

SELECT Name of the columns need to be selected

FROM Name of the database table to be assessed

WHERE Name of the column associated with the selected range NOT BETWEEN Value1 AND Value2;

 

If we run the following SQL Query including the NOT BETWEEN operator it will select all the products not in the price range of 30000 to 70000 from the tblproduct table

 

SELECT * FROM tblproducts

WHERE Price NOT BETWEEN 30000 AND 70000;

 

SQL NOT BETWEEN Product Table

 

 Now you can see the result of the above SQL query has selected the products not in with the given price range of 30000 to 70000