Access Handout 5 - Queries
Access Handout 5 - Queries
After Tables, Queries are perhaps the most important component in a database. Queries are used to
retrieve information from a database. Once again, a telephone directory can be used for an example of a
query.
When you look up a phone number in a phone directory, you use query techniques. You might begin by
looking for names that begin with J. Then you might narrow it down to listings with the last name
Johnson. The criteria for your query is Last Name = Johnson. If there are still too many results, you may
add additional criteria to narrow down the results. E.g. Initials, Suburb, street address. Eventually, your
criteria will narrow down the results until you have the result you are after.
When you query a database on a computer, you use the same techniques. A query is a set of rules for
finding information in a database. Queries in a database use the Structured Query Language (SQL). In
Access, however, it is not necessary to learn Access to create queries, as queries can be created using
the much simpler Query by Example (QBE) window. The most common variety of query is the Select
query, where the query is used to select certain information from the database according to criteria you
specify.
1. Choose which table(s) the information will be coming from (information can also come from
existing queries).
2. Choose which fields will be included in the query result(s).
3. Specify the criteria for the type of query.
4. Run the query / view the results
Note In the previous section it was advised to name Tables and Queries differently so it is easy to tell which is
which when they are listed together. We are naming Tables in UPPERCASE letters and Queries in lowercase letters.
Another common method is to use naming prefixes such as tblCustomers and qrySeptemberSales.
Also, it is important to give your queries meaningful names so you know which is which, especially when
there are a lot of queries in a database. E.g. a query that shows overdue payments should be called
something obvious like overdue payments listing.
Query Criteria
The next key step in creating a query is to specify what kind of information you want to be included in
the query results. The rules you set up for determining what will be included in the results are referred to
as Criteria. If you look in the QBE grid, you will notice that there is a criteria row. To use criteria, you can
type an example of your intended result in the criteria row below your intended field (which is why it’s
called Query by Example).
You will notice that Access has placed quotation marks around your criteria. Access uses quotation
marks to specify that it is text criteria. When dates are used as criteria the date will be surrounded by a
# on each side as you will see later. Numbers are left alone. When entering criteria you don’t need to
enter these symbols yourself but be careful when editing. If you delete quotation marks on one side and
not the other, you will get an error.
Note You can view the results of a query by clicking either the Datasheet View icon or the Run icon. However, it is
best to get in to the habit of using the Datasheet View icon. Firstly because the Design View icon is in exactly the
same place so you can easily move between Datasheet and Design view without moving your mouse. Secondly
because the Run icon has a very different purpose in certain types of queries and should be used with caution.
You can also use or Criterion by placing each criterion on a different row. You will notice that under the
Criteria row is an Or row which works an “or” operator. All of the ones below it are also rows used for Or
criteria so you can have as many criteria as you like in a single field.
In the first example, the results will show records for Male students in Yokine or Bedford. In the second
example, the results will show Male students in Yokine and all students from Bedford. This is because in
the second example, the Gender field criteria only applies to the suburb on the same line. If you wanted
both Suburbs to only show Male students you would need to modify the query as shown below.
Ranges in Criteria
Instead of using specific criteria, you can specify that you want the results to find all records within a
certain range.
You can see from these examples that range criterion work equally well with numbers, dates and text.
The first wildcard is the ? symbol. This can be used as a substitute for any letter in a criteria (similar to
the blank piece in a game of Scrabble). The second and more commonly used wildcard is the * symbol.
This can be used in place of any number of letters. The following examples show how it might be used.
1) Try the following criteria in your query. Remember to clear each one before trying the next one (Hint -
you will need to add the Comment field to the QBE Grid first).
Expression Returns
Between #12/1/98# and #2/3/99# Dates from 12/1/98 and 2/3/99, inclusive
In (“Mary”, “Louise”, “Annie”) Records with Mary, Louise, or Annie
“ ” (quotes with a space in between) Records with a blank
IsNull Records with no entry (null field)
Like “Cas?le” Cas then any character followed by le
Like “*s” Ends in s (case insensitive)
Like “v*” Starts with v (case insensitive)
<1000 Less than 1000
1000 Equal to 1000
Like “[A-C]??” Starts with A through C and has three characters
???? Any four characters
Len([Surname])=Val(4) Any surname of four characters
Right([Surname],2) = “is” Any surname ending in the letters is
Left([Surname],4) = “Cass” Surnames starting with Cass
SQL
SQL is very powerful. The simplest of its functions is to create a subset of your data. It can also create a
table, update your data, delete data, create a comparison of data, combine data, and allow you to select
your information based on a prompt.
Because Microsoft Access provides very flexible and advanced means of creating queries, you can create
almost any type of query without knowing anything about the SQL but it is important to understand this
language because you should have an idea of what Microsoft Access does when you ask it to create a
query. This would allow you to troubleshoot a query when necessary.
Queries are built from the SQL. Like every computer language, the SQL comes with its syntax,
vocabulary, and rules. The SQL is equipped with keywords that tell it what to do and how to do it. We
have established that a query resembles a question you ask the database and the database responds.
This works by asking the database to isolate or select some fields and create a new object made of
those fields. In order to ask the database to create a query, that is, to isolate fields, you must write a
statement made of keywords, operators, and database objects.
Over all, the SQL is not case-sensitive. This means that SELECT, Select, and select represent the same
word. To differentiate SQL keywords from "normal" language or from the database object, it is a good
idea to write SQL keywords in uppercase. A SQL statement must end with a semi-colon. In the SQL, the
object is specified after the FROM keyword and by the WhatObject parameter of our syntax. For
example, if you want to create a query based on a table named Persons, you would write the statement
as:
SELECT What FROM Persons;
We also saw that, after specifying the object that holds the fields, you can then select each desired field
and add it to the query.
In the SQL, to select a field, enter its name after the SELECT keyword. For example, to select the
LastName field of the Persons table, you would type
SELECT LastName FROM Persons;
If you want to include more than one field from the same table, separate them with a comma. For
example, to select the first and last names of the Persons table and include them in your query, you can
use the following statement:
SELECT FirstName, LastName FROM Persons;
We also saw that, to select all fields from an object and include them in your query, you could drag the
asterisk field and drop it in the lower section of the query in Design View. In the same way, to include
everything from the originating table or query, use the asterisk * as the field. Here is a statement that
results in including all fields from the Persons table:
SELECT * FROM Persons