Queries That Select Records
(Chapter 6 – 7)
Query Basics
•
A Microsoft Access query is a question about the information stored in Access
tables.
•
Your query can be a simple question about data in a single table, or it can be a
more complex question about information stored in several tables.
•
After run query, Microsoft Access returns only the information you requested.
Creating Queries in Design View
•
The best starting point for query creation is the Design view.
–
–
Choose
Select
Create Queries Query Design.
the table that has
the data you want, and then click Add Click Close.
Creating Queries in Design View
–
–
–
Select the fields you want to include in query.
Arrange the fields from left to right in the order to appear in the query results.
If you want to hide one or more columns, then clear the Show checkbox
columns.
for those
Creating Queries in Design View
–
–
Choose
–
Choose
a sort
order.
Set your filtering criteria by place the expression into the Criteria box for the appropriate
field.
Query Tools Design Results Run. Save the query.
Getting the top records
•
Use the Top Values box on the Design View toolbar to see the top records
produced by the query.
–
–
–
–
–
Open query in Design view.
Sort table so that the records you’re most interested in are at
In the Query Tools Design
Query Setup In return box,
choose a different option
the top.
Creating Queries by Wizard
•
•
Create a simple Query by using wizard.
The Query wizard works by asking you a series of questions.
–
Choose
Create Queries Query Wizard.
Creating Queries by Wizard
–
Choose a query type. The Query wizard includes a few common kinds of queries.
•
•
•
•
–
–
Simple Query Wizard.
Crosstab Query Wizard.
Find Duplicates Query Wizard.
Find Unmatched Query Wizard
OK.
In the Tables/Queries box, choose the table that has the data you want.
Creating Queries by Wizard
–
–
Add the fields you want to see in the query results
Click Next. Enter query name Click Finish.
Understanding the SQL View
•
•
•
Behind the scenes, every query is actually a text command written in a
specialized language called SQL (Structured Query Language).
To take a look at the SQL command for a query, right-click the tab title, and
then choose SQL view.
Example:
1 SELECT Products.ID, Products.ProductName, Products.Price
2 FROM Products
3 WHERE (((Products.Price)>50))
4 ORDER BY Products.Price;
Understanding the SQL View
•
Every query has common ingredients (thành phần), represented by:
–
–
–
–
SELECT: list of fields appear in the query results.
FROM: indicates the table (or tables) that you’re searching.
WHERE: indicates the start of your filter conditions.
ORDER BY: define the sorting order.
Queries and Related Tables
•
•
•
A join line connects tables in the query design. The join line connects the
primary key in one table to the foreign key in another table.
By default, an Access query returns only records where data exists on both
sides of a relationship.
To see records that are not matched on the other side of the join. You must
modify the default query join.
Queries and Related Tables
•
Can create joins between tables in these three ways:
–
–
–
By creating relationships between the tables when you design the database.
By selecting two tables for the query that have a field in common that has the same name
By modifying the default join behavior
Queries and Related Tables
Calculated Fields
•
To create a calculated field, you need to supply two details:
•
Example: define the PriceWithTax calculated field:
Field Name : Expression
PriceWithTax: [Price] * 1.10
Field Name
Expression
Simple Math with Numeric Fields
•
Simple Math with Numeric Fields
Operator
Name
Example
Result
+
Addition
1+1
2
–
Subtraction
1–1
0
*
Multiplication
2*2
4
^
Exponentiation
2^3
8
/
Division
5/2
2.5
\
Integer division
5\2
2
5 Mod 2
1
Mod
Modulus
Expressions with Text
•
•
To join text, use the ampersand (&) operator.
For example:
–
Create a FullName field from the FirstName and LastName fields:
FullName: [FirstName] & “ “ & [LastName]
–
To “The price is” to appear before each price value, use this calculated field:
Price: "The price is: " & [Price]
Query Functions
•
•
A function is a built-in algorithm that takes some data that you supply,
performs a calculation, and then returns a result.
Functions is used in:
–
–
–
Calculated fields. To add information to your query results.
Filter conditions. To determine what records you see in a query.
Visual Basic code.
Using a Function
•
To use a function:
–
–
Enter the function name, followed by parentheses.
Inside the parentheses, put all the information the function needs in order to perform its
calculations.
Example:
SalePrice: Round([Price] * 0.95, 2)
The Expression Builder
•
To quickly find the functions you want, Access provides a tool called the
Expression Builder.
•
To launch the Expression Builder:
–
–
–
–
Open a
query in Design view.
Right-click the box where you want to insert your expression, and then choose Build.
Add or
Click OK.
edit the expression.
The Expression Builder
•
To find a function:
–
–
–
–
Expand the Functions item, and choose Built-In Functions.
Next, choose a function category in the Expression Categories list.
The Expression Values list will show all the functions in that category.
Double click on functions name to insert it into your expression.
Formatting Numbers
Formatting Numbers
•
•
Format(): mathematical function that transforms numbers into text.
Example:
SalePrice: [Price] * 0.95
–
Use the Format() function to apply a currency
format:
SalePrice: Format([Price] / 0.95, "Currency")
Formatting Numbers
Format
Description
Example
Displays a number with two decimal places, thousand separators, and the
Currency
Fixed
currency sign.
Displays a number with two decimal places.
$1,433.20
1433.20
Displays a number with two decimal places and the thousands separator.
Standard
1,433.20
Displays a percent value. Displays 2 digits to the right of the decimal place.
Percent
143320.00%
Displays a number in scientific notation, with two decimal places.
Scientific
1.43E+03
Displays No if the number is 0 and Yes if the number is anything else.
Yes/No
Yes
Functions
•
•
•
More Mathematical Functions (Page 230)
Text Functions (Page 232)
Date Functions (Page 234)