You are on page 1of 7

To run the query, click the ! tool, which should display the complete customer table.

Since we don’t place any limitations on which records are shown, we retrieve them all.

Now, close the query window and, when prompted, save the query as "SQL Query 1".

Four things you should know when writing queries:


 Whitespace or line placement will not affect the query operation
 In the SELECT line, different fields are separated by commas
 A wildcard character (*) can be used when all fields are to be shown
 If you write queries using the wizard or design tools, Access may include table
names for fields and put square brackets around field names in the SQL view
(Customer.[ID]) and end SQL statements with a semicolon but if you use the SQL
view to write queries, you can just use the field names and also omit the closing
semicolon
Queries with WHERE
Suppose we wanted to get the last and first names of only those customers residing in zip
code 60101. Specifically, the query results will be only the names, not the zip code. Use the
query wizard to create this query, then view it in the SQL view. The query is written as
shown below (but most likely with some extra parentheses in the WHERE line):

SELECT Customer.LastName, Customer.FirstName


FROM Customer
WHERE Customer.ZipCode=60101;

The WHERE line specifies match criteria: in this case, only the records with a zip code of
60101 will be shown. If you like, you can remove the superfluous parentheses in the SQL
view, since here they don’t directly affect how this query works. Just make sure you remove
them all! Then close this query and name it SQL Query 2.

WHERE can be used with relational operators like:


 > for greater than
 >= for greater than or equal to
 < for less than
 <= for less than or equal to
 <> for not equal
 = for equal
But, remember that any matches to literal text (like checking where a last name field
matches the word Jones) should enclose the text in quotes:
SELECT LastName
FROM Customer
WHERE LastName = "Gray"

Queries with DISTINCT


Suppose you want to know which values are present in a field, like which zip codes are
present in your customer table. SQL has a DISTINCT keyword that specifies only unique
values are to be shown. To retrieve the list of zip codes in the customer table, write:

SELECT DISTINCT ZipCode


FROM Customer

With the DISTINCT keyword, only the five different zip code values present in the Customer
table are shown in the query results. The DISTINCT keyword can also be used with multiple
fields and will show only the unique combinations of those fields. Close this query and save
it as "SQL Query 3".
Queries with compound constraints
Suppose you only want to show purchases for a particular date and customer, say,
customer 1 on 11/11/2016. Then, you would separate the criteria following WHERE with
the keyword AND (meaning that all the criteria must be met). Write the query as:

SELECT Customer, Date, Item, Quantity


FROM Purchase
WHERE Customer = 1 AND Date = #11/11/2016#

Note: Just as text matches are enclosed in quotes, a date matches are enclosed in # symbols

After running the query (and verifying you only see the single purchase from 11/11/2016),
save it as "SQL Query 4".
Suppose you want to show purchases where either constraint is met (i.e., you want to show
all purchases made by customer 1 and all purchases made on 11/11/2016). Then, you
would separate the criteria following WHERE with the keyword OR (meaning that a match
happens if any of the criteria are met). In the SQL view, change SQL Query 4 to:

SELECT Customer, Date, Item, Quantity


FROM Purchase
WHERE Customer = 1 OR Date = #11/11/2016#

The OR operator will have the query return a second result when you run it.
Queries with sorted results
If you want a query to return sorted results, you can include the ORDER BY keyword and
ASC or DESC for ascending or descending order. For example, suppose you want your list of
purchases in descending order for item ID and, for any particular item ID, in ascending
order by customer ID. Then, you would write the query as:

SELECT Item, Customer


FROM Purchase
ORDER BY Item DESC, Customer ASC

If you are interested in learning more about Structured Query Language, the W3 schools
tutorials are a good place to start: https://www.w3schools.com/sql/

You might also like