You are on page 1of 26

Querying a

Database

Prof. Himanshu Joshi


International Management Institute
himanshu@imi.edu

Information Technology for


Decision Making (ITDM)
What is a Query?

• Queries are the information retrieval


requests you make to the database
• Your queries are all about the
information you are trying to gather

Information Technology for


Decision Making (ITDM)
Creating a Query using Design View

• The Query window in Design view allows you


to specify which fields you want to be
included in your query results.
• Each column in the design grid represents a
field that will be used in the query.
• Add fields to the design grid by double-
clicking the field in the field list or by dragging
the field to the design grid.
– Remember that the results of a query provide a
temporary view of the data
– The contents displayed in a query are a result of the
fields you select and the criteria you specify in the
query design grid
Information Technology for
Decision Making (ITDM)
The Query Design view window

Fields used in the query are selected from You can run the query at any
the field list and added to the design grid. time by clicking the Run button.

Information Technology for


Decision Making (ITDM)
Selecting, displaying and sorting fields

You can use the mouse to click You can specify fields other than the
and drag fields to the design grid. primary key to use as sort fields.

Fields can be
added to the
query but not
displayed in the
results sheet by
removing the
check mark from
the Show: field.

Information Technology for


Decision Making (ITDM)
Sort data in a query
• You can control the order of data displayed in a query by
altering the queries sort key.
• Choose a field on which you want the data to be sorted and
then specify whether you want the data sorted in ascending
or descending order.
• Specify multiple fields as the sort key if you want to
arrange data in sorted order within groups of data.
• Specify sort criteria for the query in the query design grid.

Information Technology for


Decision Making (ITDM)
Sort results by data type

Depending on the type of data that you specify as a sort field, the data will
be sorted according to the data type as shown in the following figure.

Information Technology for


Decision Making (ITDM)
Sort single or multiple fields

To sort only one field, click in the desired field to select it, and then click the Sort
Ascending or Sort Descending button on the toolbar. To sort multiple fields, you
must specify them in the query design grid.

Information Technology for


Decision Making (ITDM)
Specify an exact match
criteria in a query
• You can specify an exact match criteria, which
allows you to specify that only records that match
your criteria will be displayed.
• To specify an exact match, use the criteria row in
your query design grid.
• When you run the query after specifying an exact
match, only those records that meet the criteria
will be displayed.
• An exact match condition specifies that the value
in the specified field must match the condition
exactly.

Information Technology for


Decision Making (ITDM)
Exact match operators and results

When creating an exact


match condition you
can use any of the
comparison operators
shown in the figure to
the right.

Information Technology for


Decision Making (ITDM)
Use a comparison operator to
match a range of values

You can use a comparison operator, such as the greater-than sign (>), to specify that
you want to see only records that have a value greater then the value specified. This
design grid shows a datasheet view with a criteria set to show records that have a
wage value greater than or equal to 17.

Note that this design grid also shows that the StartDate field had criteria
supplied from another run of the query. This condition needs to be
deleted, or otherwise, both conditions would be applied to this query.

Information Technology for


Decision Making (ITDM)
Use the And and Or logical operators
• When you need to use multiple conditions for a query, you
can use the logical operators to combine conditions.
– The And logical operator specifies that both conditions must be
met
– The Or logical operator specifies that one or the other of the
conditions must be met
• When you enter two conditions on the same row of the
query design grid, an And condition is created.
• If you enter two conditions that are on separate rows, an
Or condition is created.

Information Technology for


Decision Making (ITDM)
Illustration of And and Or logic

The And
condition.

The Or
condition.

Information Technology for


Decision Making (ITDM)
Creating And and Or conditions
in the design grid

And conditions must be


specified on the same line.
Or conditions must be specified on different lines.

Information Technology for


Decision Making (ITDM)
Filter data in a query
• Sometimes you may want to isolate a certain portion of
data in your query results.
• You can set a filter, or a set of restrictions, to specify what
records you want to view in the results. Only those records
that match the criteria will display in the results datasheet.
• There are two methods used to make your selections:
Filter By Selection or Filter By Form.
• If you no longer want to view the data with the filter
applied you can remove the filter.

Information Technology for


Decision Making (ITDM)
Using Filter By Selection

Select the value you want to use as a filter,


then click the Filter By Selection button.

Click the Remove Filter


button to display all records.
Access displays a message to
tell you a filter is applied.

Information Technology for


Decision Making (ITDM)
Apply an advanced filter in a table
• You can create advance filters that allow you to specify
multiple selection criteria and to specify a sort order for
the selected records.
• The Advance Filter window looks very similar to a query
design window and works in a similar manner.
• You add the fields to the design grid for which you want
to specify selection criteria and/or sorting specifications.
• Once you have added the criteria you want for your
filter, you can apply the filter by pressing the Apply Filter
button.
• You can also remove the filter by pressing the Remove
Filter button.

Information Technology for


Decision Making (ITDM)
The Filter window

In the Filter window, you select only the fields that will be involved in the filtering and
sorting specifications. The Filter window works very much like the query design grid.
You can specify filtering and sorting criteria for the Advanced Filter.

Information Technology for


Decision Making (ITDM)
Aggregate Query: Using Aggregate Functions

Access has several Aggregate Aggregate functions are specified in


Functions that can be used to the Total row of the design grid. They
calculate various statistical can be assigned by clicking the Totals
information. button on the Query Design toolbar.

Information Technology for


Decision Making (ITDM)
Create a crosstab query
• A crosstab query performs aggregate function
(arithmetic operations) calculations on the values
of one database field and displays the results in a
spreadsheet format.
• There are a variety of functions that can be used
in crosstab queries including average, count, first
field value, last field value, etc.
• Excel provides a Crosstab Query Wizard that will
walk you through the creation of the crosstab
query.

Information Technology for


Decision Making (ITDM)
Crosstab query aggregate functions

The figure below shows the aggregate functions that can be used in a crosstab query.

Information Technology for


Decision Making (ITDM)
A select query versus
a crosstab query

This figure shows a Select query


and a crosstab query. These two
queries are based on the same data.

In this case the crosstab query


is using the Sum function to
summarize the potential
However, notice that the crosstab income for each country.
query data is much more summarized
than the select query data.

Information Technology for


Decision Making (ITDM)
The Crosstab Query Wizard steps
• First: select the table or query upon which you want to
create the crosstab query.
• Second: select the fields that will be used for the row
headings.
• Third: select the field that will be used for the column
headings.
• Fourth: select the field that will be calculated upon and
what calculation is to be performed.
• In the list of queries, crosstab queries will have a special
icon that will identify them as such.

Information Technology for


Decision Making (ITDM)
Perform calculations in a query using
calculated fields and expressions
• Queries can be built to perform a calculation as part of the
query.
• Expressions can be entered into the query design grid.
• Make certain that you are following the rules of
precedence.
• To perform a calculation in a query, you must add a
calculated field to the query design.
• You have three options for entering expressions:
– Enter the expression directly into the field text box
– Enter the expression in the Zoom box
– Enter the expression in the Expression Builder

Information Technology for


Decision Making (ITDM)
The Expression Builder dialog box

The Expression Builder allows you to create expressions by


clicking fields and operators from list boxes and buttons.

Click a button to choose an operator.

Choose an Access
object from this pane.

Choose a field for the selected


object from this pane.

Information Technology for


Decision Making (ITDM)
A calculated field in
the query datasheet

Expression Builder adds your calculated field to the query design grid. You can then
assign it a name, which will display in query datasheet view when the query is run.

Information Technology for


Decision Making (ITDM)

You might also like