1
Computer Science & Engineering 2111
Computer Science & Engineering 2111
Lecture 11Querying a Database
2
What is a Database Query?• A request for information from a database
• To extract information from the Database you must use a Query which is a “question” or “request”– Criterion
• An expression that tells the DBMS which records to retrieve• Make up of conditions
– Can be one conditions or many
• When you run the query a dynaset, or subset of the database is displayed.
• You can make changes to this dynaset and the changes will be reflected in your database, because the dynaset is just a view of your database.
3
• SQL is the language you use to talk to the database
• MS Access 2010 supplies a graphical user interface (GUI) called the Query By Example Grid or (QBE) grid
• MS Access 2010 creates the SQL for you.
Structured Query Language (SQL)
4
Query By Example (QBE) Grid
SQL Created from QBE Grid
Dynaset c
reated
when Query
is Run
Create a list of clients by first and and last name
FIELD FirstName LastNameTABLE Client ClientSORTSHOWCRITERIAOROR
5
The resulting dynaset:
What happens if we later change the last name of John Smith to Jones on the Client Table and re-run the query?
6
7
Write a query to list the First Name and Last Name and state for all clients who live in Ohio.
Field FirstName LastName State
Table Client Client Client
Sort
Show x x x
Criteria “oh”
OR
OR
Query Name: Ohio Residents Tables Required: ClientForeign Keys: None Join Type: None
When typing in any non-numeric criteria, always surround the criteria with quotes.
The data table
The resulting dynaset
8
Wild cards in CriteriaAn asterisk * replaces any number of characters
• Used with the keyword, Like– 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
9
10
Write a query to list the First Name and Last Name for all clients who live within the 614 area code.
Field FirstName LastName HomePhone
Table Client Client Client
Sort
Show x x
Criteria Like “614*”
OR
OR
Query Name: Phone#614 Tables Required: ClientForeign Keys: None Join Type: None
The resulting dynaset
11
The data table
12
Write a query to list the First Name and Last Name of all clients who DO NOT live within the 614 area code.
Field FirstName LastName HomePhone
Table Client Client Client
Sort
Show x x
Criteria Not Like “614*”
OR
OR
Query Name: NotPhone#614 Tables Required: ClientForeign Keys: None Join Type: None
The resulting dynaset
13
The data table
Relational operators in a query = , <= , >= , <> , < , >
• With Numbers<= 10 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.
14
15
Write a query to list the First Name, Last Name, amount and payment date for all clients who paid $200 or more.
Field FirstName LastName Amount PaymentDate
Table Client Client Payments Payments
Sort
Show x x x
Criteria > 200
OR
OR
Query Name: GreaterThanExample Tables Required: Client/PaymentsForeign Keys: ClientID Join Type: Inner
The resulting dynasetDynaset only includes records that have matching keys on both tables. This is called an inner join which is the default join type in Access
16
The data tables
For multiple conditions in the same field use Boolean Operators - AND, OR, NOT
• “BE or “BA”• >5 AND <10• NOT “BE”
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 a criteria is on a separate line it is automatically considered an OR
17
18
Write a query to list the First Name, Last Name, and amount for all clients who paid $250 or more or made payments of less than $75.
Query Name:
Example1 TablesRequired:
Client/Payments
Foreign Keys:
ClientID Join Type: Inner
Field FirstName LastName Amount
Table Client Client Payments
Sort
Show x x
Criteria >= 250
OR < 75
OR
Query Name:
Example2 TablesRequired:
Client/Payments
Foreign Keys:
ClientID Join Type: Inner
Field FirstName LastName Amount
Table Client Client Payments
Sort
Show x x
Criteria >= 250 or < 75
OR
OR
The resulting dynaset
Dynaset only includes records that have matching keys on both tables. This is called an inner join which is the default join type in Access
19
The data tables
20
Write a query to list the First Name, Last Name, amount and payment date for all clients who paid more than $100 on or after 3/3/2008.
Field FirstName LastName Amount PaymentDate
Table Client Client Payments Payments
Sort
Show x x x x
Criteria > 100 >= #3/3/2008#
OR
OR
Query Name: AndExample Tables Required: Client/PaymentsForeign Keys: ClientID Join Type: Inner
The resulting dynaset
21
The data tables(partial view)
22
Write a query to list the First Name and Last Name of all clients who made payments between 1/1/2008 and 3/8/2008.
Field FirstName LastName PaymentDate
Table Client Client Payments
Sort
Show x x x
Criteria Between #1/1/2008# And #3/8/2008#
OR
OR
Query Name: BetweenExample Tables Required: Client/PaymentsForeign Keys: ClientID Join Type: Inner
The resulting dynaset
23
The data tables