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


  

Section Menu 

Are you a University or College bookstore and want to order multiple books?  Email us at alphapress@hotmail.com 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 alphapress@hotmail.com.

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

  

 

 

Using the AND operator on multiple columns the right way

To contact the author click here

This article is from the book "Access 2007 Pure SQL

To download the sample database click here


Using AND on multiple columns

Produce a report of customers from a specific state and a specific city 

Discussion:

This time we have a business request to produce a report of customers who reside in the state of NY and in particular in the city of Albany.  In this case, we need to use the AND operator to isolate records which satisfy both criteria at the same time.  Note that if we use the OR operator, we would get all the customers in the state of NY irrespectively of city and all the customers from all the cities named "Albany" around the country.  This is a problem because there are 28 cities around the US named Albany!  In addition, if we do business worldwide there are six cities named Albany.  Consequently, an experienced database user would always use AND to isolate the customers only for Albany, New York.    As you can see from the result set, we have only four customers that satisfy both criteria.

 

Code:

SELECT lastname, firstname, city, state

FROM customers

WHERE (state = "NY")

AND (city = "Albany")

 

Result: