Escolar Documentos
Profissional Documentos
Cultura Documentos
Thus far we have discussed the structure of relational database and how to plan and create a
database. Once the database is created and data placed in the appropriate table, we will need to
be able to extract data. The data in a database can be retrieved by examining its tables; however,
all the information contained in a table is frequently more information than is needed by a
database user. In addition, database users often want to view information that spans related
tables, or view data in a different order. Queries provide a way for users to obtain more
meaningful information from a database that simply viewing all data contained in a table.
Queries can be used to:
This chapter will cover the first four bullets; the last item, joining tables, will be discussed in
subsequent chapters.
WHAT IS A QUERY
Remember that a table is a database object that stores records of related data according to a
prescribed field order. A query is also an Access database object; a query defines how data will
be extracted from existing data tables or the results of previous queries and how the extracted
data will be organized. In English, a query is a question. In a database, a query is a set of
instructions the computer will follow to answer a question about the data in a
database. The answers themselves are dynamic, it changes whenever the data in the database
changes, and thus is not part of the query but rather the result of the querying operation. The
results of a query are often referred to as a dynaset.
Specifying and performing a query involves the use of relational algebra operations such as
select, project, join, etc. Several languages exist that can be used to create database tables,
relationships, and queries. One of the oldest is SQL, which stands for Structured Query
Language (pronounced "SQL" or "see-qwill"). It was originally developed by IBM for use on its
mainframe computers. Fortunately, in modern day databases, we will neither have to
understand how to write instructions in SQL or the process by which the computer actually
obtains the dynaset. A user of Microsoft Access can specify a query using a tool called Query by
Example (QBE). This approach makes use of the graphics capabilities of the computer (GUI
interface) to allow the user to select the elements required for a query. The software then
translates QBE grids into SQL for processing by the computer.
Page 1
Using Databases to Solve Problems
Before we can begin to use the query tool, we or someone else must have already completed the
following tasks:
Created tables with defined fields, field properties, and primary keys.
Entered data into the tables.
Set up relationships between the tables using foreign key(s).
The next section will describe a sample database that meets these requirements and will be used
to demonstrate the query throughout the chapter.
A SAMPLE DATABASE
In order to illustrate some of the query operations we will utilize the sample database seen in
Figure 1.
Members
ID# Name Phone Address Active Join Date
635 Acquila, Jose 614-927-8345 Pickerington, OH Yes 1/5/97
534 Adler, Lawrence 614-746-2385 Pataskala, OH Yes 3/4/99
345 Amico, Donald 419-345-8472 Toledo, OH Yes 1/5/97
546 Anderson, William 513-345-9384 Cincinnati, OH No 4/10/00
903 Applegate, Bernice 513-485-3833 Cleveland, OH Yes 3/17/01
002 Aston, Martin 614-485-3888 Columbus, OH No 11/7/98
047 Attwater, Lester 614-292-1442 Columbus, OH Yes 9/1/00
Finances
ID# Dues-Paid Donation
635 100 0
534 50 0 Figure 1: Club Database Database
345 100 50 View of Tables
546 0 250
903 0 0
534 50 20
This database contains two tables: Members and Finances. The table structures are as follows:
Note that when typing a Boolean field into an Access table, the column is usually displayed
as check box. In this case, a check indicates that this is an active member; no check indicates
Page 2
Using Databases to Solve Problems
a past or retired member. The actual values stored are the Boolean values. If an Access table
with a Boolean field is copied into an Excel spreadsheet, the Boolean values are translated to
True/False cell entries.
Both tables contain and an ID# field representing the same information. Both these fields are
defined as type Number. ID# is the primary key of the Members table. Thus all three
requirements are met to set up a relationship (same information, same data type, primary of at
least one table). The ID# fields create a foreign key relating the two tables, as diagrammed in
Figure 2. We have now completed setting up the database relationships and are ready to ask
questions about the database.
Members ID#
Primary key: ID# Finances
In Access, the query object contains the instructions the computer will carry out to answer a
specific question about the data in an Access database. These instructions tell the computer
what set of data needs to be examined, and what filters and/or sorts to apply. Executing an
Access query results in a dynaset, a set of data based on the most recent information in the
database.
There are two different ways to use the query tool in Access; one way is to follow a set of steps in
the Query Wizard and the other is to use the Query Design tool. Both of these options can be
launched from the Create ribbon in the Other group. This chapter will deal exclusively with the
latter option, the Query Design tool. Figure 3 displays the Query Design tool window including
the Show section and the QBE (query by example) grid. Figure 4 displays the content sensitive
Design ribbon containing the different design commands available.
Page 3
Using Databases to Solve Problems
Transaction Table
added to QBE grid
Figure 4
Account# &
Transaction$
fields selected
Figure 3
In the upper portion of Design window, the data objects needed for the query (either tables or
previously created queries) are displayed. In the lower portion on the QBE grid, the user
specifies the fields, criteria, sorting, and information needed to carry out the query. This section
is divided into a series of rows as follows:
FIELD: Lists of all the fields involved in the query.
TABLE: For each field entered, the table it came from must be listed. Sometimes two tables
may have a field with the same name so the table is necessary to distinguish the fields.
TOTAL: This line is normally not shown. It can be toggled using the toolbar button. It is
used in queries that require grouping of data.
SORT: This line is used when sorting. One or more fields can be organized in Ascending or
Descending order.
SHOW: This box is checked by default and is used to specify whether or not the field will be
shown in the resulting dynaset.
CRITERIA: This specifies criteria that a field must meet in order for a record to be included
in the resulting dynaset. e.g., Donation >= 200.
OR: This specifies any alternate (Boolean Or) criteria.
The mechanics of selecting the data objects and selecting the appropriate fields is covered in the
course textbook. Here, the focus will be on which design elements are needed to solve specific
problems.
Page 4
Using Databases to Solve Problems
How can a question be translated onto the QBE grid? As with any problem solving activity, it
helps to have a methodology containing a set of logical steps to follow. The following is a
simplified method for taking a question and translating it into a QBE grid. This procedure will
be extended in later chapters to allow for the use of multiple tables in a query.
When filling out the QBE grid it is always wise to plan ahead, since the next task will be to
select the table to be included in the query. In order to know which table (or previous query)
is required, the following information must be known:
o What fields are to be shown in the resulting query and in which table (or previously
written query) can these fields be found?
o What field(s) will be filtered and therefore what criteria should be specified for that
field? For example, if only a list of active members is desired, the criterion Yes must
be specified in the field Active.
Figure 6
Page 5
Using Databases to Solve Problems
1. List each of the fields to be shown on the resulting dynaset. Select the fields from the
table(s) shown. The detailed mechanics are described in the course text.
2. List any fields not shown but that will contain criteria. Make sure to uncheck the show
box for such fields.
3. Write the necessary criteria in the appropriate fields using the Criteria and OR lines.
4. Use the Sort feature, Group by feature, and Expression builder as needed.
SELECT QUERIES
This section discusses writing queries that select specific records based on a set of criteria. This
includes a single select criterion in a single field, multiple criteria in a single field, and multiple
criteria in multiple fields.
First, the methodology described in the previous section will be used to create a query
containing a single condition on a single field. A condition can be specified by using the Criteria
line of the QBE grid. Consider the following example:
Write a query to select all active members, listing their names and addresses. The
results should be similar to structure/data listed in Figure 7.
Step 1: Plan the QBE grid. The query specifies that the Name Address
Name and Address fields are to be shown in the Acquila, Jose Pickerington, OH
resulting dynaset. Both are found in the Members Adler, Lawrence Pataskala, OH
table. In addition, a criterion that only active Amico, Donald Toledo, OH
members should be included on this list is specified, Applegate, Bernice Cleveland, OH
requiring a condition on the Active field (i.e., that it is Attwater, Lester Columbus, OH
equal to Yes), which also is on the Members table. Figure 7
Step 2: Add the Members table on the QBE design view in Access. All information needed to
answer the question is found in this table
Step 3: Select the appropriate fields (Name, Address, and Active) and place them in the grid.
Since Name and Address are to be shown in the dynaset, be sure the Show checkbox for each
is checked. Uncheck the show box for Active, as this field requires a criteria but is not asked
to be shown in the final result.
Page 6
Using Databases to Solve Problems
Fill in the selection criteria on the Criteria line of the QBE grid. Place the criteria, in this
case, in the Active field column on the Criteria line. Since this is a Boolean type field the
criterion can be either Yes or No. Since only Active members are specified, the criterion will
be Yes. Quotes should not be used around Yes since it is a value in a Boolean field and not a
text label.
Step 4: Save the QBE grid by naming the query Query1. The final version of the QBE grid can
be seen below (Figure 8).
Tables: Members
Field Name Address Active
Table Members Members Members
Sort
Show x x
Criteria Yes
Figure 8 Query 1
Logically, what steps are required to obtain this dynaset? To obtain this result, one would
systematically go through each record of the Members table and test to see if the criteria are
met. If the criteria are met (in this example, the member is active), then the appropriate fields
for that record should be displayed. Notice that the records for Aston and Anderson do not
appear since they are not active members.
Another example of how a QBE grid is designed to answer a question with a single criterion is as
follows:
Write a query to list the names and addresses of all Members who live in Bexley,
Ohio.
Table: Members Using the methodology outlined, the QBE grid can
Field Name Address be written as seen in Figure 9. Notice the criterion,
Table Members Members Bexley, OH, is a criterion on a text field and
Sort therefore requires quotation marks.
Show x x
Criteria Bexley, OH If you run this query you will discover that the
OR dynaset contains no records, since none of the
members live in Bexley.
OR
Figure 9 Query 2A
Is it a good idea to have a field in a database that contains both the city and state combined, as is
the case in the example database? This is probably not the best way to organize data in a table
as the city and state is not an inseparable unit. At times only the city or only that state may be
needed. A better way to have designed the original table would have been to make these two
pieces of information separate fields.
Page 7
Using Databases to Solve Problems
Such a query contains no criteria. The two fields containing the required information should be
placed on the QBE grid and the criteria row should be left blank, as seen in Query 2B.
Table: Members
Field Name Address
Table Members Members
Sort
Show x x
Criteria
OR
Figure 10 Query 2B
It is also possible to use relational operators to specify criteria on a QBE grid. For example:
Write a query to list by ID# all transactions with donations of greater than $0.
Show the ID# and donation amount.
What fields will be shown? ID# from the Finances table and Donation amount from the
Finances table.
What fields will have criteria that are not shown? No additional fields
What criteria are needed? The donation field will have the criteria >0. This should be
inserted on the grid in the column containing the Donation field on the row labeled Criteria.
What table(s) are needed? All fields used are from the Finances table.
The query grid and resulting dyanset are shown in Figure 11.
To specify a greater than criteria, use the greater than operator followed by the value. The syntax
does not include quotes when using a numerical type field. The following relational operators
can be used in an Access query.
Page 8
Using Databases to Solve Problems
Another tool that can be used when writing a criterion on a text field is to use a wildcard. The
* wildcard can be used to replace one or more characters. The ? wildcard can be used to replace
a single character. Whenever a wildcard is included in a criterion the expression Like must
precede the expression.
Like J* - this tells the computer the criterion is anything that starts with a J no matter
how many characters it has or what they are: Jay, Justice, Jo, would all meet the criteria
Like t*p this tells the computer the criterion is anything that starts with t and ends with
p. top, tip, tekdp all would meet the criteria.
Like *4* this tells the computer the criterion is any entry that contains the character 4.
Like ?d - this tells the computer the criterion is anything that ends in d and has one
character in front of it. Id, bd, od would all meet the criteria. Tad would not meet this
criteria
Like s??n this tells the computer the criterion is anything that is four characters that
begins with s and ends with n. sean, soon, sxxn would all meet this criteria.
Page 9
Using Databases to Solve Problems
Write a query to list all members who have phone numbers that begin with 614.
The QBE grid (Query 4 in Figure 13) and resulting dynaset are as follows. Notice that since
614 is part of a text field, the numeral characters are enclosed in quotes.
Tables: members
name
Field Name Phone Acquila, Jose
Table Members Members Adler, Lawrence
Sort Aston, Martin
Show x Attwater, Lester
Criteria Like 614*
OR
Figure 13 Query 4
The examples shown thus far have been limited to using a single criterion one condition in one
field. Often multiple criteria are required to obtain the desired record set. An example of
multiple criteria in one specific field is as follows:
List the names of all members who either live in Pickerington or in Columbus.
The QBE grid to list the members names either living in Pickerington, OH or in Columbus,
OH is seen in Figure 14.
Table: Members
name Field Name Address
Acquila, Jose Table Members Members
Aston, Martin Sort
Attwater, Show x
Lester Criteria Pickerington,OH OR
Columbus, OH
OR
Figure 14 Query 5A
Notice the use of the Boolean operator OR within the Criteria Box of the QBE grid. The OR
operator in Access has the same meaning as the one in Excel, but the syntax is different.
Another way the same criteria can be written is to place the OR item on a separate line. These
OR lines assume that any item listed has an or relationship to the criteria above. An example
of this alternate QBE design is seen in Query 5B on Figure 15. The resulting dynaset will be
identical to the one generated in Query 5A.
Page 10
Using Databases to Solve Problems
Table: Members
Field Name Address
Table Members Members
Sort
Show x
Criteria Pickerington, OH
OR Columbus, OH
Figure 15 Query 5B
Another example: List the ID#s and transaction amounts for all donations
between $50 and $100.
The criteria here can be interpreted as >=$50 and <=$100. The QBE grid would be as follows:
Table: Finances
Field ID# Donation
Table Finances Finances
Sort id# donation
345 50
Show x x
Criteria >=50 AND <=100
OR
Figure 16 Query 6
Notice that the Criteria box for the Donation field in Figure 16 contains a familiar Boolean
operator, AND, again with a different syntax than seen in previous applications. The resulting
dynaset contains only one record. Only member 345 made a donation between 50 and 100
dollars.
Another valid syntax to express >=$50 and <=$100 is between 50 and 100.
Can the AND operator be eliminated and the second criterion (<=100) placed on the next line of
the grid? No, the OR lines represent or operations only.
What if the criteria in the donation field looks like this: $50 AND $100? Logically, no single
record can have both a $50 value and a $100 value in the donation field simultaneously. Thus
no records will be selected.
Page 11
Using Databases to Solve Problems
The next step in learning how to set up QBE grids is representing multiple criteria in multiple
fields. Consider the following example:
Here there are criteria is on both the Address and Active fields simultaneously. Both of these
criteria must be met for a record to be selected. This indicates an AND operation is needed. To
represent an AND relationship between two or more fields on a QBE grid, place the criteria
on a single line. So the resulting grid and dynaset for this example would be (Figure 17):
Tables: Members
Field Name Address Active
Table Members Members Members
Sort name
Attwater, Lester
Show x
Criteria Columbus, OH Yes
OR
Figure 17 Query 7
Now consider the case of listing all active members from Columbus and all inactive
members from Cincinnati. Include the members name, address and active status. Here we
have the following logical operations:
To express the AND operation place the criteria on the same line. To express the OR operation,
place one set of criteria on one line and the second set on the other. Thus the resulting QBE grid
and dynaset will look like the following:
Tables: Members
Field Name Address Active name address active
Anderson, William Cincinnati, OH No
Table Members Members Members Attwater, Lester Columbus, OH Yes
Sort
Show x x x
Criteria Columbus, OH Yes
OR Cincinnati, OH No
Figure 18 Query 8
Page 12
Using Databases to Solve Problems
The use of the Sort feature to temporarily organize records on tables was discussed in Chapter
2.1. A similar but more powerful feature is available in the query tool. To sort a field in a query,
select the field to be sorted and indicate the sort order (Ascending or Descending) on the Sort
line of the QBE grid. Sorting can be combined with any other set of criteria.
Write a query that will list the names of all Members, their address and join date.
List them in order of join dates, with the most recent first.
To accomplish this sort, the word Descending is added to the grid on the Sort row in the
corresponding field column. Dates, much like in Excel, are represented by values, where the
lower the value the earlier the date. Thus to sort the Join Date field by most recent date first, the
word descending is used. The word Ascending is used to sort from smallest to largest value
(earliest date first). When entering a sort, click on the Sort row in the column desired, and
choose either Ascending or Descending from the drop-down list.
Again consider the same query but this time sort by address and then by join date. This
requires sorting on multiple fields. The question is which field is sorted first which field is the
major sort and which field is the minor sort? Should the list have (1) all like addresses together
organized by join date, or (2) all like join dates together organized by address (Figure 20)?
Page 13
Using Databases to Solve Problems
(1)Sorted First by City then Date (2) Sorted first by Date then City
Cincinnati, OH 4/10/00 Pickerington, OH 1/5/97
Cleveland, OH 3/17/01 Toledo, OH 1/5/97
Columbus, OH 11/7/98 Columbus, OH 11/7/98
Columbus, OH 9/1/00 Pataskala, OH 3/4/99
Pataskala, OH 3/4/99 Cincinnati, OH 4/10/00
Pickerington, OH 1/5/97 Columbus, OH 9/1/00
Toledo, OH 1/5/97 Cleveland, OH 3/17/01
Figure 20
Notice that the order of the records in each dynaset differs. In case (1) the location field is the
major sort and the date field is the minor sort. In case (2) the date field is the major sort and the
location field is the minor sort. How is the sort order specified on the QBE grid?
Thus far the order of the selected fields on a QBE grid has not been important as it has only
impacted the order of the fields displayed and not which records are selected. However, when
trying to sort records into a specific order with multiple sort fields, the order of the fields listed on
the QBE grid is critical to obtaining the desired outcome. The placement of these fields on the grid
will determine which field is the major sort and which fields are the minor sorts. In other words,
the order of the fields in the QBE grid determines the order of the records in the resulting dynaset.
When executing a query, first the computer selects the requested records based on the given
criteria. Then the computer sorts the records. It looks through the Sort row form left to right,
sorting the first field it finds in the QBE grid with sorting instructions. Then it continues to the
next column, sorting as instructed but this performing the minor sorts within the major sort
groupings. The next column is then considered for a minor sort, and so on. Sorted fields need not
be the first field nor adjacent to each other, but their order must be major sort first, then minor
sort 1, minor sort 2, etc. The QBE grid for (1) is Figure 21 and for (2) is Figure 22:
Write a query that will list the names of all Members, their address, and join dates.
Sort the list by address and then by join date (most recent first).
Tables: Members
Name Address Join Date
Field Name Address Join Date
Table Members Members Members Anderson, William Cincinnati, OH 4/10/00
Sort Ascending Descending Applegate, Bernice Cleveland, OH 3/17/01
Show x x x Attwater, Lester Columbus, OH 9/1/00
Criteria
Aston, Martin Columbus, OH 11/7/98
OR
Adler, Lawrence Pataskala, OH 3/4/99
OR
Acquila, Jose Pickerington, OH 1/5/97
Page 14
Using Databases to Solve Problems
Write a query that will list the names of all Members, their address, and join date.
Sort the list by join date (most recent first) and then by address.
Tables: Members
Name Join Date Address
Field Name Join Date Address
Applegate, Bernice 3/17/2001 Cleveland, OH
Table Members Members Members
Attwater, Lester 9/1/2000 Columbus, OH
Sort Descending Ascending
Anderson, William 4/10/2000 Cincinnati, OH
Show x x x
Adler, Lawrence 3/4/1999 Pataskala, OH
Criteria
Aston, Martin 11/7/1998 Columbus, OH
OR
Amico, Donald 1/5/1997 Toledo, OH
OR
Acquila, Jose 1/5/1997 Pickerington, OH
Figure 22 Query 11 Sort (2)
Re-write Query 11 to only display records for members who joined before January 1,
2000.
Use the exact same QBE grid, but include a criterion in the Join Date field. Note that when typing
in dates, # signs are used before and after the date. Dates can be expressed as mo/day/yr -
#1/1/2000#, or they can written out as follows - #January 1, 2000#. The # allows Access to
determine that #1/1/2000# is January 1, 2000 and not the number 1 divided by 1 divided by
2000. Figure 23 is the QBE grid and resulting dynaset for this query.
Figure 23 Query 12
How much has each member donated in total? To answer this question manually requires going
through each transaction record and adding up the donations for each specific member number.
For example member 534 has two transactions listed: one with a donation amount of $50 and the
other with a donation amount of $0. Thus the total donation amount for member 534 is $50. This
process will need to be repeated for each member id to find the total for each member.
Access queries can be used to accomplish this same task using a feature known as Aggregate
Functions. This feature can perform tasks such as counting all the members living in Columbus,
Page 15
Using Databases to Solve Problems
OH, totaling the dues or donations by ID#, or even to determine the average donation amount.
Microsoft Access has the following built in aggregate functions: Sum, Avg, Count, Min, Max,
Stdev, Var, First, Last. This aggregation of data may be done on all selected records or in
specific groupings using the Group by specification in the desired field.
Look at the QBE grid below that counts all members by location. The resulting dynaset
should have a record for each location and a count of field containing the number of records
for that location.
Tables: Members
address CountOfid#
Field Address ID# Cincinnati, OH 1
Table Members Members Cleveland, OH 1
Total Group by Count Columbus, OH 2
Sort Pataskala, OH 1
Pickerington, OH 1
Show x x
Toledo, OH 1
Criteria
OR Figure 24 Query 13
Aggregating is specified on the Total line of the QBE grid. This line does not automatically
appear on the grid. To display the Total line, click on the button in the Design ribbon. If an
aggregate function is specified for any field in the grid, the total line must be filled in for every
other field in the grid. The Total boxes in the row must be filled in either with a Group by, a
function (Count,Sum, Avg etc), Where (in fields with criteria), or Expression(for calculated
data).
The grid in Figure 24 groups the Address field so that all like entries in this field (Columbus
entries, Toledo entries, etc) are put together into a group. Then within each group, the
records in that group are counted using the ID# field. Notice that in the resulting dynaset, the
field name for this counted field is now referred to as CountofID# instead of ID#.
In the example above, the records in the ID# field of the Members table were counted. This query
would work just as well if the phone number field or any other field were used to count the records
in this grouping. The new field names in the dynaset would then correspond to the field used,
CountofPhone etc. However, when summing a field (or using min, max, average or stdev) only the
field being summed may be used, as demonstrated in a later example.
Also consider what happens if the Names field were to be included in a query to count the number
of members by location? Consider that each grouping of city may include many different members
and thus many different member names. If the query is grouped by Name and City, the computer
will automatically group by the smallest level of detail, in this case Name. Hence, the subtotal
calculated will no longer be for each city as desired, but for each Name. Thus, it is necessary to
plan the groupings to correctly reflect the level of aggregation.
Page 16
Using Databases to Solve Problems
Tables: Members
Field Address ID# Active
Table Members Members Members address CountOfid#
Cleveland, OH 1
Total Group by Count Where
Columbus, OH 1
Sort Pataskala, OH 1
Show x x Pickerington, OH 1
Criteria Yes Toledo, OH 1
OR
Figure 25 Query 14
By ID#, create a summary listing the total dues-paid and the average donation made.
Assume each member can have multiple dues and donation payment transactions. The QBE grid
and resulting dynaset are illustrated in Figure 26.
Tables: Finances
id# SumOfdues-paid AvgOfdonation
Field ID# Dues-Paid Donation
345 100 50
Table Finances Finances Finances 534 100 10
Total Group by SUM AVG 546 0 250
Sort 635 100 0
Show x x x 903 0 0
Criteria
Figure 26 Query 15
Recall member 534 had two transactions. One transaction was a $50 dues payment; the other was
a $50 dues payment and a $20 donation. Notice that the aggregated values displayed for member
534 are $100 in total dues payments and $10 in average donations per transaction. As previously
mentioned, unlike the Count function the other aggregate functions such as Sum, Avg, Min, Max
must be used only on the field being summed, averaged etc.
How can the total dues paid by all members and the overall average donation made
by members be obtained? It would look almost the same as Query 15, but exclude the ID#
field grouping (Group by). Query 16 details the results; notice only a single record containing
these values is returned.
Tables: Finances
Field Dues-Paid Donation SumOfdues-paid AvgOfdonation
Table Finances Finances 300 53.33
Total Sum Average
Figure 27 Query 16
Sort
Show x x
Criteria
Page 17
Using Databases to Solve Problems
Consider the follow problem: Write a query that adds Dues-Paid to the Donations for
each transaction.
To create a query to calculate these values from the existing data fields, follow the steps below:
Exp1:[Finances]![Dues-Paid] + [Finances]![Donations]
Page 18
Using Databases to Solve Problems
Notice the expression syntax which is generated: each object (table, field) appears inside of
brackets [ ], and that table names are followed by an exclamation point (!). Use this syntax when
writing expressions.
Also notice that this expression is named Exp1 followed by a colon. Expressions can be renamed
by simply typing over Exp1 on the QBE grid. To use any object already listed (or previously
calculated) in the current query, save the query before writing the expression. By doing so, these
objects now become available to use in the Expression builder. When referring to fields already
selected on the QBE grid, the table names are not required in the formula unless the names are
ambiguous (i.e., several fields have the same name this can occur when using multiple tables).
Placing this expression into the appropriate query we get the following:
Tables: Finances id# total
Field ID# total: [Finances]![dues-paid]+ 635 100
[Finances]![donation] 534 50
345 150
Table Finances
546 250
Sort 903 0
Show x x 534 70
Criteria
Figure 29 Query 17
Queries may become more and more complex as the many tools available are combined together.
Thus far queries have been used to select (filter) records using one or more sets of criteria, sort
records, aggregate data based on field groupings, and now perform calculations. In the following
query, both the aggregation of data and the use of expressions will be required.
Summarize for each member id the dues-paid, donations made, and total value of
their transactions. Remember that an individual member can make one or more financial
transactions. Thus to total these transactions the Aggregate function tools will be required. Once
these aggregated values are saved, the expression builder feature can be used to add SumofDues-
Paid to SumofDonations.
This may be done in a single query or with the use of a previously written query such as the
previous query, which has already calculated the total dues plus donations and just requires
aggregation. The example below uses the one-step approach, both aggregating the data and then
applying the expression. Please note that not all questions can be answered in a single query. The
query tool first aggregates data and then does the calculation. In some instances the order of these
two operations may require opposite, first a calculation is required then the data needs to be
aggregated. In such cases the user will need to create two queries.
Page 19
Using Databases to Solve Problems
Tables: Finance
Field ID# Dues-Paid Donation total: [sumofdues-paid]
+[sumofdonation]
Table Finances Finances Finances
Total Group by Sum Sum Expression
Sort
Show x x x x
Criteria
Figure 30 Query 18
To execute this query it is best to create the query excluding the expression and then save
it. Only after it is saved will these values (SumofDues-Paid, SumOfDonations) be available for use
in the Expression Builder. Notice that the Total row contains the word Expression in the field
containing the formula. Whenever the total row is used (whenever there are aggregations) fields
containing calculations must include Expression.
Determine the amount still owed, assuming members are required to pay a total of
$100 in dues. To accomplish this, the information generated in the previous query will be
needed, specifically the sum of the dues paid by member. Thus a new query based on the results
of Query 18 will be constructed. The calculation required will be the $100 minus the SumOfDues-
Paid
Tables: Query 18 id# owed
Field ID# dues owed: 100 [Query18]![Sumofdues-paid] 345 0
Table Query18 Query18 534 0
546 100
Sort
635 0
Show x x 903 100
Object used is a
Criteria previous Query
Figure 31 Query 19
Notice that in Query 19, Query 18 was used instead of a table. It is permissible to use any
combination of tables and query objects to create new query objects. Note the syntax of the
expression in the second query. The expression is named dues owed. The formula subtracts the
previously calculated amount from a constant. Here the object identifies the data source as
[Query18]! with a table name it is followed by an exclamation point. The field name is
[Sumofdues-paid] (since the dues-paid field was summed in Query 18, the resulting field in the
Query 18 dynaset is named Sumofdues-paid).
Page 20
Using Databases to Solve Problems
NULL VALUES:
Consider the original finances table. When a member makes a dues payment without a donation,
a zero has been entered in the donation field. But what if that field is left blank as seen on the
modified table in Figure 32? These blank fields in a record are known as null values.
In Excel, blank cells are considered to be 0 when they are added. For example, the result of the
expression A1+B1, where cell A1 is blank (null) and cell B1 contains 10, is the value 10. Excels
algorithm for dealing with a blank cell in addition is to ignore it. In Access however, these null
values are treated differently. In a calculated field of a query with the formula [sumofdues-
paid] +[sumofdonation], if the donation amount is null for a specific record, the value
returned will be null for that record.
This propagation of null values can often lead to incorrect results. Lets consider the affect this
modified table would have on the QBE grid from query 17 that is designed to calculate total
transaction value (dues+donation). Observe that the total value for the first record is null. The
computer performs the calculation 100 + null which results in a null value. So even though
member 645 made a $100 payment the total value displayed is null.
Figure 34
Page 21
Using Databases to Solve Problems
Obviously this poses the problem of how to obtain an accurate subtotal of each financial
transaction. One of the easiest ways to work with fields that may contain null values is to use the
Nz function. The Nz function works like an IF statement. It takes a value in a field and if the
value is null, it substitutes an alternate value. If the value is not null, it simply returns the original
value. The value substituted for the null value can be user defined, or in the absence of a user
definition it will default to zero.
Nz ([variant], valueifnull)
Here is the modified query using this Nz function in the calculated field expression as follows:
Field ID# total: Nz([Finances]![dues-paid])+
id# total
Nz([Finances]![donation]) 635 100
Table Finances 534 50
Sort 345 150
546 250
Show x x 903 0
Criteria 534 70
Figure 35
Page 22
Using Databases to Solve Problems
The following Access tables represent staff utilization for an ad agency. Each record in the Jobs1
and Jobs2 tables represents a task performed for that job. Answer the questions in this problem
based on the tables below. You will be asked to list all tables required to complete each query.
1. (a) Draw the relationship diagram between these 3 tables. Label each primary key and
the foreign keys on your relationship diagram.
(b) Why cant employee# be the primary key on the Job1 table?
Page 23
Using Databases to Solve Problems
2. Design a query to list for each Job2 task the following information: the taskid#, employees id
and minutes they worked on this task.
Table __________________________________
Field
Table
Total
Sort
Show
Criteria
OR
OR
3. Design a query to list the taskid# and employees id for each Job1 task which lasted at least 15
minutes.
Table __________________________________
Field
Table
Total
Sort
Show
Criteria
OR
OR
Page 24
Using Databases to Solve Problems
4. Design a query to list the employee id number for employees who worked on Job2 on either
July 1, 2006 or on July 10, 2006.
Table __________________________________
Field
Table
Total
Sort
Show
Criteria
OR
OR
5. Design a query that lists the employees id and date and minutes spent for the Job 1 tasks that
were between 15 and 20 minutes long (inclusive).
Table __________________________________
Field
Table
Total
Sort
Show
Criteria
OR
OR
Page 25
Using Databases to Solve Problems
6. Using the Access Query design view below, design a query that results in a list of employees
(Employee#) who worked on Job1 who:
Worked for more than 10 minutes on a task on or after July 10, 2006 or
Worked at least 5 minutes on a task before July 10, 2006.
Table __________________________________
Field
Table
Total
Sort
Show
Criteria
OR
OR
7. Using the Access Query design view below, design a query that will result in a list of all
employees whose last names begin with the letter M or N.
Table __________________________________
Field
Table
Total
Sort
Show
Criteria
OR
OR
Page 26
Using Databases to Solve Problems
8. Using the Access Query design view below, design a query to list the names of each employee in
alphabetical order.
Table __________________________________
Field
Table
Total
Sort
Show
Criteria
OR
OR
9. Using the Access Query design view below, design a query to list the Employees who earn more
than $35 per hour (job rate). List each employees name, Empoyee#, and Job Rate. Sort your list
by Job Rate (most expensive first) and then alphabetically by name.
Table __________________________________
Field
Table
Total
Sort
Show
Criteria
OR
OR
Page 27
Using Databases to Solve Problems
10. Design a query summarizing the Job2 tasks by employee. The list should include Employee#
and the total minutes worked by that employee.
Tables __________________________________
Field
Table
Total
Sort
Show
Criteria
OR
OR
11. Design a query summarizing the Job1 tasks by employee#. The list should include Employee#,
the number of tasks they worked, and the average time per task that they worked.
Tables __________________________________
Field
Table
Total
Sort
Show
Criteria
OR
OR
Page 28
Using Databases to Solve Problems
12. An overhead fee of $5 per task is assessed for each task regardless of duration. Using the
result from the previous query (which you named query11), write a query in the design view below
which will return the Employee#, and the total amount of overhead fees for this employee for
Job1. If an employee has not worked their name should not appear on this list.
Tables __________________________________
Field
Table
Total
Sort
Show
Criteria
OR
OR
Page 29
Using Databases to Solve Problems
13. Currently clients are billed the cost of the tasks completed (hourly rate times hours) plus the
overhead fee assessment. An alternative method of billing overhead has been suggested by
management as follows: Hourly overhead costs will be billed at 25% of the hourly wage plus $3
per hour. So an Ad Exec with an hourly wage of $125/hour would have an hourly overhead charge
of $125*.25 +3. Write a query to calculate the hourly overhead charge for each of the different
employees listed on the employees table. If the Job Rate field is blank the overhead charge should
simply be $3.
Table __________________________________
Field
Table
Total
Sort
Show
Criteria
OR
OR
Page 30
Using Databases to Solve Problems
The tables below represent the database system for the Fergie Library. The Book table lists the
names of all the books and periodicals owned by the library. The Cardholder table lists the names
of all authorized library users. The Circulation table is a running list of books that are borrowed.
When books are borrowed, they are entered onto this table, and when they are returned, the
original entry is noted as returned.
Books
Book# Title Author Type Price
23456 1812 Vidal 4 $39
834592 Gone With the Wind Mitchell 4 $19
4539 Websters Unabridged Dictionary Webster 3 $86
33982 Hunt for Red October Clancy 1 $22
33983 Hunt for Red October Clancy 1 $22
43928 Joy of Cooking 5th Edition Smith 2 $45
Circulation
Borrow# Book# Date Borrowed Returned
A83765 4539 7/1/98 Yes
A83765 33982 7/1/98 No
C52938 4539 7/4/98 No
C77328 43928 7/6/98 Yes
Cardholders
Borrower# LastName FirstName Address City State Zipcode
th
A23457 Smith Michael 222 East 7 ST Bexley OH 43209
C52938 Jones Jennifer 18 Main Street Dublin OH 43218
A83765 Walters John 55 Elm Street Columbus OH 43213
1. Database Relationships. Set up the relationships of this database. Using the boxes below, fill in
the primary key (if any) of each table and draw relationship lines between tables. Label each
relationship with the name of the foreign key(s).
Page 31
Using Databases to Solve Problems
2. What field type (Text, Number, Currency, Date/Time, Yes/No, AutoNumber) is best suited for
each of the following fields:
3. Using the query design view below, construct a query to list the book titles and authors of all
the type 4 books in alphabetical order by author then by book.
Field
Table
Total
Sort
Show
Criteria
OR
OR
4. List the dynaset created from the query in question #3 based solely on the data shown.
Page 32
Using Databases to Solve Problems
5. List the title of all books that begin with the letters G or J.
Field
Table
Total
Sort
Show
Criteria
OR
OR
6. List the book# and borrow# of all books borrowed between June 1, 1998, and July 4, 1998.
Sort the list by the borrowers number.
Field
Table
Total
Sort
Show
Criteria
OR
OR
Page 33
Using Databases to Solve Problems
7. List the title of all books that are either type 1 and cost less than $25 or type 2 and cost less
than $50.
Field
Table
Total
Sort
Show
Criteria
OR
OR
8. Write a query that will summarize the books by author. The query should list the authors
name, the total number of books in the collection by that author, and the average price of that
authors books.
Field
Table
Total
Sort
Show
Criteria
OR
OR
Page 34
Using Databases to Solve Problems
9. The library would like to calculate the accrued fines for lost books. You will assume that all
books borrowed before January 1, 2001 and not returned are lost. The following fine is
charged for all lost books: $.05 per day from the date borrowed plus a $10 flat fee handling
charge. For each lost book, list the book number, the borrower number, and the fine.
Field
Table
Total
Sort
Show
Criteria
OR
OR
Additional Expressions:
Page 35