By Wanda Gibson, AI, MMI, SME. Querying a Database A query is a question written specifically so...
-
Upload
erin-anderson -
Category
Documents
-
view
224 -
download
2
Transcript of By Wanda Gibson, AI, MMI, SME. Querying a Database A query is a question written specifically so...
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
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
Users should only choose the fields necessary for the Query.
Include Stock Number, Description, Inventory, and Selling Price for all records.
Click Finish
After all fields are selected, click the “Next” button and finally “Finish” to complete the Wizard.
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
Include the Stock Number, Description, Inventory, and Selling Price for all records.
Run the Query.
Run Button
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.
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.
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.
Display the Stock Number, Description, Cost, and Vendor ID for all products whose Vendor ID is TT96. Run.
Run Button
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.
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?
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*
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.
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.
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
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.
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.
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.
Display all fields for those items with a Selling Price greater than $10 AND the Inventory count is at least 10.
AND Criteria
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.
OR criteria
Display all fields for toys with a Vendor ID of GT36 OR a Selling Price greater than $30.
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.
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 [].
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.
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.
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
Include the Stock Number and Description in the design grid. Calculate the inventory value (inventory * cost) for all records.
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.
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.
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.
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.
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.
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
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 ().
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
Aggregate Functions – Users must turn on the Total Key ().
Total is added to the Design Grid List
Select Avg from the drop down list.
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
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