500 likes | 721 Views
Querying a Database. 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 an answer Specific rules must be followed when creating a query
E N D
Querying a Database 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 an answer • Specific rules must be followed when creating a query • If no results are given, a user asked the question incorrectly
New Button 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.
In the Simple Query Wizard window, users choose the Table and the Fields needed for the Query.
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.
In the Select Query Window, choose the Toys Table from the Show Table box.
Run Button Include the Stock Number, Description, Inventory, and Selling Price for all records. Run the Query.
The results of this query are the same as the results the Simple Query Wizard produced.
All records are requested when a user chooses the * from the table list.
This is another way to select all the fields in the table for the query. 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 exact table, with all records requested, will be produced if a user chooses the * or if a user chooses the individual field names.
Run Button Display the Stock Number, Description, Cost, and Vendor ID for all products whose Vendor ID is TT96. Run.
Vendor ID TT96 The query was – Display the Stock Number, Description, Cost and Vendor ID for all products whose Vendor ID is TT96.
Wildcards Access 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.
Display the Stock Number and Description for all items where the description includes the letters Block.
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 Cost does not show in result.
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.
All Selling Prices are greater than $30. $65.00 $65.00 $62.50 $40.00 $75.00 $87.50
AND Criteria Display all fields for those items with a Selling Price greater than $10 AND the Inventory count is at least 10.
Inventory count is at least 10 AND Selling Price is greater than $10 The AND means both criteria must be met.
Display all fields for toys with a Vendor ID of GT36 OR a Selling Price greater than $30. OR criteria
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. OR
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 New Field Name Separator Math Function Field Name : [Field] * [Field] Fields from Table In this calculated field, two field names from the table are used.
New Field Name Separator Math Function Field Name : [Field] * .10 Field from Table Calculation In this calculated field, only one field from the table is used.
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.
Include the Stock Number and Description in the design grid. Calculate the inventory value (inventory * cost) for all records.
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 useris able to use Fields from both Tables in Queries.
Add Vendors Table to design grid Show Table
Join Line These Tables are Joined, and both are used in this query.
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.
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.
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 identical values Sum –- Gives the total of a specified numeric field Avg –- Gives the average of a specified numeric field Min –- Gives the minimum number of a specified numeric field Max –- Gives the maximum number of a specified numeric field Count –- Counts the records in a specified field
Total is added to the Design Grid List Select Avg from the drop down list. Aggregate Functions – Users must turn on the Total Key ().
Select Avg from the drop down list. Average Selling Price by Vendor ID
Average Selling Price Average Selling Price by Vendor
Count records using Stock Number Select Count from the drop down list.
Average Selling Price Average Selling Price by Vendor Count of Stock Numbers
References Microsoft product screen shots reprinted with permission from Microsoft Corporation.