Top Banner
By Wanda Gibson, AI, MMI, SME
50

By Wanda Gibson, AI, MMI, SME. Querying a Database A query is a question written specifically so Access will understand Users run a query to obtain.

Dec 17, 2015

Download

Documents

Erin Anderson
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: By Wanda Gibson, AI, MMI, SME. Querying a Database  A query is a question written specifically so Access will understand  Users run a query to obtain.

By

Wanda Gibson, AI, MMI, SME

Page 2: By Wanda Gibson, AI, MMI, SME. Querying a Database  A query is a question written specifically so Access will understand  Users run a query to obtain.

Querying a Database

A query is a question written specifically so Access will understand

Users run a query to obtain an answer

Specific rules must be followed when creating a query

If no results are given, a user asked the question incorrectly

Page 3: By Wanda Gibson, AI, MMI, SME. Querying a Database  A query is a question written specifically so Access will understand  Users run a query to obtain.

Ways to create a Query

Page 4: By Wanda Gibson, AI, MMI, SME. Querying a Database  A query is a question written specifically so Access will understand  Users run a query to obtain.

Clicking the “New” button on the Query Object Bar takes a user to the “New Query” Box. A user can then choose the type of query he or she wants from the list.

New Button

Page 5: By Wanda Gibson, AI, MMI, SME. Querying a Database  A query is a question written specifically so Access will understand  Users run a query to obtain.

In the Simple Query Wizard window, users choose the Table and the Fields needed for the Query.

Page 6: By Wanda Gibson, AI, MMI, SME. Querying a Database  A query is a question written specifically so Access will understand  Users run a query to obtain.

Users should only choose the fields necessary for the Query.

Include Stock Number, Description, Inventory, and Selling Price for all records.

Page 7: By Wanda Gibson, AI, MMI, SME. Querying a Database  A query is a question written specifically so Access will understand  Users run a query to obtain.

Click Finish

After all fields are selected, click the “Next” button and finally “Finish” to complete the Wizard.

Page 8: By Wanda Gibson, AI, MMI, SME. Querying a Database  A query is a question written specifically so Access will understand  Users run a query to obtain.

Stock Number Description Inventory Selling Price

06543677 Puppy Puzzle 16 $12.50

06543678 Kittens Puzzle 5 $12.50

07835463 Barnyard Buddies 22 $65.00

07890987 Pets Puzzle 10 $14.00

07890990 Zoo Puzzle 12 $15.00

87324568 Building Blocks 15 $27.95

87543197 Blocks in a Box 22 $62.50

87543199 Blocks Set 15 $21.00

87543365 Log Blocks 7 $75.00

87548976 Colored Shape Blocks 9 $87.50

97863545 Box of Blocks 20 $65.00

98763245 Sports Car 22 $40.00

98764547 Police Car 4 $18.50

98764556 Dump Truck 8 $15.00

98764567 Fire Truck 17 $15.00

Simple Query Wizard Results

Page 9: By Wanda Gibson, AI, MMI, SME. Querying a Database  A query is a question written specifically so Access will understand  Users run a query to obtain.

In the Select Query Window, choose the Toys Table from the Show Table box.

Page 10: By Wanda Gibson, AI, MMI, SME. Querying a Database  A query is a question written specifically so Access will understand  Users run a query to obtain.

Include the Stock Number, Description, Inventory, and Selling Price for all records.

Run the Query.

Run Button

Page 11: By Wanda Gibson, AI, MMI, SME. Querying a Database  A query is a question written specifically so Access will understand  Users run a query to obtain.

Stock Number Description Inventory Selling Price

06543677 Puppy Puzzle 16 $12.50

06543678 Kittens Puzzle 5 $12.50

07835463 Barnyard Buddies 22 $65.00

07890987 Pets Puzzle 10 $14.00

07890990 Zoo Puzzle 12 $15.00

87324568 Building Blocks 15 $27.95

87543197 Blocks in a Box 22 $62.50

87543199 Blocks Set 15 $21.00

87543365 Log Blocks 7 $75.00

87548976 Colored Shape Blocks 9 $87.50

97863545 Box of Blocks 20 $65.00

98763245 Sports Car 22 $40.00

98764547 Police Car 4 $18.50

98764556 Dump Truck 8 $15.00

98764567 Fire Truck 17 $15.00

The results of this query are the same as the results the Simple Query Wizard produced.

Page 12: By Wanda Gibson, AI, MMI, SME. Querying a Database  A query is a question written specifically so Access will understand  Users run a query to obtain.

All records are requested when a user chooses the * from the table list.

Page 13: By Wanda Gibson, AI, MMI, SME. Querying a Database  A query is a question written specifically so Access will understand  Users run a query to obtain.

Either drag field names to the field line of the design grid or double click the field

names to add them to the grid. Selecting individual field names will produce the

same results as using the *.

This is another way to select all the fields in the table for the query.

Page 14: By Wanda Gibson, AI, MMI, SME. Querying a Database  A query is a question written specifically so Access will understand  Users run a query to obtain.

Stock Number Description Inventory Cost Selling Price Vendor ID

06543677 Puppy Puzzle 16 $9.00 $12.50 WT36

06543678 Kittens Puzzle 5 $9.00 $12.50 WT36

07835463 Barnyard Buddies 22 $45.00 $65.00 HT19

07890987 Pets Puzzle 10 $8.50 $14.00 HT19

07890990 Zoo Puzzle 12 $9.00 $15.00 HT19

87324568 Building Blocks 15 $15.95 $27.95 TT96

87543197 Blocks in a Box 22 $52.00 $62.50 TT96

87543199 Blocks Set 15 $17.00 $21.00 TT96

87543365 Log Blocks 7 $59.00 $75.00 TT96

87548976 Colored Shape Blocks 9 $64.00 $87.50 TT96

97863545 Box of Blocks 20 $55.00 $65.00 GT36

98763245 Sports Car 22 $25.00 $40.00 GT36

98764547 Police Car 4 $10.00 $18.50 GT36

98764556 Dump Truck 8 $10.00 $15.00 GT36

98764567 Fire Truck 17 $10.00 $15.00 GT36

This exact table, with all records requested, will be produced if a user chooses the * or if a user chooses the individual field names.

Page 15: By Wanda Gibson, AI, MMI, SME. Querying a Database  A query is a question written specifically so Access will understand  Users run a query to obtain.

Display the Stock Number, Description, Cost, and Vendor ID for all products whose Vendor ID is TT96. Run.

Run Button

Page 16: By Wanda Gibson, AI, MMI, SME. Querying a Database  A query is a question written specifically so Access will understand  Users run a query to obtain.

Stock Number Description Cost Vendor ID

87324568 Building Blocks $15.95 TT96

87543197 Blocks in a Box $52.00 TT96

87543199 Blocks Set $17.00 TT96

87543365 Log Blocks $59.00 TT96

87548976 Colored Shape Blocks $64.00 TT96

Vendor ID TT96

The query was – Display the Stock Number, Description, Cost and Vendor ID for all products whose Vendor ID is TT96.

Page 17: By Wanda Gibson, AI, MMI, SME. Querying a Database  A query is a question written specifically so Access will understand  Users run a query to obtain.

WildcardsAccess has two wildcards.

Question Mark [?] -- Is used for a specific number of characters. For instance, A23 would appear A??. Each character needs a ?. Verify placement of the ? in the following ways.

If there is a letter or number before the text a user is including in the criteria, the user must place a ? before the text.

3 Malone would be ?Malone

If there is a letter or number after, the user must place the ? after the text.

Malone E would be Malone?

If there is a letter or number before and after, the user will need a ? before and after the text.

3 Malone E would be ?Malone?

Page 18: By Wanda Gibson, AI, MMI, SME. Querying a Database  A query is a question written specifically so Access will understand  Users run a query to obtain.

Asterisk [*] -- Is for any number of characters. Verify placement of the * in the following ways.

If there are letters or numbers before the text a user is including in the criteria, the user must place an * before the text.

365 Malone would be *Malone

If there are letters or numbers after, the user must place the * after the text.

Malone Avenue would be Malone*

If there are letters or numbers before and after, the user will need an * before and after the text.

365 Malone Avenue would be *Malone*

Page 19: By Wanda Gibson, AI, MMI, SME. Querying a Database  A query is a question written specifically so Access will understand  Users run a query to obtain.

Display the Stock Number and Description for all items where the description includes the letters Block.

The * is used before Block and after Block because of the placement of the letters in the description of the toys.

Page 20: By Wanda Gibson, AI, MMI, SME. Querying a Database  A query is a question written specifically so Access will understand  Users run a query to obtain.

Stock Number Description

87324568 Building Blocks

97863545 Box of Blocks

87543197 Blocks in a Box

87543199 Blocks Set

87543365 Log Blocks

87548976 Colored Shape Blocks

Display the Stock Number and Description for all items where the description includes the letters Block.

Page 21: By Wanda Gibson, AI, MMI, SME. Querying a Database  A query is a question written specifically so Access will understand  Users run a query to obtain.

Cost does not show in result.

Display the Stock Number, Description, and Vendor ID for all items with a Cost less than $15. Run.

Take out of Show Box so field will not

show in result

Page 22: By Wanda Gibson, AI, MMI, SME. Querying a Database  A query is a question written specifically so Access will understand  Users run a query to obtain.

Stock Number Description Vendor ID

07890987 Pets Puzzle HT19

98764567 Fire Truck GT36

06543678 Kittens Puzzle WT36

06543677 Puppy Puzzle WT36

07890990 Zoo Puzzle HT19

98764556 Dump Truck GT36

98764547 Police Car GT36

Since the Show Box was removed from the Cost field, it is not part of the result.

Page 23: By Wanda Gibson, AI, MMI, SME. Querying a Database  A query is a question written specifically so Access will understand  Users run a query to obtain.

Display the Stock Number and Description for all products that have a Selling Price greater than $30. Run.

Again, a criteria field is not included in the result.

Page 24: By Wanda Gibson, AI, MMI, SME. Querying a Database  A query is a question written specifically so Access will understand  Users run a query to obtain.

Stock Number Description

97863545 Box of Blocks

07835463 Barnyard Buddies

87543197 Blocks in a Box

98763245 Sports Car

87543365 Log Blocks

87548976 Colored Shape Blocks

$65.00

$65.00

$62.50

$40.00

$75.00

$87.50

All Selling Prices are greater than $30.

Page 25: By Wanda Gibson, AI, MMI, SME. Querying a Database  A query is a question written specifically so Access will understand  Users run a query to obtain.

Display all fields for those items with a Selling Price greater than $10 AND the Inventory count is at least 10.

AND Criteria

Page 26: By Wanda Gibson, AI, MMI, SME. Querying a Database  A query is a question written specifically so Access will understand  Users run a query to obtain.

Stock Number Description Inventory Cost Selling Price Vendor ID

87324568 Building Blocks 15 $15.95 $27.95 TT96

97863545 Box of Blocks 20 $55.00 $65.00 GT36

07890987 Pets Puzzle 10 $8.50 $14.00 HT19

98764567 Fire Truck 17 $10.00 $15.00 GT36

07835463 Barnyard Buddies 22 $45.00 $65.00 HT19

06543677 Puppy Puzzle 16 $9.00 $12.50 WT36

07890990 Zoo Puzzle 12 $9.00 $15.00 HT19

87543197 Blocks in a Box 22 $52.00 $62.50 TT96

87543199 Blocks Set 15 $17.00 $21.00 TT96

98763245 Sports Car 22 $25.00 $40.00 GT36

Selling Price is greater than $10Inventory count is at least

10AND

The AND means both criteria must be met.

Page 27: By Wanda Gibson, AI, MMI, SME. Querying a Database  A query is a question written specifically so Access will understand  Users run a query to obtain.

OR criteria

Display all fields for toys with a Vendor ID of GT36 OR a Selling Price greater than $30.

Page 28: By Wanda Gibson, AI, MMI, SME. Querying a Database  A query is a question written specifically so Access will understand  Users run a query to obtain.

Stock Number Description Inventory Cost Selling Price Vendor ID

97863545 Box of Blocks 20 $55.00 $65.00 GT36

98764567 Fire Truck 17 $10.00 $15.00 GT36

07835463 Barnyard Buddies 22 $45.00 $65.00 HT19

87543197 Blocks in a Box 22 $52.00 $62.50 TT96

98764556 Dump Truck 8 $10.00 $15.00 GT36

98764547 Police Car 4 $10.00 $18.50 GT36

98763245 Sports Car 22 $25.00 $40.00 GT36

87543365 Log Blocks 7 $59.00 $75.00 TT96

87548976 Colored Shape Blocks 9 $64.00 $87.50 TT96

OR

Selling Price is more than $30.

Vendor ID is GT36.

At least one of the two criteria must be met, but both criteria do not have to be met together.

Page 29: By Wanda Gibson, AI, MMI, SME. Querying a Database  A query is a question written specifically so Access will understand  Users run a query to obtain.

Calculated Field

A Calculated Field is a Field that is added to the Query where a calculation can be made.

Any Fields from the Table used in a Calculated Field must be included in square brackets [].

Page 30: By Wanda Gibson, AI, MMI, SME. Querying a Database  A query is a question written specifically so Access will understand  Users run a query to obtain.

New Field Name

Fields from Table

SeparatorMath Function

New Field Name

Field Name : [Field] *[Field]

In this calculated field, two field names from the table are used.

Page 31: By Wanda Gibson, AI, MMI, SME. Querying a Database  A query is a question written specifically so Access will understand  Users run a query to obtain.

New Field Name

Field from Table

SeparatorMath Function

Calculation

Field Name : [Field] * .10

In this calculated field, only one field from the table is used.

Page 32: By Wanda Gibson, AI, MMI, SME. Querying a Database  A query is a question written specifically so Access will understand  Users run a query to obtain.

Include the Stock Number and Description in the design grid. Calculate the inventory value (inventory * cost) for all records.

In the first blank grid section,

Right Click and choose

Zoom

Page 33: By Wanda Gibson, AI, MMI, SME. Querying a Database  A query is a question written specifically so Access will understand  Users run a query to obtain.

Include the Stock Number and Description in the design grid. Calculate the inventory value (inventory * cost) for all records.

Page 34: By Wanda Gibson, AI, MMI, SME. Querying a Database  A query is a question written specifically so Access will understand  Users run a query to obtain.

Stock Number Description Inventory value

06543677 Puppy Puzzle $144.00

06543678 Kittens Puzzle $45.00

07835463 Barnyard Buddies $990.00

07890987 Pets Puzzle $85.00

07890990 Zoo Puzzle $108.00

87324568 Building Blocks $239.25

87543197 Blocks in a Box $1,144.00

87543199 Blocks Set $255.00

87543365 Log Blocks $413.00

87548976 Colored Shape Blocks $576.00

97863545 Box of Blocks $1,100.00

98763245 Sports Car $550.00

98764547 Police Car $40.00

98764556 Dump Truck $80.00

98764567 Fire Truck $170.00

This result shows the Inventory value (inventory * cost) for all records.

Page 35: By Wanda Gibson, AI, MMI, SME. Querying a Database  A query is a question written specifically so Access will understand  Users run a query to obtain.

Joining Tables Joining Tables refers to one Field listed in 2 or

more tables that shows the same information (Vendor ID).

This Field is usually the Primary Key in one of the Tables.

This Field must have exactly the same Structure (Name, Length, Data Type) and the same data.

A Join Line connects the Tables on the Field Names that are the same. By Joining Tables, a user is able to use Fields from both Tables in Queries.

Page 36: By Wanda Gibson, AI, MMI, SME. Querying a Database  A query is a question written specifically so Access will understand  Users run a query to obtain.

Add Vendors Table to design grid

Show Table

Page 37: By Wanda Gibson, AI, MMI, SME. Querying a Database  A query is a question written specifically so Access will understand  Users run a query to obtain.

These Tables are Joined, and both are used in this query.

Join Line

Page 38: By Wanda Gibson, AI, MMI, SME. Querying a Database  A query is a question written specifically so Access will understand  Users run a query to obtain.

Fields from both tables are used.

Display the Stock Number, Description, and Cost from the Toys Table. Display Vendor Name,

Phone Number, and Sales Rep from the Vendors Table.

Page 39: By Wanda Gibson, AI, MMI, SME. Querying a Database  A query is a question written specifically so Access will understand  Users run a query to obtain.

Stock Number

Description Cost Vendor NamePhone

NumberSales Rep

97863545 Box of Blocks $55.00

Gambardella Toys

756-491-4689 Kim Bowen

98764567 Fire Truck $10.00

Gambardella Toys

756-491-4689 Kim Bowen

98764556 Dump Truck $10.00

Gambardella Toys

756-491-4689 Kim Bowen

98764547 Police Car $10.00

Gambardella Toys

756-491-4689 Kim Bowen

98763245 Sports Car $25.00

Gambardella Toys

756-491-4689 Kim Bowen

07890987 Pets Puzzle $8.50 Hooper Toys 306-954-4681 Pat Kirtland

07835463 Barnyard Buddies $45.00

Hooper Toys 306-954-4681 Pat Kirtland

07890990 Zoo Puzzle $9.00 Hooper Toys 306-954-4681 Pat Kirtland

87324568 Building Blocks $15.95

Tailspin Toys 404-561-8704 Stephanie Hooper

87543197 Blocks in a Box $52.00

Tailspin Toys 404-561-8704 Stephanie Hooper

87543199 Blocks Set $17.00

Tailspin Toys 404-561-8704 Stephanie Hooper

87543365 Log Blocks $59.00

Tailspin Toys 404-561-8704 Stephanie Hooper

87548976 Colored Shape Blocks

$64.00

Tailspin Toys 404-561-8704 Stephanie Hooper

06543678 Kittens Puzzle $9.00 Wingtip Toys, Inc.

945-468-9521 Matt Gambardella

06543677 Puppy Puzzle $9.00 Wingtip Toys, Inc.

945-468-9521 Matt Gambardella

The result of using fields from both tables is shown here.

Page 40: By Wanda Gibson, AI, MMI, SME. Querying a Database  A query is a question written specifically so Access will understand  Users run a query to obtain.

Inventory was used in the criteria, but it is not

included in the results.

Using the previous query, restrict retrieval to those whose Inventory is less than or equal to 10.

Page 41: By Wanda Gibson, AI, MMI, SME. Querying a Database  A query is a question written specifically so Access will understand  Users run a query to obtain.

Stock Number

Description Cost Vendor NamePhone Number

Sales Rep

07890987 Pets Puzzle $8.50 Hooper Toys 306-954-4681

Pat Kirtland

06543678 Kittens Puzzle $9.00 Wingtip Toys, Inc.

945-468-9521

Matt Gambardella

98764556 Dump Truck $10.00 Gambardella Toys

756-491-4689

Kim Bowen

98764547 Police Car $10.00 Gambardella Toys

756-491-4689

Kim Bowen

87543365 Log Blocks $59.00 Tailspin Toys 404-561-8704

Stephanie Hooper

87548976 Colored Shape Blocks

$64.00 Tailspin Toys 404-561-8704

Stephanie Hooper

Inventory is used in the criteria, but it is not included in the results.

Toys Table Vendors Table

Page 42: By Wanda Gibson, AI, MMI, SME. Querying a Database  A query is a question written specifically so Access will understand  Users run a query to obtain.

Aggregate Functions

An aggregate function is a function that performs a mathematical calculation using a group of records in a table.

In order to use an aggregate function, a user must turn on the Total key ().

Page 43: By Wanda Gibson, AI, MMI, SME. Querying a Database  A query is a question written specifically so Access will understand  Users run a query to obtain.

Examples of Aggregate Functions

Group By -- Combines records that have identicalvalues

Sum –- Gives the total of a specified numeric fieldAvg –- Gives the average of a specified numeric fieldMin –- Gives the minimum number of a specified

numeric fieldMax –- Gives the maximum number of a specified

numeric fieldCount –- Counts the records in a specified field

Page 44: By Wanda Gibson, AI, MMI, SME. Querying a Database  A query is a question written specifically so Access will understand  Users run a query to obtain.

Aggregate Functions – Users must turn on the Total Key ().

Total is added to the Design Grid List

Select Avg from the drop down list.

Page 45: By Wanda Gibson, AI, MMI, SME. Querying a Database  A query is a question written specifically so Access will understand  Users run a query to obtain.

AvgOfSelling Price

$36.43

Average Selling Price

Page 46: By Wanda Gibson, AI, MMI, SME. Querying a Database  A query is a question written specifically so Access will understand  Users run a query to obtain.

Average Selling Price by Vendor ID

Select Avg from the drop down list.

Page 47: By Wanda Gibson, AI, MMI, SME. Querying a Database  A query is a question written specifically so Access will understand  Users run a query to obtain.

AvgOfSelling Price

$36.43

Vendor IDAvgOfSelling Price

GT36 $30.70

HT19 $31.33

TT96 $54.79

WT36 $12.50

Average Selling Price

Average Selling Price by Vendor

Page 48: By Wanda Gibson, AI, MMI, SME. Querying a Database  A query is a question written specifically so Access will understand  Users run a query to obtain.

Count records using Stock Number

Select Count from the drop down list.

Page 49: By Wanda Gibson, AI, MMI, SME. Querying a Database  A query is a question written specifically so Access will understand  Users run a query to obtain.

AvgOfSelling Price

$36.43

Vendor IDAvgOfSelling Price

GT36 $30.70

HT19 $31.33

TT96 $54.79

WT36 $12.50

Average Selling Price

Average Selling Price by Vendor

Count of Stock Numbers CountOfStock Number

15

Page 50: By Wanda Gibson, AI, MMI, SME. Querying a Database  A query is a question written specifically so Access will understand  Users run a query to obtain.

References

Microsoft product screen shots reprinted with permission from Microsoft Corporation.