2.8 Creating A Query
2.8 Creating A Query
Contents
Creating a Query....................................................................................................................................2
Using Wildcards in a query....................................................................................................................2
Sum.......................................................................................................................................................3
Count.....................................................................................................................................................3
Not and Null Queries.............................................................................................................................4
Using Not...........................................................................................................................................4
Using Null..........................................................................................................................................4
Group By................................................................................................................................................4
Example query using Group By, Count and Sum....................................................................................5
Creating the Query............................................................................................................................5
Using Mathematical Operators.............................................................................................................6
Access operators and their meaning.................................................................................................6
Average Values......................................................................................................................................7
Maximum and Minimum Values............................................................................................................7
Calculated Fields in Queries...................................................................................................................7
Parameter Queries................................................................................................................................8
Using wildcard selection................................................................................................................9
Using range selections...................................................................................................................9
1
Access 2007 Quick guide to queries
Creating a Query
You can create a query two ways, Click on the Create Tab to
display the ribbon as above.
Within a database
2
Access 2007 Quick guide to queries
Sum
Various summary calculations can be carried out on numerical data within Access. The most
common includes finding the total and average of a set of figures, the
maximum and minimum value and counting the number of records. These
calculations are carried out on a total line within a query on the data. Instead
of displaying all records which met the criteria match, in a sum query the
calculations are applied to the selected records and only the summary result is
displayed in the query.
3. Click in the Total: box for the field you which to total, then click the down arrow to display
the drop down list. Select Sum from this list.
4. Run the query.
5. The answer table shows only one field, SumOffieldname. This is the total value of all the
data.
Count
The count calculation is another summary calculation, which counts how many entries in a query
have the value in a particular field, including duplicate values. It does not count null entries, I.E.
empty fields.
Note: Take care when choosing which field to use in the count. If any records on the table did
not have an entry in the chosen field, they would not be included in the count.
3
Access 2007 Quick guide to queries
Not and Null Queries
Queries may be applied to tables to exclude certain records when the query is run and therefore to
display all the records which do not match the criteria. It is also possible to search for fields that
have no data by using a Null query. This can be useful when you want to make sure that all records
have been fully completed.
Using Not
1. Open your query in Design View
2. In the criteria for your chosen field enter “Not” and the value you wish to exclude. See the
example below;
3. Run the query and bikes will be shown apart from ‘Giant’.
Using Null
1. Open your Query in Design View
2. To find records which have no data in a selected field. Type Null in the Criteria box for the
required field.
3. Run the query and the items returned are the ones with data in your selected field.
Group By
Using a summary code of Group By in a query does not show all selected records but will show a
result row for each different value in a field. Group By is often combined with other summary codes
to produce sub totals.
4
Access 2007 Quick guide to queries
Example query using Group By, Count and Sum
The Example uses the Bike Sales data which has been imported into Access 2007 from a CSV file on
the Edexcel website.
This query is going to show how many bikes of each model sold, using count, and the total amount
each model sold, using Sum.
5
Access 2007 Quick guide to queries
Using Mathematical Operators
Where values are used in tables, queries can be performed which select records based on a range of
values, e.g. quantities more that a certain amount, prices less than a certain value, or ages within an
age range. To do this requires the use of a range operators such as < (Less than) in the criteria.
Note: The £ sign is not needed when using range of value queries.
6
Access 2007 Quick guide to queries
Average Values
Avg is another summary calculation. It calculates the average of all values in a field. It is selected
from the Total: row in the query grid, in a similar way as Count and Sum. All such summary
calculations can be combined with selection criteria to obtain statistics for certain parts of the data.
The name of the new field is entered in the Field row of a query grid, followed by a colon, :. The
expression is then entered, with field names enclosed in the square brackets.
Once a calculated field is defined, it can be used like any other field. It can be used for selection, for
sorting or appear in forms and reports.
The standard mathematical symbols are Add +, subtract -, Divide / and multiply *.
7
Access 2007 Quick guide to queries
Parameter Queries
If a query is being run frequently and all that is changing is the selection criteria for one or two fields,
then a Parameter Query can be used. A parameter Query displays a dialog box prompting for the
criteria information. Once the information is entered, the query is performed, with the entered
value being used as the selection criteria. The next time the query is run, a different value can be
entered and used, without having to change the query design.
Parameters can be used together with other types of query, such as partial queries using wildcards
and range queries.
A query can contain as many parameter values as required. Simply set a parameter value for each
required field. Every time the query is run, a dialog box will appear for each field that contains a
parameter value. The dialog boxes will appear in the order of the field names in the query grid, from
left to right.
Note: Ensure that the prompt text between the square brackets is not the same as any field name.
This will confuse Access.
5. Enter the Town you wish to find and click ok. The
query will run showing all records where town
equals what you entered into the prompt box.
8
Access 2007 Quick guide to queries
Using wildcard selection
Parameters can be used with wild card selections.
When the query runs it replaces the [Enter Letter Required] with the value entered in the
prompt box as the criteria.
4. The query will show all records where the field starts with the lettered entered.
4. The query will show all records which have a value between the values you entered.