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.

  

 

 

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: