Query in Microsoft
Access
Lesson plan
• Expression in Microsoft Access
• Create a query
Expression in Microsoft Access
• An expression is a combination of
– data fields, operators, values, and/or functions
• Data fields: column names/attribute names that are put inside[ ] such as [Price]
• Values: number (Int, Double), date…
• Functions: Math (e.g abs), String(len)
Expression in Microsoft Access
Algebraic/Arithmetic Expressions : Contains arithmetic operators:
+ add
- Subtract
* Multiplication
/ Division
Mod: modulo
^ raise to a power
\ divide one integer by another
Expression in Microsoft Access
Algebraic/Arithmetic Expressions : Example:
Expression in Microsoft Access
Boolean Expressions :
Contains comparison/relational and/or boolean operators:
Comparison operators:
< less than
<= less than or equal to
= equal to
<> Not equal to
> Greater
>= Greater than or equal to
Expression in Microsoft Access
Example:
Expression in Microsoft Access
Boolean Expressions : Boolean operators:
and logical AND
or logical OR
NOT Logical NOT
Xor Exclusive OR
Expression in Microsoft Access
Example
Expression in Microsoft Access
Other operators:
Is: Example: Is null
In: determine whether a string value is a member of a list of values
Like: determine whether a string value
begins with one or more characters, use with * or ?
E.g [firstname] like “Jon*”
Between:determine whether a numeric or date value lies within a specific
range of values
E.g between 1 and 7
Expression in Microsoft Access
Example
Expression in Microsoft Access
Functions:
Math
String (Text manipulation) Financial
Data & time
Expression in Microsoft Access
Query in Microsoft Access
Is used to select a group of records that meet the criteria a user specifies (VIEW)
Combine information from different tables and provide a unified view of related items
Query in Microsoft Access
Example: Create a query using Query design window
Query -> Query by Design View
Query in Microsoft Access
Select tables being included in a query Select fields being included in a query Specify the criteria
Query in Microsoft Access
Sorting the display
Query in Microsoft Access
Preventing updates to the query result set Why needed?: preventing users from
updating the results of the queries
Query in Microsoft Access
Types of queries
• Select: as shown in previous slides
• Action: such as create a table, delete, update
• Parameter: repeatedly use a query and make only a simple changes to its criteria
• Crosstab: summarize data from one or more tables in the form of spreadsheet
Action queries
Create Query by Design View -> Right click mouse
Action queries
Parameter queries
Parameter queries
Queries – Practice
• Please try the first 11 queries in project 1 part 3.
• If done, please try queries from 12-14
Crosstab queries
Join tables
Create Crosstable queries
Join tables
– How to join multiple tables
• Identify relationships among tables
• Four types of joins:
– Equi-joins – Outer joins – Self-joins – Theta joins
Equi-joins (inner joins)
– Display all the records in one table that have corresponding records in another table
Example:
Equi-joins (inner joins)
Equi-joins (inner joins)
Equi-joins (inner joins)
….
Designing Nested Queries
Establish the relationship between a query and a table
Designing Nested Queries
Establish the relationship between a query and a table
Designing Nested Queries
Designing Nested Queries
Designing Nested Queries
Outer joins
Display records in one member of the join, regardless of whether corresponding
records exist on the other side of the join
Outer joins
Self joins
Display rows for which values of two columns in the same table are equal
Theta joins
Relate data by using NOT equal comparison rather than equal comparison
Theta joins
Relate data by using NOT equal comparison rather than equal comparison
Practice
Try queries 10-20 in the project part 3