Professional Documents
Culture Documents
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".
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.
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:
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:
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:
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/