Exercise 4 Create Queries
Exercise 4 Create Queries
Exercise 4 Create Queries
Exercise 4
Create Queries
Queries are generally used to extract information from the tables and present it in a non-formal format or create formal
reports. Queries can be created from one table (single table queries) or multiple tables (multi-table queries).
In this class, we are primarily concerned with constructing queries using QBE (Query By Example) where you use a
graphical interface to select the tables and conditions to be queried.
The other form of query construction is through SQL (Structured Query Language) where you instruct the computer to
select certain fields and records by writing SQL statements.
For example: If you want to list the Customers’ Company Names and Titles, you need to display “CompanyName”
and “Title” for every customer, which are all located in the same table: Customers Table.
Example: List the company names and the product description for the stock items the customers purchased.
In this case, “Company” comes from the Customers Table and “Description” comes from the Stocks Table.
For Multi-table queries, you will need at least two tables and possibly more because we need to link (traverse) the tables
before we can get from one field to another. We will leave this for now and come back to it later.
1. Select the Queries Button / Tab on the Database Window, and select Create query in Design view.
Alternative way: Click on the icon bar at top right corner (you might have to use the pull down list to get to queries)
From the available selections, select the ‘Design View’ option and click OK.
3) Add the tables you will need to access the fields you want.
In this case, we only want to add the Customers table. Select “Customers”, Click Add and Close.
4) Select the necessary fields. There are three methods of selecting fields:
- Drag the fields from the tables into each column
- Double click the field you want
- Click in the field that you want to set and use the pull down list to select one
When you have more than one table, be sure to select the table and the field
5) To execute this simple query, which lists the “CustomerID”, “Company” and “Title” for every customer, we click the
execute button .
MANA 120 College of Business Administration
Access Tutorial Exercise 4 MarquetteUniversity
Page 3
Though we didn’t use all of them, each of the rows in the Query Designer has a purpose:
MINI-EXERCISE
I. For every Customer, list the Company in alphabetical order, the CustomerID and the Title.
Examples:
>=10005 inside the CustomerID criteria field:
This means only customers that have a CustomerID greater or equal to 1005 will be displayed in the query.
Like Operator
You can use the Like operator to find the values in a field that match the pattern you specify
Examples:
1. Like “WA” in the State criteria field:
Only customers that live in Washington will be displayed. Note that for non-numeric fields we use double quotes
(“”).
MANA 120 College of Business Administration
Access Tutorial Exercise 4 MarquetteUniversity
Page 5
IS NULL Operator
eg.: put the IS NULL as a criteria in Company and only records with a NULL (nothing) in the company field will be
displayed.
MINI-EXERCISE
II. List only customers that are located in WA.
III. List only customers that are located in WA and whose ratings is higher than fair (Remember that we denote 0 for
No ratings, 1 for poor, 2 for fair, 3 for acceptable, 4 for good, 5 for excellent)
Close the query and name your query “simple”.
1. Follow the same steps as we did for single table queries. When you have to select the tables, you can select as many
tables as you need.
2. Add the Customers table (to get company names) and the Stocks table (to get the product description).
3. You will notice that the two tables are not linked. In order to get the product that the customer has ordered, we need to
get the orders that the customer places and from those orders, the products that were purchased. To get the orders the
customer has placed, we need the Orders table. To get the individual product ordered in that particular order, we
need the OrderDetails table. Once we add the two tables, all the tables are linked.
If you want to list only the customers that purchased products in January, type Like “1/*/96” in the Criteria row of “Date”
as follows:
Note that I did not put a Υ mark in the Show row of Date, therefore a “Date” column will not be displayed.
MINI-EXERCISE
IV. List all the companies located in Washington that purchased only Software items in the month of February.
Display the CustomerID, Company name, Product Description.
You can close the query and save it as complex.