You are on page 1of 74

Query-By-Example (QBE)

in MS Access

Presented By: Engr. Lizel Rose Q. Natividad


Queries in MS Access

Concepts of Database Management 2


Query-by-Example (QBE)

• Query is simply a question represented in a way


the DBMS can recognize and process
• Without queries, a database is nothing more than a
collection of tables.
• Query-By-Example (QBE)
– Visual approach to writing queries
– Users ask their questions using an on-screen grid
– Data appears on the screen in tabular form

Concepts of Database Management 3


Query-by-Example (QBE)

• Query window in Access has two panes:

– Upper pane
– Lower pane

Concepts of Database Management 4


Query-by-Example (QBE)

– Upper pane
It contains a field list for each
table you want to query

Concepts of Database Management 5


Query-by-Example (QBE)
– Lower pane
It contains the design grid,
where you specify:
• Format of output
• Fields to be included in the
query results
• Sort order for query results
• Any criteria the records must
satisfy

Concepts of Database Management 6


Simple Queries

• To include a field in an Access query, double-click


the field in the field list to place it in the design grid
• Clicking Run button in Results group on the Query
Tools Design tab runs query and displays query
results
• Add all fields from a table to the design grid by
double-clicking the asterisk in the table’s field list

Concepts of Database Management 7


Simple Queries

• Example 1: List the number, name,


balance, and credit limit of all customers
in the database.
• Example 2: List all fields and all rows in
the Orders table.

Concepts of Database Management 8


Simple Queries (continued)

FIGURE 2-3: Fields added to the design grid


Concepts of Database Management 9
Simple Queries (continued)

FIGURE 2-4: Query results

Concepts of Database Management 10


Simple Criteria

• Criteria: conditions that data must satisfy


• Criterion: single condition that data must
satisfy
• To enter a criterion for a field:
– Include field in the design grid
– Enter criterion in Criteria row for that field

Concepts of Database Management 11


Simple Criteria (continued)

• Comparison operator
– Also called a relational operator
– Used to find something other than an exact match
= (equal to)
> (greater than)
< (less than)
>= (greater than or equal to)
<= (less than or equal to)
< > or NOT (not equal to)

Concepts of Database Management 12


Simple Criteria

• Example 3: Find the name of customer


148.
• Example 3A: List the description, on
hand, and warehouse number for all parts
that have more than 25 units on hand.

Concepts of Database Management 13


Simple Criteria

• Example 3B: Find the number and name


of each customer located in the city of
Grove.
• Example 3C: List the descriptions of all
parts that are not in warehouse 3.

Concepts of Database Management 14


Compound Criteria

• You form a compound criteria/condition


by connecting two or more simple
conditions with the AND & OR operators.

Concepts of Database Management 15


Using Compound Conditions

• AND operator
- All simple conditions must be true in order for the
compound condition to be true.
• OR operator
- The compound condition will be true whenever
anyone of the simple condition is true.

Concepts of Database Management 16


Compound Criteria

• To create an AND criterion in QBE:


– Place the criteria for multiple fields on the same
Criteria row in the design grid

Concepts of Database Management 17


Compound Criteria

• To create an OR criterion in QBE:


– Place the criteria for multiple fields on or Criteria
rows in the design grid

Concepts of Database Management 18


Compound Criteria (continued)

FIGURE 2-9: Query that uses an AND criterion

Concepts of Database Management 19


Compound Criteria (continued)

FIGURE 2-11: Query that uses an OR criterion

Concepts of Database Management 20


Compound Condition

• Example 4: List the description for all


parts that have more than 10 units on-
hand and that are located in warehouse
3.

Concepts of Database Management 21


Compound Condition

• Example 5: List the description for all


parts that have more than 10 units on
hand or that are located in warehouse 3.
• Example 6: List the number, name and
balance for each customer whose
balance is between $1,000 and $5,000.

Concepts of Database Management 22


Computed Fields

• A computed field does not exist in


database but can be computed using the
data in the existing columns.
• Computations can involve any arithmetic
operator.
• Arithmetic Operators such as Addition (+),
Subtraction (-), Multiplication (*) and
Division (/)

Concepts of Database Management 23


Computed Fields
• To include a computed field in a query:
– Enter a name for the computed field, followed by a
colon, followed by an expression in one of the
columns in the Field row
• Alternative method
– Right-click the column in the Field row, and then
select the Build option to open the Expression
Builder dialog box
– Type the expression in the dialog box

Concepts of Database Management 24


Computed Fields (continued)

FIGURE 2-15: Query that uses a computed field


Concepts of Database Management 25
Computed Fields

• Example 7: List the customer


number, name, and available credit
for all customers.

• AvailableCredit: CreditLimit - Balance

Concepts of Database Management 26


Computed Fields

• Example 7A: Find the customer


number, name, and available credit
for each customer with at least
$5,000 of available credit.

Concepts of Database Management 27


Computed Fields

• Example 7B: Find the part number,


description, and OnHand Price for
each part in item class HW.

• OnHandPrice: OnHand * Price

Concepts of Database Management 28


Functions
• Are built-in formulas in Access
• Sometimes called aggregate functions in Access.
These functions apply to group of rows.

• Count
• Sum
• Avg (average)
• Max (largest value)
• Min (smallest value)

Concepts of Database Management 29


Functions
• Use the symbol in the Design tab.

• Then the Total option will be appeared on the Design


Grid.

Concepts of Database Management 30


MySQL Aggregate Functions
FUNCTIONS DESCRIPTION
AVG Calculates the average value
in a column
COUNT Determines the number of
rows in a table
MAX Determine the maximum value
in a column
MIN Determine the minimum value
in a column
SUM Calculates a total of the values
in a column

Concepts of Database Management 31


Functions
• Note: You can create your own column name by
preceding the desired column name and a colon in
a query design.
• For example, NumberofCustomers:
CustomerNum changes the default name to
“NumberofCustomers”

Concepts of Database Management 32


Functions (continued)

FIGURE 2-17: Query to count records


Concepts of Database Management 33
Functions (continued)

FIGURE 2-18: Query results

Concepts of Database Management 34


Using the COUNT Function

• Example 8: How many customer does


sales rep 35 represent?

Concepts of Database Management 35


Using the COUNT Function

• Example 8A: How many parts are in item


class HW?

Concepts of Database Management 36


Using the COUNT and SUM Function
Example 8B: Find the total number of
Premiere Products customers and the total
of their balances. Change the field name to
CountofCustomer and TotalBalance.

Concepts of Database Management 37


Using Sum, AVG, MAX and MIN
Functions
• Example 8C: Find the total balances of
the Premiere Products customer, as well
as the average balance, minimum and
maximum value of their balances.

Concepts of Database Management 38


Functions

• Example 9: What is the average and


total credit limit of all customers of
sales rep 65? Also, get the largest
and smallest credit limit of the
customers.

Concepts of Database Management 39


Functions
• Example 9: What is the average and
total credit limit of all customers of
sales rep 65? Also, get the largest
and smallest credit limit of the
customers.

Concepts of Database Management 40


Grouping

• Grouping is creating groups of records that share


some common characteristic.
• To group records in Access:
– Select Group By operator in the Total row for the
field on which to group

Concepts of Database Management 41


Grouping (continued)

FIGURE 2-21: Query to group records

Concepts of Database Management 42


Grouping
• Example 10: What is the average
balance for all customers of each
sales rep? Also, count the number of
customers of each sales rep.

Concepts of Database Management 43


Grouping
• Example 10: What is the average
balance for all customers of each
sales rep? Also, count the number of
customers of each sales rep.

Concepts of Database Management 44


Grouping (cont.)

• Example 10A: What is the average,


and total balance, max and min of
credit limit for all customers of each
sales rep in the Premiere Products
database?

Concepts of Database Management 45


Grouping

• Example 10B: For each parts, list


the warehouse and the total units on
hand in each warehouse.

Concepts of Database Management 46


Sorting
• Sorting: listing records in query results in an
ordered way
• Sort key: field on which records are sorted
• Major sort key
– Also called the primary sort key
– First sort field, when sorting records by more than
one field
• Minor sort key
– Also called the secondary sort key
– Second sort field, when sorting records by more than
one field
Concepts of Database Management 47
Sorting (continued)

FIGURE 2-23: Query to sort records

Concepts of Database Management 48


Sorting on Multiple Keys

• Specifying more than one sort key in a query


• Major (primary) sort key
– Sort key on the left in the design grid
• Minor (secondary) sort key
– Sort key on the right in the design grid

Concepts of Database Management 49


Sorting on Multiple Keys (continued)

FIGURE 2-27: Correct query design to sort by RepNum and then by


CustomerName
Concepts of Database Management 50
Sorting

• Example 11: List the customer


name, balance and rep number for
each customer. Sort the output
alphabetically by

Concepts of Database Management 51


Sorting using Multiple Sort Keys

• Example 12: List the customer


name, balance and rep number for
each customer. Sort the output by
sales rep number. Within the
customers of each sales rep, sort the
output alphabetically by customer
name.

Concepts of Database Management 52


Sorting using Multiple Sort Keys

Concepts of Database Management 53


Joining Tables

• Queries to select data from more than one table


• Join the tables based on matching fields in
corresponding columns
• Join line
– Line drawn by Access between matching fields in
the two tables
– Indicates that the tables are related

Concepts of Database Management 54


Joining Tables (continued)

FIGURE 2-29: Query design to join two tables

Concepts of Database Management 55


Joining Multiple Tables

• Joining three or more tables is similar to joining two


tables
• To join three or more tables:
– Add the field lists for all tables in the join to upper
pane
– Add the fields to appear in query results to design
grid in the desired order

Concepts of Database Management 56


Joining Tables

• Example 13: List the customer


number, customer name, rep
number, last name and first name of
each customer’s representative.

Concepts of Database Management 57


Joining Tables

• Example 14: For each customer


whose credit limit is $10,000 list the
customer number, customer name,
rep number, last name and first
name of each customer’s
representative.

Concepts of Database Management 58


Joining Tables

• Example 15: For each order, list the


order number, order date, customer
number, customer name. In addition,
list the part number, description,
number of units ordered and quoted
price. Sort the output alphabetically
by customer name.

Concepts of Database Management 59


Using an Update Query

• Update query: a query that changes data


– Makes a specified change to all records satisfying
the criteria in the query
• To change a query to an update query:
– Click Update button in the Query Type group on the
Query Tools Design tab

Concepts of Database Management 60


Using an Update Query

• Update To row is added when an update query is


created
– Used to indicate how to update data selected by the
query
– Don’t forget to click RUN to perform the action
specified by a query.

Concepts of Database Management 61


Using an Update Query (continued)

FIGURE 2-35: Query design to update data

Concepts of Database Management 62


Using an Update Query
• Example 16: The zip code for customer located
in the city of Fillmore is incorrect. It should be
33363. Change the zip code for these
customers to the correct value.

Don’t forget to click RUN to perform the action


specified by a query.
63
Using a Delete Query
• Delete query: permanently deletes all records
satisfying the criteria entered in the query
• To change query type to a delete query:
– Click Delete button in the Query Type group on the Query
Tools Design tab
• Delete row is added
– Indicates this is a delete query

Concepts of Database Management 64


Using a Delete Query (continued)

FIGURE 2-36: Query design to delete records

Concepts of Database Management 65


Using an Delete Query

• Example 17: Delete all order line in


which the order number is 21610.

Don’t forget to click RUN to perform the action


specified by a query.
66
Using a Make-Table Query

• Make-table query: creates a new table


using results of a query
• Records added to new table are separate
from the original table

Concepts of Database Management 67


Using a Make-Table Query

• To change the query type to a make-table query:


– Click Make Table button in the Query Type group on
the Query Tools Design tab
– In Make Table dialog box, enter the new table’s
name and choose where to create it

Concepts of Database Management 68


Using a Make-Table Query (continued)

FIGURE 2-38: Make Table dialog box


Concepts of Database Management 69
Using an Make Table Query

• Example 18: Create a new table


containing the customer number and
customer name, and rep number,
last name and first name of the
representative. Name the new table
as CustomerRep.

Concepts of Database Management 70


Summary

• Relation: two-dimensional table in which the entries


are single-valued, each field has a distinct name,
all values in a field are values of the same attribute,
order of fields is immaterial, each row is distinct,
and order of rows is immaterial
• Relational database: collection of relations
• A table’s primary key is the field or fields that
uniquely identify a given row within the table
• Query-By-Example (QBE) is a visual tool for
manipulating relational databases

Concepts of Database Management 71


Summary (continued)

• To indicate AND criteria in an Access query, place


both criteria in the same Criteria row of the design
grid; to indicate OR criteria, place criteria on
separate Criteria rows of the design grid
• To create a computed field in Access, enter
expression in the desired column of design grid
• To use functions to perform calculations in Access,
include the appropriate function in the Total row
• To sort query results in Access, select Ascending
or Descending in Sort row for the field or fields that
are sort keys
Concepts of Database Management 72
Summary (continued)

• To join tables in Access, place field lists for both


tables in upper pane of Query window
• To make the same change to all records that
satisfy certain criteria, use an update query
• To delete all records that satisfy certain criteria,
use a delete query
• To save the results of a query as a table, use a
make-table query

Concepts of Database Management 73


Thank you! ☺

74

You might also like