Presentation is loading. Please wait.

Presentation is loading. Please wait.

Writing Simple Queries in Access

Similar presentations


Presentation on theme: "Writing Simple Queries in Access"— Presentation transcript:

1 Writing Simple Queries in Access
Objectives: Learn how to use the Access Query Design Tool to obtain specific information by - Displaying on specific data fields Filtering data using criteria

2 Querying : The process of asking the database for specific information
A specific format is used to ask these questions The computer saves the format, not the results which can be generated upon demand (dynaset) We will study the following types of queries: Filtering data using single and multiple criteria Sorting data Grouping and summarizing data Performing calculations on the data

3 Query Writing queries involves
A query is a question about the data in the database Writing queries involves Choosing the required tables Where is your information coming from Choosing the output fields What information do you want to list Choosing the criteria fields What selection criteria do you want to specify -which records to “choose”

4 Writing a Criterion Criteria is a collection of criterion or conditions: Criterion - an expression that tells the DBMS which records to retrieve Criteria with 2 conditions: List records from category BE with more than 40 units in stock

5 Consider the following database :
Products: Suppliers: Primary Key Foreign Key

6 Create a list of products by name and the number of units in stock for each
CategoryID ProductName Unitsinstock SupplierID ProductID Price No conditions

7 The resulting dynaset:
What happens if we later change the units in stock for Mini-cookies on the Products Table an re-run the query?

8 List the products & units in stock for category BE
CategoryID ProductName Unitsinstock SupplierID ProductID Price Single condition criterion showing ProductName & Units

9 The resulting dynaset:
The data table: The resulting dynaset:

10 = , <= , >= , <> , < , >
Relational operators in a query: = , <= , >= , <> , < , > With Numbers <= values of less than or equal to 10 With Text < “G” text beginning with letters A through F > “Jones” text from Jones through end The appropriate expression is placed in the field where this data if found.

11 List the product names for products with greater than 40 units in stock
CategoryID ProductName Unitsinstock SupplierID ProductID Price You can use relational operators in your criteria

12 The resulting dynaset:
The data table: The resulting dynaset:

13 List the product names and units in stock for products from the letters M to Z
CategoryID ProductName Unitsinstock SupplierID ProductID Price You can use relational operators in your criteria

14 Wild cards in Criteria:
An asterisk * replaces any number of characters: Like “C*” – in Product Name field will select c, Cookie, cake. Like “*cookie*” - in Product Name field will select all records that include the word cookie in the Product Name field A ? replaces a single character Like “B?” – in the Category field will select BE & BA Would the criteria Like “432??” in the zipcode field select a record with the zipcode ?

15 List the product names for products that start with the letter M
CategoryID ProductName Unitsinstock SupplierID ProductID Price You can use wild cards in your criteria - just need to type m* the computer will add the syntax Like “...”

16 For multiple conditions in the same field use Boolean Operators - AND, OR, NOT
“BE” Or “BA” >5 AND <10 NOT “BE” What dynaset would result from the criteria “BE” and “BA” in the category field? No records would be selected since you cannot specify 2 mutually exclusive conditions with an And.

17 List by product the units in stock for both categories BE and BA
Products CategoryID ProductName Unitsinstock SupplierID ProductID Price The “OR” operator for a single field’s conditions

18 The resulting dynaset:
The data table: The resulting dynaset:

19 Products CategoryID ProductName Unitsinstock SupplierID ProductID Price List by product the units in stock for all products but those in category BE You can use the “not” operator for a single field’s conditions

20 List the products & units in stock for all items with at least 20 units in stock but no more than 30 units in stock Products CategoryID ProductName Unitsinstock SupplierID ProductID Price You can use the “AND” operator for a single field’s conditions

21 List the products & units in stock for all items with at least 20 units in stock but no more than 30 units in stock Products CategoryID ProductName Unitsinstock SupplierID ProductID Price An alternate syntax: >=20 and <=30 The between syntax allows you to set lower & upper limits - inclusively

22 For conditions in multiple fields the placement of your arguments determines the Boolean relationship between those arguments: If a criteria is on the same line it is automatically considered an AND If criteria is on a separate line it is automatically considered an OR

23 List product names for products in category BA with less than 30 units in stock
CategoryID ProductName Unitsinstock SupplierID ProductID Price The placement of both criteria on the same line implies an AND relationship

24 List product names for products in category BA with less than 30 units in stock
CategoryID ProductName Unitsinstock SupplierID ProductID Price The placement of both criteria on the same line implies an AND relationship

25 The resulting dynaset:
The data table: The resulting dynaset:

26 Products CategoryID ProductName Unitsinstock SupplierID ProductID Price List product names for products in category BA with less than 30 units in stock or category BE with more than 31 units in stock Criterion on same line implies AND, Criterion on a separate line implies OR

27 How does the computer store your query?
Access does not store the resulting table from your query - it simply stores the instructions to run the query The actual SQL instructions can be seen in the VIEW menu This is “memory efficient” since it does not require you to save all the information each time you want a different “view” of it. It also insures that each time you use a query “view” you get the most current version.

28 Summarizing Queries A query is a set of instructions that specifies how to extract and organize data from a database Access provides the QBE grid as a GUI interface to create the SQL code. To properly use the QBE grid you must select the required data (table(s), query), the fields you wish to display and then specify the desired criterion, sorts, groupings and/or expressions.


Download ppt "Writing Simple Queries in Access"

Similar presentations


Ads by Google