You are on page 1of 24

[Yarmouk University- DB Course

Training] presents:

Microsoft Office
Access 2003 Training
Part 2: Queries

Presented By: Bayan Ghozlan


Supervised By: Dr.Qasem Radaideh
Dr.Belal Abu Al-Huda

Access Tutorial - Bayan Ghozlan


What is Query?
• Query is a way to answer questions about the data in your
tables.

• It allows you to retrieve items in a database that meet a set of


criteria that you specify.

• Queries are the main reason for using a database.


Since:
The real reason for using a database is to “store data records
is so they can be retrieved using some criteria that is
specified interactively”. this is the definition of a query.

Access Tutorial - Bayan Ghozlan


Filters

• Other sort sort of retrieval


• Can be used to select or exclude a set of records from a
table that meets specified criteria.

Limitations of Filters

Powerful in their search and retrieval but..


• Selection limited to one table
• Cannot calculate new data
• Must retrieve the entire record

Access Tutorial - Bayan Ghozlan


Query vs. Filter
A query is more complex than a filter, since it can:

• Apply to multiple tables.


• Select individual fields instead of full records.
• Be saved and reused.

Access Tutorial - Bayan Ghozlan


Creating a Query
• Using Design View
• Using Wizard

Access Tutorial - Bayan Ghozlan


Creating a Query
Design View:
Create a query
from scratch. Simple Query Wizard
You will get a Create a select query
blank QBE1 from one or more
grid tables for simple
selections and
calculations

Cross tab Query:


Create a cross
tab table based on
a single table or
previously created
Find Unmatched query
Wizard: Find
records in one table Find Duplicates
that do not match Wizard: Find
records in a related duplicate records
table. within a single
table or query

Access Tutorial - Bayan Ghozlan


Using Design View to Create Queries
Select a data source:

1. Click on the name of the table or


query that you wish to use in
your query.

2. Click on Add.

3. Repeat for each table that you


want to add to the current query
4. Click on Close when finished
Alternative technique: you can
double click on the table names to
add them to the query.

Access Tutorial - Bayan Ghozlan


Query By Example Window

You can add tables to a query at any time by choosing from the
Query menu  Show Table
Access Tutorial - Bayan Ghozlan
Selecting Fields
• Only the fields that are added to the QBE grid will be displayed in the
resulting set.
• Fields can be added from multiple tables as long as there is a
relationship between the two tables.

• Fields can be added a the Query in one of three ways:

1. In the Field row of the QBE grid, click on the drop down menu and
choose the field name that you want to add.
2. Click and drag the field that you want to add from the table listing in the
top portion of the screen to the lower portion.
3. Double click on the field name that you want to add from the table
listings; the field should then appear in the right-most column of the QBE
grid. Use any combinations of these methods to add all of the fields to
the query that you wish to include.

• To run the query: Click ! Button, or by switching to Datasheet View


• Close the query and save it with the name FindOwners

Access Tutorial - Bayan Ghozlan


On your own:

1. Create a simple, single table query on any table in the


Northwind database. �
2. Select 2 or more fields from the table using your
favorite technique.
3. Specify a criteria for at least one of the fields (you
might need to preview the data first).
4. Save and close the query.

Access Tutorial - Bayan Ghozlan


Main Query Activities In Access

• Access provides the following query


activities some are:
1. Combining Criteria in a Single Field .
2. Combining Criteria using Multiple Fields .
3. Creating a Multi-table Sorted Query
4. Make Table Query.
5. Parameter Query .

Access Tutorial - Bayan Ghozlan


1. Combining Criteria in a Single Field

• Ex: Query will find all products with a price of $25 or


greater and less than or equal to $100.
1. Click on New in the Query objects
2. Select Design View
3. Click on OK
4. Add the Products table
5. Click on Close
6. Add the fields ProductName and UnitPrice
7. Enter >25 AND <100 in the criteria area under UnitPrice
8. Run the query
9. Switch to Design View
10. Change the criteria to >= 25 AND <= 100
11. Run the query.
12. Close the query and save it with the name Products between $25
and $100

Access Tutorial - Bayan Ghozlan


2. Combining Criteria using Multiple Fields

• Exercise: Query will find all customers from the USA


who are listed as owner or some type of manager in
the contact title field.

1. Click on New in the Query objects


2. Select Design View
3. Click on OK
4. Add the Customers table
5. Click on Close
6. Add the fields ContactName, ContactTitle, and Country to the grid
7. Set “USA” as the criteria for Country. Repeat this on the second row
8. Set the first criteria under ContactTitle as "Owner"
9. Set the second row of the criteria for ContactTitle as *manager
10. Run the query
11. Close the query and save it with the name USA Owners and
Managers
Access Tutorial - Bayan Ghozlan
2.Combining Criteria using Multiple Fields
Cont..

Access Tutorial - Bayan Ghozlan


3. Creating a Multi-table Sorted Query

1. Click on New in the Query objects


2. Select Design View
3. Click on OK
4. Add the Customers, Orders, Order Details, and Products
tables
5. Click Close
6. Add the fields CompanyName, ProductName, UnitPrice (from
Order Details), Quantity, and OrderDate
7. In the Sort row, under CompanyName, change it to Ascending
8. In the Sort row, under OrderDate , change it to Descending
9. Run the query
10. Close the query and save it with the name Order Info Sorted by
Company

Access Tutorial - Bayan Ghozlan


3. Creating a Multi-table Sorted Query
Cont..

Now.. On your own:

1. Create your own


query, using one or
more tables.
2. Sort by one or more
fields.
3. Specify a criteria, if
you want to.
4. Run the query.
5. Save and close the
query.

Access Tutorial - Bayan Ghozlan


Saving Query Results in a Table

• The data set that results from a query can be saved by creating a
new table to hold the data. This is done by creating a Make-Table
query.

When a Make-Table query is run:


• Access checks to see if there is an existing table with the name that
you have specified.

• If one exists, it will be deleted (with default settings you will see a warning
dialog window before this happens).

• Selects the data according to the criteria.


• Pastes the data into the new table. If you have not changed the
default settings, you will see a warning dialog window before this
happens.

Access Tutorial - Bayan Ghozlan


4. Make Table Query
1. Click on New in the query objects
2. Select Design View
3. Click on OK
4. Add the Employees table
5. Click on Close
6. Choose any fields to add to the query
7. Add criteria and sorting, if you want
8. Run the query to view the results

9. Switch back to Design View


10. Choose Make Table from the Query menu
11. Enter a name for the table (Make sure that the name you enter is different than
any object that already exists in the database.)
12. Click on OK.
13. Run the query – running the query is the only way that the table will be created.
Follow…

Access Tutorial - Bayan Ghozlan


4. Make Table Query Cont..

14. Close the query and save it with the name Make Table Test

15. Notice the different icon that this query has. This is because it is an
action query.

16. Click on the Tables object

17. Open your table and review the contents – it should appear the
same as when you ran the query in step 8

18. Close the table.

Note: Note that this table you created is a static snapshot of the database –
if you update or add to the original table the query was based on, this
snapshot will not change.

Access Tutorial - Bayan Ghozlan


5. Parameter query
• A parameter query prompts you for the selection criteria when you
run the query.

To create a parameter query:

1. Create a normal select query but omit the criteria.


2. In place of the criteria, place a parameter [Country?] .
3. This text will be your meaningful prompt to enter information. Repeat this for
each field that you want to define as a parameter .

2. from the Query menu Choose Parameters. to bring up the Parameter


Dialog Box .

3. In the dialog box, enter the first parameter exactly as it is in the criteria cell
but without the square brackets. The data type of the parameter should
match the data type of the field .

4. Repeat for each parameter in the sequence that you want to be prompted for
and then click OK
Access Tutorial - Bayan Ghozlan
5. Parameter query Cont..

Notes on Parameters:

1. If you have more than 1 parameter in a query, you will


be prompted for the input in the order that the fields are
entered into the Query Parameter dialog window.

2. You can use a wildcard in your parameter specification.


When you enter your parameter into the criteria area,
add the work like in front of the criteria – Like
[Country?]

Access Tutorial - Bayan Ghozlan


5. Parameter query Cont..

1. Click on New in the Query objects


2. Select Design View
3. Click on OK
4. Add the Customer table
5. Click on Close

6. Add the CompanyName, City, and Country fields


7. In the criteria row under the Country field enter [Country?]
8. Choose Parameters from the Query menu.
9. Type Country? In the first cell of the first row
10. Leave the data type as Text

11. Run the query – you will be prompted to supply a country name.
12. Close the query and save it as CompanyByCountry

Access Tutorial - Bayan Ghozlan


5. Parameter query Snapshots

Access Tutorial - Bayan Ghozlan


Thank You
Follow..
Access Forms

Access Tutorial - Bayan Ghozlan

You might also like