Professional Documents
Culture Documents
Computer9.q3 Module
Computer9.q3 Module
BRIEF INTRODUCTION:
Hello dear students! Welcome to Database queries. In this quarter, we’ll introduce you to
database queries with multiple common uses. A database query is a request for a database’s data so we
can retrieve or manipulate it. Each and every lesson is explained step by step with examples and
demonstrations.
Learn the fundamentals of interacting with relational database management systems, including
issuing queries that return results sets and modify data.
In this subject, your prior knowledge in your previous Computer classes will still be used for you
to be able to cope with given tasks/ activities. I hope you will enjoy our lessons!
Thus, in this third quarter learning module, you are expected to acquire the essential knowledge
and develop the basic skills prescribed by DepEd’s learning standards aligned with the MELCs as
shown in the table below.
God bless and let’s get into it!
Most Essential Learning Discuss how queries work, Create and Display query results,
Competencies (MELCs) Relate the lesson to real-life situations
21st Century Learning Skills Creativity, Critical Thinking, Collaboration, Career Learning,
Cross-cultural Understanding, Computer Technology,
Communication
Core Values Academically Excellent, Social Responsibility, Community
Building, Christian Witnessing
References: (Please be guided by the following references to help you perform the given daily
activities.)
Printed:
A.C.E.T. (n.d.). My Computer Microsoft MS ACCESS XP. ACET.
Online Sources:
Database Development - Lesson 30: Introduction to Queries. (n.d.). Functionx. Retrieved
November 15, 2021, from http://www.functionx.com/access/Lesson30.htm
MS Access - Query Data. (n.d.). Tutorialspoint. Retrieved November 15, 2021, from
https://www.tutorialspoint.com/ms_access/ms_access_query_data.htm#:
%7E:text=Advertisements,change%2C%20or%20delete%20table%20data.
1
INTRODUCTION:
Using a query makes it easier to view, add, delete, or change data in your Access database. Some
other reasons for using queries are: find specific quickly data by filtering on specific criteria
(conditions); calculate or summarize data; and automate data management tasks, such as reviewing the
most current data on a recurring basis.
SHORT EXERCISES/DRILLS
Tell whether if the statement is TRUE or FALSE. Write your answer on the line.
______________ 1. Query Wizard is used to create queries such as a Simple query, and a Crosstab
query that finds duplicates and unmatched query.
______________ 2. Queries that you use to retrieve data from a table or to make calculations are
called select queries.
______________ 3. Action Query is used if you want to make changes to any records or move a
group of records. An action query will show an exclamation point (!) along with a
specific identifier icon in the
Navigation Pane.
______________ 4. If you want to review data from only certain fields in a table, or review data
from multiple tables simultaneously or maybe just see the database on certain
criteria, you can use the Action query.
______________ 5. CrosstabQuery. It is used if you want to make changes to any records or move a
group of records. An action query will show an exclamation point (!) along with a
specific identifier icon in the
Navigation Pane.
2
CONTENT DISCUSSION:
Creating a query will help in analyzing data. By using queries, you can search and retrieve
data from one or more tables that meets the condition you specified. In this lesson, you will learn
how.
In a well-designed database, the data that you want to present through a form or report is
usually located in multiple tables. A query can pull the information from various tables and
assemble it for display in the form or report. A query can be either be a request for data results
from your database for action on the data, or for both. A query can give you an answer to a simple
question, perform calculations, combine data from different tables, add, change, or delete data from
a database. Since queries are so versatile, there are many types of queries and you would create a
type of query based on the task.
In a library system, how can you display all of the students who borrowed books together with
the information about the books they borrowed? How are you going to combine the data from your
tables into one location?
WHAT IS A QUERY?
A query is a database object that is used to view, change, and analyze data in different ways. It
can be used to filter data, perform calculations, and summarize data. There are two ways to create
queries in MS Access, using Query Wizard and Query Design. Query Wizard is used to create queries
such as a Simple query, and a Crosstab query that finds duplicates and unmatched query. If you want to
create a new query from scratch, you can use Query Design. In Query Design, the Show table dialog box
will allow you to choose the appropriate fields from the tables or queries in your database. A query is
like a virtual table that allows users to display records from one or more tables.
A query is a request for data results, and for action on data. You can use a query to answer a
simple question, to perform calculations, to combine data from different tables, or even to add, change,
or delete table data.
As tables grow in size, they can have hundreds of thousands of records, which makes it
impossible for the user to pick out specific records from that table.
With a query you can apply a filter to the table's data, so that you only get the information that
you want.
Queries that you use to retrieve data from a table or to make calculations are called select
queries.
Queries that add, change, or delete data are called action queries.
You can also use a query to supply data for a form or report.
In a well-designed database, the data that you want to present by using a form or report is often
located in several different tables.
The tricky part of queries is that you must understand how to construct one before you can
actually use them.
TYPES OF QUERIES
There are three main types of queries: Select, Action, and Crosstab.
1. Select Query. It is the most commonly used type of query. It retrieves records from one or more
tables and displays the results in the order you want in the datasheet view.
2. Action Query. It is used if you want to make changes to any records or move a group of
records. An action query will show an exclamation point (!) along with a specific identifier icon
3
in the
Navigation Pane.
3. Crosstab Query. It is used to calculate and restructure data for easier analysis. By using the
Crosstab query, you can calculate the sum, average, count, or other types of totals for data that is
grouped by two types of information.
4
In the Students table, double-click all those fields which you want to see as result of the query. Add
these fields to the query design grid as shown in the following screenshot.
5
The query runs, and displays only data in those field which is specified in the query
Query Fields
Table Source
The table displayed on the upper portion of the Query Design Grid shows the field sources that
can be added to the query. The query fields on the grid, just below the table source, show the fields that
are included in the query results, which, of course, represent the fields that will be displayed by the
query. Just below the query field is the table source, which tells users from which tables the fields came
from. In most cases, you will be designing queries that contain fields from different tables that contain
6
the same field names. Hence, the table field should be present to verify that you selected the correct
fields for the queries.
Criteria
At the lower portion of the grid, you will see the Criteria grid wherein you can specify data that
can be found on the selected field, which tells the query to display only those records that meet the
criteria.
Query design consists of selecting the fields that would be part of a query. Fields can be added by
designing a query.
To start designing a new query, on the Ribbon, click the Create tab. In the Queries section, click the
Query Design button . This would display the Show Table dialog box.
After selecting a table, some tables, a query, or some queries from the Show Table dialog box,
you can click the Close button of the dialog box. If the Show Table dialog box is closed or for any
reason you want to display it:
In the Query Setup section of the Design tab of the Ribbon, click the Show Table button
Right-click anywhere on the query window and click Show Table...
8
When designing a query, you use a window
named the Query window. If the database is
set to show overlapped windows, the query's
title bar displays its system button on the left
section. This can be used to minimize,
maximize, restore, move, resize, or close the
window. Like all Microsoft Access window
objects, the tab or the title bar displays a
special menu when right-clicked.
In the top wide area of the Query window, the
query displays an object ((a) table(s), (a)
query (queries)) or a group of objects that was
(were) selected to create the query. The lower
portion of the query displays various boxes.
The upper and the lower sections of the query
window are separated by a splitter bar that
you can use to resize them:
EXECUTING A QUERY
10
Executing a query consists of viewing its results. The action or outcome may depend on the type of
query. To view the result of a query:
If the query is currently closed, in the Navigation Pane:
o Right-click the query and click Open
o Double-click the query
If the query is currently opened in the Design View:
SAVING A QUERY
As mentioned already and as we will see in different lessons and sections, there are various ways you
can use a query. For example, you can start a query simply to see some records or to test a condition. If
you plan to use a query many times, you should save it. This is done the same way as for a table:
Right-click the tab or title bar of the window
Click the Save button on the title bar of Microsoft Access
If you are viewing the query, close it
If the query was not previously saved, you would be asked whether you want to save it. You will have
to name the query. Unlike a form or report, the name of a query must be different from the name of any
table of the same database.
To display a query in Design View, from the Navigation Pane, right-click the query and click Design
View.
Probably the easiest way to create a query is by using a wizard. As seen with other categories of
objects (forms and reports), Microsoft Access provides a wizard to help you easily create a query.
The wizard presents the tables and queries of the database so you can select the fields you need.
1. To use the Query Wizard, on the Ribbon, click the Create tab. In the Queries section, click Query
2. On the New Query dialog box, click Simple Query Wizard and click OK. The first page of the
Simple Query Wizard expects you to choose the origin of the query as a table or an already created
query.
11
After creating a query using the Simple Query Wizard, it becomes a normal query like one you had
designed. This means that you can manage its fields any way you like.
3. To create a query, in the Queries section, click the Query Wizard button
4. In the New Query dialog box, click Simple Query Wizard if necessary, and click OK
In the Tables/Queries combo box, make sure Table: States is selected. If not, select it. In the
Available Fields list box, double
click StateName, AreaSqrMiles, AdmissionUnionDate, Region, and Capital
5. Click Next
6. Accept the Detail selection in the wizard page and click Next. Change the name of the query
to States Analysis
7. Click
Finish
1. What is query?
A query is a database object that is used to view, change, and analyze data in different
ways. It can be used to filter data, perform calculations, and summarize data. A query is like a
virtual table that allows users to display records from one or more tables. A query is a request for
data results, and for action on data. You can use a query to answer a simple question, to perform
calculations, to combine data from different tables, or even to add, change, or delete table data.
12
a. Select Query. It is the most commonly used type of query. It retrieves records from one
or more tables and displays the results in the order you want in the datasheet view.
b. Action Query. It is used if you want to make changes to any records or move a group of
records. An action query will show an exclamation point (!) along with a specific
identifier icon in the
Navigation Pane.
c. Crosstab Query. It is used to calculate and restructure data for easier analysis. By using
the Crosstab query, you can calculate the sum, average, count, or other types of totals for
data that is grouped by two types of information.
The query design grid consists of the field and table names, sorting, showing and hiding
fields, and the query criteria
The query in design view consists of the following parts: view options, run query, object
relationship pane, and design grid.
A query retrieves specific data from a table or from two or more related tables. It is also used to
view, change, and analyze data inn different ways. Queries can be used to filter data, perform
calculations, and summarize your data. The three main types of queries are: Select, Actions, and
Crosstab queries.
Most Essential Learning Give the possible query result of a given criterion, Formulate
Competencies sample criterion in query and give its result, Relate the lesson to
real-life situations
13
INTRODUCTION:
SHORT EXERCISES/DRILLS
Tell whether if the statement is true or not. Write TRUE if it is correct and FALSE if otherwise.
______________________ 3. Literal values return to the value based on the results of the calculations
or other operations. For example: Date(), Sum(), Avg()
______________________ 5. Constant are elements in an expression with values that do not change
which will automatically be defined by MS Access such as True, False, and
Null.
CONTENT DISCUSSION:
Query criteria help you zero in on specific14 items in an Access database. If an item matches
all the criteria you enter, it appears in the query result.
CRITERIA FILTERS
Criteria filters record queries. When using criteria, make sure that you place your expressions
under the correct field. Otherwise, your query may return unexpected results.
Query criteria helps you to retrieve specific items from an Access database. If an item matches
with all the criteria you enter, it appears in the query results. When you want to limit the results of a
query based on the values in a field, you use query criteria.
A query criterion is an expression that access compares to query field values to determine
whether to include the record that contains each value.
Some criteria are simple, and use basic operators and constants. Others are complex, and
use functions special operators, and include field references.
To add some criteria to a query, you must open the query in the Design View.
You then identify the fields for which you want to specify criteria.
For instance, using the number 1421 as a criterion under the BOOKTITLE field will
definitely have no results because 1421 should be in the BOOKCODE.
=1421
A query criterion is an expression that you type into the Criteria cell of the query or filter grid. It is also
referred to as the Expression in MS Access. Expression is a fundamental part of the MS Access
operation, similar to a formula which is a combination of symbols, identifiers, operators, and values. If
you do not assign a criterion or expression to a field, MS Access will assume that you want to display
everything in the field. Basic criteria is just like creating a simple mathematical operation. It will display
records using operations like less than, greater than, equal to, etc. Components of an Expression
15
COMPONENTS OF AN EXPRESSION
Literal Values- These are values such as numbers, characters, or dates that MS Access will evaluate
exactly as written. For example: "Jefrey", 1435254, or #06/18/1981#
Constant - These are elements in an expression with values that do not change which will automatically
be defined by MS Access such as True, False, and Null.
Functions - These return to the value based on the results of the calculations or other operations. For
example: Date(), Sum(), Avg()
Identifiers - These refer to the value of the field, control, or property. For example: Forms![Students]!
[Last Name]. This example refers to the value in the Last Name control on the Students form.
Query criteria help you zero in on specific items in an Access database. If an item matches all the
criteria you enter, it appears in the query results.
To add criteria to an Access query, open the query in Design view and identify the fields (columns) you
want to specify criteria for. If the field is not in the design grid, double-click the field to add it to the
design grid and then enter the criterion in the Criteria row for that field.
A query criterion is an expression that Access compares to query field values to determine whether to
include the record that contains each value. For example, = "Chicago" is an expression that Access can
compare to values in a text field in a query. If the value for that field in a given record is "Chicago",
Access includes the record in the query results.
Here are some examples of commonly used criteria that you can use as a starting point to
create your criteria.
16
Result: The query will only display the records of all the books published from the month of August.
Result: The query will display the records of all the students from Saint Therese class.
Result: The query will display the records with last name equal to Dollente
Criteria filters record queries. When using criteria, make sure that you place your
expressions under the correct field. Otherwise, your query may return unexpected results.
17
=1421
3. What are the components of an expression
Literal Values- These are values such as numbers, characters, or dates that MS
Access will evaluate exactly as written. For example: "Jefrey", 1435254, or #06/18/1981#
Constant - These are elements in an expression with values that do not change
which will automatically be defined by MS Access such as True, False, and Null.
Functions - These return to the value based on the results of the calculations or
other operations. For example: Date(), Sum(), Avg()
Identifiers - These refer to the value of the field, control, or property. For
example: Forms![Students]![Last Name]. This example refers to the value in the Last
Name control on the Students form.
You have understood in this lesson that criteria filters record queries. When using
criteria, make sure that you place your expressions under the correct field. A query criterion is an
expression that Access compares to query field values to determine whether to include the record
that contains each value. For example, =”Chicago” is an expression that Access can compare to
values in a text field in a query.
Most Essential Learning Provide the expression of the dialog boxes/query results, Create
Competencies parameter query expressions, Relate the lesson to real-life
situations
References: (Please be guided by the following references to help you perform the given daily
activities.)
Printed:
A.C.E.T. (n.d.). My Computer Microsoft MS ACCESS XP. ACET.
INTRODUCTION:
In our previous lesson, you learned that a query could be used to calculate data. This means that
a query can perform mathematical operations to provide new information derived from one or more
fields. This new information can be displayed s part of a query or report as part of a data table.
When you want a query in Access to ask for input every time that you run it, you can create a
parameter query. You can also create a form to collect parameter values that will be used to restrict the
records returned for queries, forms or reports. This lesson explains how enhance your use of parameters
in queries.
To make a query in Access desktop databases ask for criteria when you run it, create a parameter
query. This allows you to use the same query over and over without having to constantly open it in
Design view to edit the criteria.
SHORT EXERCISES/DRILLS
In the following rows words are jumbled up on the left hand side and a clue is given to help you
re-arrange it into a proper word.
CONTENT DISCUSSION:
Parameter queries require more advanced use of criteria in queries. Parameter queries display their
own dialog boxes to prompt you for the values on which to base your criteria. Unlike simple
criteria wherein you type constant values for fields19(e.g., ="Male" or ="Female"), parameters allow
you to change the value or data in the field to look in, giving you more flexibility. Now, create a
parameter query that will allow you to type the data for the record that you want to display.
USING PARAMETER QUERIES
Parameter is a piece of information you supply to a query right as you run it. Parameters can be used by
themselves or as part of a larger expression to form a criterion in the query. You can add parameters to
any of the following types of queries: select, crosstab, append, make-table, and update.
A parameter query is a query that when it runs, it will display its own dialog box prompting you for
information, such as criteria for retrieving records or a value you want to insert in a field in entering a
parameter, instead of typing the criteria or expressions into the criteria cell, type a text string that would
appear as a prompt for the dialog box. It could be in the form of a question or a simple text that will
prompt you on what to type. You should enclose the text in square brackets ([)
Using parameter queries in a query is as easy as creating a query that uses criteria. You can design a
query to prompt you for one piece of information, such as a part number, or for more than one piece of
information, such as two dates. For each parameter, a parameter query displays a separate dialog box
that prompts you for a value for that parameter.
Parameter
Criterion
Create a query as you normally would, modifying the table joins if necessary, selecting the fields to
include in your query, and adding any non variable criteria to the appropriate fields in the Criteria: row.
• Locate the field or fields where you want the variable criteria to appear, then select
the Criteria: row.
• Type the phrase you want to appear in the prompt that will pop up each time you run your query.
Make sure to enclose the phrase in brackets [ ]. For example, in our parameter query that
searches for orders placed on a certain date, we might type our criteria like this: [What Date?].
• On the Design tab, click the Run command to run your query. A dialog box will appear with the
specified prompt. Enter your search term and click OK to view your query results.
Ideally, the prompt you create for your query should make it clear what type of information the search
term should be, as well as the desired format. For example, to guarantee users enter a search for a date in
the format used in our database, we could write the following in the Criteria: row of the Pickup
Date field like this: [What Date? (mm/dd/yyyy)] .
21
EXAMPLE OF PARAMETER QUERY
Parameter Query EXPRESSION: [ENTER JOB TITLE] under the field name Job Title
22
criteria cell, type a text string that would appear as a prompt for the dialog box. It could be in the
form of a question or a simple text that will prompt you on what to type. You should enclose the
text in square brackets ([)
The simplest parameter query will give you an exact-match criterion, meaning the query will
search for the exact text you enter in the prompt. However, you can turn any type of criteria into variable
criteria. Simply type your prompt text in brackets in the part of the criteria where you would normally
put a search term.
Most Essential Learning Analyze data and identify the type of each value in a table,
Competencies Evaluate expressions based on given values, Relate the lesson to
real-life situations
INTRODUCTION:
23
Now that you already have an idea on using criteria, work on something more interesting than filtering
records. This time, you will combine some fields together to make the query result more presentable.
SHORT EXERCISES/DRILLS
Analyze the table below and identify the data type of each value. Write the corresponding letters of
your answers in the spaces provided.
CONTENT DISCUSSION:
24
Now that you already have an idea on using criteria, work on something more interesting than
filtering records. This time, you will combine some fields together to make the query result more
presentable.
In previous topics, you started using simple expressions, which are combinations of symbols such
as operators (e.g., =, -, +, /, * , < >, <, >), identifiers, and values that produce results. So
far, you only used the operator and a value in your expressions. This time, you will make use of
another operator, which will allow you to combine data in fields that contain data of the same type.
When you want to combine the values in two or more text fields in Access, you create an expression that
uses the ampersand (&) and plus sign (+) operators for example, suppose that you have a form that is
called Employees. You enter each employee’s first and last name in separate fields, but you want to
display the employee’s full name in the form header.
=[LastName]& “” &[FirstName]
The expression uses the & operator to combine the values in the FirstName and LastName fields. The
expression also uses a pair of double quotation (") marks separated by a space character to insert a space
between the first and last names. When you want to insert something between two fields, such as a
space, a punctuation character, or literal text, you must enclose that additional value in quotation marks.
As another example, you can use the following expression to display last name and first name, separated
by a comma and a space:
In this case, the expression inserts a comma and a space enclosed in quotation marks between the
LastName and FirstName fields.
The steps in the following procedure assume that you have a form that is based on a table containing
fields named FirstName and LastName. Otherwise, you can alter the expression in step 6 to suit your
own data.
In this expression, notice that the values LastName and FirstName are enclosed in brackets instead of
quotation marks. This is because these are identifiers. Remember that quotation marks are only used for
constant string values (i.e., text values that do not change such as the name, Smith, or the city, New
York).
Sometimes, one of the fields that you want to combine may not contain a value. This absence of data is
called a null value. When you use the & operator on a field that has no value, Access returns a zero-
length string for that field. For example, if an employee's record has only a last name, the expression in
the preceding example returns a zero-length string for the FirstName field, a space character, and the
value in the LastName field.
Because there is no data in the FirstName field, "Martinez" is preceded by a zero-length string and a
space.
When you combine values from multiple fields into a new string, you may want to include a value in the
new string (such as a comma) only when data exists in a particular field. To conditionally include a
value, you use the + operator instead of the & operator to combine your fields. For example, suppose
that you have a table called Customer, and that table contains fields called City, State, and Postal Code.
You want to combine the values in those fields for a report, but some records may not have a value in
the State field. In that case, you end up with an unwanted comma before the postal code value if you use
the & operator to combine the fields.
25
To eliminate the unwanted comma, you can use the plus (+) operator instead, as shown in the following
sample expression:
=[LastName]+[FirstName]
=([City] & (", " + [State]) & " " & [PostalCode])
The + operator combines text in the same way as the & operator. However, the + operator also supports
what is called Null propagation. Null propagation ensures that if any component of an expression is null,
the result of the entire expression is also null. In the preceding example, consider the portion of the
expression (", " + [State]). Because the + operator is used, the expression that is evaluated within the
inner parentheses includes a comma only if a value exists in the State field. If a value does not exist in
the State field, Null propagation goes into effect, and the expression within the inner parentheses
evaluates to a null value, thus "hiding" the comma.
1. The records that include values for states are displayed with a comma, a space, and the
state abbreviation.
2. There is no state specified for the record with the value Houston in its City field, so the
result of the expression is displayed without the comma, space, or state abbreviation.
We also use the mathematical operator, plus (+), to combine values from two fields. The plus signs (+)
combine the value in the first name field, a space character (a space enclosed in quotation marks), and
the value in the Last name field.
Use calculated fields to concatenate data from separate fields, like first name and last name; or to
calculate dates, like order date plus five days.
Or calculate the quantity, price, and tax rate for each item in an order to display the total price, like this :
([Quantity]*[UnitPrice])+([UnitPrice]*[TaxRate])
You can change this expression into a more descriptive name by editing the expression and typing
the following:
Name:[FirstName]+[LastName]
26
You will see that the result may have combined the data from the fields though they are still not properly
displayed. So, we will edit the expression again to make it look better. The following shows how the
new expression works in detail:
The identifiers, LastName and FirstName fields, contain different values because of their reference
values
from the table records. The expressions contained within string values. For instance, given the following
values the double quotation marks represent constant or fixed
FirstName = Johnny
LastName = Begood
MiddleName = N.
27
When using expressions, never forget that you can only perform mathematical operations if the values
within the same expression are of the same data type. The table below shows how different values of
different data types will be evaluated.
Based on the examples above, you cannot perform calculations on strings and numbers. This means you
cannot use the following expression:
FirstName + 10 or Age = 10 + "10"
In Access, you create an expression that uses the ampersand (&)and plus sign (+)
operators.for example, suppose that you have a form that is called Employees. You enter each
employee’s first and last name in separate fields, but you want to display the employee’s full
name in the form header.To display the full name, you can use this expression:
=[LastName]& “” &[FirstName] Or =[LastName]+[FirstName]
2. How to combine the field using the firstname and lastname field to make an expression?
Name:[FirstName]+[LastName] or Name:[FirstName]+[LastName]
When you want to combine the values in two or more text fields in Access, you create an expression that
uses the ampersand (&)and plus sign (+) operators. For example, suppose that you have a form that is
called Employees. You enter each employee’s first and last name in separate fields, but you want to
display the employee’s full name in the form header.
Most Essential Learning Identify what expressions will produce the given results in a table,
Competencies Write the description of the given expressions and give exact
values for each expression, Relate the lesson to real-life situations
28
INTRODUCTION:
In a well-designed database, the data that you want to present through a form or report is usually
located in multiple tables. A query can pull the information from various tables and assemble it for
display in the form or report. A query can either be a request for data results from your database or for
action on the data, or for both. A query can give you an answer to a simple question perform
calculations, combine data from all different tables, add, change, or delete data from a database. Since
queries are so versatile, there are many types of queries and you would create a type of query based on
the task.
Directions: Fill in the K-W-H-L Chart below to assess your prior knowledge and
understanding of the topic, Using Query Criteria
SHORT EXERCISES/DRILLS
Expression Description
Displays only fields whose value is greater than 870
Displays only records whose Date field value is greater than the date you enter
in the parameter query
Displays only fields whose value is either greater than or equal to 34
Displays only values that are equal to the text string, Bambang
Displays only fields whose value is either less than or equal to 4500
CONTENT DISCUSSION:
In the previous lessons, you learned how to use simple expressions in a query and how to
combine tables in a query to display results from 29 multiple tables. Do you remember during the
database design phase that you were advised not to create calculated fields or fields that store
changing values based on calculations yet?
In this lesson, you will create a calculated field in a query based on values from existing
Query Criteria and Expressions
In the expression above, (TotalPrice is a variable (i.e., a value that may change) while UnitCost and
ProductQuantity are considered fields. The result of the expression's evaluation is stored in the variable,
TotalPrice. In queries, the equal sign (=) is replaced by a colon (:) but the result stays the same.
Operator Function
+ Determines the sum of values in an expression
- Determines the difference between values in an expression
* Determines the product of values in an expression
/ Determines the quotient of values in an expression
The table shows examples of expressions that use arithmetic operators with their corresponding
descriptions and results based on values provided in table below.
Expression Description
=[Stocks]+[Orders] Adds the values of the Stocks and Orders fields
Result 150
30
=[Score/[Items] Divides the value of Score by Items
Result 6.0
Fields are always enclosed in brackets when used in expressions. When fields are included in an
expression, they need to be identified by using brackets ([]) because having several records in a table
means that fields also contain several values. This way, MS Access will make field calculations based
on the values of the current records. This means moving between records also changes field values in
expressions and, therefore, affect results.
You can also use other values in an expression such as constants. A numerical or number constant in an
expression is not enclosed in brackets as shown below. The figure below enumerates the parts of an
expression.
Field/Variable
Total = [Quantity] * 100 Literal/Constant
Variable Operator
Literal values are values that are constant (i.e., do not change), They can either be in the form of
numbers or text. A literal numerical value may be any number that you use in an expression that
represents its original value such as: Total = [UnitPrice] * 100
In the expression above, the UnitPrice field is multiplied by the literal value, 100, to get the Total in
some of our previous examples, you may have noticed the literal values that we used. These are the
values that were not enclosed in brackets and are interpreted by MS Access as they appear. Variables, on
the other hand, are used to store either constant or changing values. The result of an expression is
normally stored in variables. Variables are assigned names so that they can be identified and provide
descriptions of the values that can be stored just like fields.
“100”
"1/05/2006"
These values are all text strings, meaning they are all literal values. Study the following expressions:
MyName "John" + "A." + "Smith"
TotalOrders = "100"
The expressions above contain text strings and their values are all text field data types. You cannot use
strings to compute for the total of numerical values. This means that the sample expression below is
invalid:
Totals [UnitPrice] * [TotalOrders]
This is because the value that is stored in TotalOrders is not a text string.
Aside from adding text and numbers to expressions, you can also use an expression that contains the
>, <, >=, <=, and = operators as a criterion to limit query results. The table below shows examples of
expressions that use these operators and their descriptions.
Example Description
> 100 Displays only fields whose value is greater than 100
< 500 Displays only fields whose value is less than 500
<= 200 Displays only fields whose value is either less than or equal to 200
>= 300 Displays only fields whose value is either greater than or equal to 300
= "Manila" Displays only values that are equal to the text string, Manila
<[Enter Date:] Displays only records whose Date field value is less than the date you enter in
the parameter query
Operator Function
+ Determines the sum of values in an expression
- Determines the difference between values in an expression
* Determines the product of values in an expression
/ Determines the quotient of values in an expression
Literal values are values that are constant (i.e., do not change), They can either be in the
form of numbers or text. A literal numerical value may be any number that you use in an
expression that represents its original value such as: Total = [UnitPrice] * 100
In this lesson, you learned about other query expressions that you can use with different operators such
as <, =, >, >=, and <=. These operators allow you to specify query results. You can also specify which
records in tables should be displayed and operator and the or operator. These operators allow you to set
criteria for single or multiple columns or rows in the Query Design Grid. You can also use the in
operator instead of and operator to create multiple criteria in a single column of a query and the not
operator, which tells MS Access which records should not be displayed.
Most Essential Learning Identify what operator is being used in the query and write the
Competencies corresponding result of the query expression, Analyze the NOT
expressions, Relate the lesson to real-life situations
INTRODUCTION:
The Access query design grid offers almost limitless possibilities for filtering data. The
application of AND and OR logic in the correct combination, and how this combination is represented in
the grid, are key to your filter correctly delivering the set of data you’re seeking.
To illustrate the design of query filters we’ll use the example of a database that track people’s
project work, day-to-day.
Directions: Fill in the K-W-H-L Chart below to assess your prior knowledge and understanding of the
topic, Using AND/OR/NOT OPERATORS
33
Skills I expect to use:
CONTENT DISCUSSION:
Expressions linked by logical AND imply that those expressions are inclusive. So when
you say to yourself that you want your query to filter all records relating to Project Mars
in the North Region, you’re thinking Project Mars AND Region North. Or, to address
your thinking as a logical expression, Project = Mars And Region = North. In the
Access query design grid, you apply filters by inputting into the Criteria part of the grid.
By inputting into the same row of Criteria, you link the filters by logical AND.
In the last example, MS Access makes use of the and operator by displaying only records whose:
34
Level field = “Grade 7”
Only records of both fields which meet the respective criteria are displayed, which means that if a record
only matches the Level field ("Grade 7") but does not match the Gender field (“Male"), it will not be
included in the query result.
The OR OPERATOR, on the other hand, is used when expressions are entered in more than one criteria
row of the same column as shown below. This means that the query will display records that meet any of
the criteria in the column.
When using the AND operator in only one query field, it displays records that meet all of the critería in
the same column. The figure below shows an example of using the and operator in a single column.
J_ _ r _ = Jerry
In the expression above, the query is being told to display records whose:
FirstName field starts with the letter j and contains the letter r
The asterisk (") represents any other letter in the field and is interpreted as follows:
j*" -"j+ any letter"
"r"-"any letter +j + any letter"
You can also use the or operator in more than one query field or column as in the figure
below.
This displays records that meet any of the criteria expressions in any of the columns, which means the
query will display:
IT WILL DISPLAY ALL THE Records whose Gender field "Female" even if the QuizScore is
not equal to 10
IT WILL DISPLAY ALL THE Records whose QuizScore field = 10 even if the Gender field is
not ="Female"
You can also use both the AND and OR operators in three or more fields simultaneously.
The in operator works like the and operator in a single query column. In the previous example for the
and operator, you used the like function to specify values that begin with j and contain the letter r in its
succeeding letters. This time, you will specify two values in a single column by using only the in
operator.
36
The figure above tells MS Access to display the following:
Records of students whose QuizScore 10
Records of students whose QuizScore 12
Expression Result
Not "Female" Does not display records with the value "Female" in the selected field
Not 100 Does not display fields with the value 100 in the selected field
Not A* Does not display records whose values start with A in the selected field
The figure below shows how the not operator is used in a query with a text string value field. It tells
MS Access not to display records of students in the "Grade 9" level.
37
The figure below uses the not operator with a number field and tells MS Access not to display student
records whose QuizScore is equal to 10.
The asterisk (") represents any other letter in the field and is
interpreted as follows:
j*" -"j+ any letter"
o "r"-"any letter +r + any letter"
38
Two Fields Using the Or Operator
You can also use the or operator in more than one query field or column as
in the figure below.
This displays records that meet any of the criteria expressions in any of the columns,
which means the query will display:
Records whose Gender field "Female" even if the QuizScore is not equal to 10
Records whose QuizScore field = 10 even if the Gender field is not ="Female"
You can also use both the AND and OR operators in three or more fields simultaneously.
39
The figure above tells MS Access to display the following:
Records of students whose Level = "Grade 7"
Records of students whose Level "Grade 8"
Expression Result
Not "Female" Does not display records with the value "Female" in the selected field
Not 100 Does not display fields with the value 100 in the selected field
Not A* Does not display records whose values start with A in the selected field
Prepared: Checked:
Noted:
ANNABELLE G. TACADENA
School Principal
40