AlphaPress Publishing
P.O. Box 5942 Albany, NY 12205
Tel: 518-250-3890
Email: alphapress@hotmail.com  


  
Click to buy this book from Amazon

Section Menu 

News

Pindaro is now working on a comprehensive book on SQL Server 2014.  For any comments you can email the author at alphapress@hotmail.com.

  

 

 

How to Use the IN Operator with Dates and Subqueries

To contact the author click here

This article is from the book "Access 2007 Pure SQL

To download the sample database click here



The IN operator with subqueries and dates

Find customers who have not placed any orders in the second half of 2009

Discussion

Our task in this scenario is to find customers who have not placed any orders in the second half of the year 2009.  Management needs this information for initiating promotions to inactive customers.  To achieve this task, we will use the NOT IN and BETWEEN operators and a subquery.  Remember that the purpose of the subquery is always the same—to generate a list of values to be used by the IN operator.  Here, the subquery will generate a list of CustomerIDs from the Orders table, which will be used by the main query to filter records from the Customers table.  In total, 70 customers have not ordered anything during this six-month period. 

Code:

SELECT *

FROM Customers

WHERE CustomerID NOT IN

(Select CustomerID FROM Orders WHERE

OrderDate BETWEEN #6/1/2009# AND #12/1/2009#)
 

Result: