AlphaPress Publishing
P.O. Box 5942 Albany, NY 12205
Tel: 518-250-3890


Section Menu 

Are you a University or College bookstore and want to order multiple books?  Email us at or call us at 518-250-3890.  We will be happy to talk to you.  

Are you a professor and you need a sample copy?  We will be happy to send you a pdf or printed copy!  Email us at

Are you outside the United States?  We can ship multiple books worldwide in most countries.




How to use the Count() function with the DISTINCT predicate

To contact the author click here

This article is from the book "Access 2007 Pure SQL

To download the sample database click here


The count() function with DISTINCT

Count the number of unique orders from customers


The previous example is fine, but we need to find a way to count the number of unique orders in this table.  The proper way to accomplish this task in SQL is to use the COUNT and DISTINCT statements together as: 

SELECT Count (Distinct orderid) as NumberofOrders FROM tbls_cusotmersag 

This is excellent, but our problem is that it does not work in Access 2007.  We need to deviate thematically for a second and use a subquery with DISTINCT to achieve the result we want.  (See chapter 30 on subqueries for a detailed explanation of how they work).  From the result set below, please notice that the number of orders produced by the count() function is now 15.  This is actually the number of unique orders in the table, which is exactly what we need. 


SELECT Count(*) AS NumberofOrders


(SELECT DISTINCT orderid FROM tbls_customersag)