Monday, October 19, 2015

BETWEEN Operator

NOT BETWEEN Operator Example

To display the products outside the range of the previous example, use NOT BETWEEN:

Example

SELECT * FROM Products
WHERE Price NOT BETWEEN 10 AND 20; 
ProductIDProductNameSupplierIDCategoryIDUnitPrice
Chef Anton's Cajun Seasoning  48 - 6 oz jars  22 
Chef Anton's Gumbo Mix  36 boxes  21.35 
Grandma's Boysenberry Spread  12 - 8 oz jars  25 
Uncle Bob's Organic Dried Pears  12 - 1 lb pkgs.  30 
Northwoods Cranberry Sauce  12 - 12 oz jars  40 

BETWEEN Operator with IN Example

The following SQL statement selects all products with a price BETWEEN 10 and 20, but products with a CategoryID of 1,2, or 3 should not be displayed:

Example

SELECT * FROM Products
WHERE (Price BETWEEN 10 AND 20)
AND NOT CategoryID IN (1,2,3); 

ProductIDProductNameSupplierIDCategoryIDUnitPrice
31  Gorgonzola Telino  14  12 - 100 g pkgs  12.5 
36  Inlagd Sill  17  24 - 250 g jars  19 
40  Boston Crab Meat  19  24 - 4 oz tins  18.4 
42  Singaporean Hokkien Fried Mee  20  32 - 1 kg pkgs.  14 

BETWEEN Operator with Text Value Example

The following SQL statement selects all products with a ProductName beginning with any of the letter BETWEEN 'C' and 'M':

Example

SELECT * FROM Products
WHERE ProductName BETWEEN 'C' AND 'M'; 

ProductIDProductNameSupplierIDCategoryIDUnitPrice
Chais  10 boxes x 20 bags  18 
Chang  24 - 12 oz bottles  19 
Chef Anton's Cajun Seasoning  48 - 6 oz jars  22 
Chef Anton's Gumbo Mix  36 boxes  21.35 

NOT BETWEEN Operator with Text Value Example

The following SQL statement selects all products with a ProductName beginning with any of the letter NOT BETWEEN 'C' and 'M':

Example

SELECT * FROM Products
WHERE ProductName NOT BETWEEN 'C' AND 'M'; 
ProductIDProductNameSupplierIDCategoryIDUnitPrice
Aniseed Syrup  12 - 550 ml bottles  10 
Uncle Bob's Organic Dried Pears  12 - 1 lb pkgs.  30 
Northwoods Cranberry Sauce  12 - 12 oz jars  40 
Mishi Kobe Niku  18 - 500 g pkgs.  97 

Sample Table

Below is a selection from the "Orders" table:
OrderID CustomerID EmployeeID OrderDate ShipperID
10248 90 5 7/4/1996 3
10249 81 6 7/5/1996 1
10250 34 4 7/8/1996 2
10251 84 3 7/9/1996 1
10252 76 4 7/10/1996 2

BETWEEN Operator with Date Value Example

The following SQL statement selects all orders with an OrderDate BETWEEN '04-July-1996' and '09-July-1996':

Example

SELECT * FROM Orders
WHERE OrderDate BETWEEN #07/04/1996# AND #07/09/1996#; 
OrderIDCustomerIDEmployeeIDOrderDateShipperID
10248  90  7/4/1996 
10249  81  7/5/1996 
10250  34  7/8/1996 
10251  84  7/8/1996 
10252  76  7/9/1996