Sunteți pe pagina 1din 18

MICROSOFT ACCESS

INTRODUCTION TO QUERIES

OVERVIEW OF QUERIES
In Access, queries are used to manipulate
data in the database. Queries can be used as views that show the specific data or group of data rather than all the data in the database. Queries can be used in forms, reports and controls for requesting data from database. Queries can be based on a single table or multiple tables.

TYPES OF QUERIES
Select queries
Retrieve data based on specific conditions.

Action queries
Add, update and delete data.

Parameter queries
Prompt user for search/action criteria.

Crosstab queries
Summarize data into categories.

SQL queries
SQL commands (in SQL view only).

CREATING QUERIES
Access provides a tool called Query Design
window for user to create queries manually. The window comprises two parts:
Field list Design grid

Query Design window can be used to create


all types of queries supported by Access. The Query Design window is interchangeable with the SQL view window.

CREATING QUERIES
User can use the SQL view window to create
queries manually. When executing a query, Access translates the query (design view version) into Jet SQL commands. Jet SQL is the version of SQL adopted by Access, which supports most of the standard SQL operations. To use the SQL view window, user needs to know how to write a SQL statements.

CREATING QUERIES
User can also use the Query Wizard to create
queries. Query Wizard is a program that helps user to create select queries in an automated, stepby-step manner. However, it doesnt provide the flexibility for user to customize or specify the output of a query.

SIMPLE SELECT QUERIES


A simple select query is a query for retrieving
data from a table without complex condition. To create a select query, user needs to

Add tables into the field list. Choose the tables and fields required. Specify the criteria or conditions. Run the query or switch to Datasheet View. Save the query.

SIMPLE SELECT QUERIES


Fields & Expressions In the Field cell (design grid), user can enter an existing field name, or an expression. Expression can be a new name or a simple calculation expression. The calculation expression may include any combination of the arithmetic operations. The new names or calculation expressions are temporary, and will not affect the data or the name of the actual fields.

SIMPLE SELECT QUERIES


Criteria & Expressions. In the Criteria cell (design grid), user may enter expressions that limit the records to be selected by a select query. The criteria expressions often include the use of arithmetic (less common), comparison, and logical operators. The criteria expression can limit the data by one or more conditions. Only records that satisfy the condition will be selected.

SIMPLE SELECT QUERIES


Wildcard Characters Wildcard characters are special characters that used in searching operations based on the pattern matching technique. Access supports four wildcard characters: asterisk (*), question mark (?), exclamation mark (!) and brackets ([]). The use of wildcard characters is associated with the LIKE operator.

SIMPLE SELECT QUERIES


Examples Like A* Match any string starts with A. Like N??E Match any 4-letter string starts with A and ends with E. Like [A-C]* Match any string starts with A, B or C. Like [!A-C]* Match any string doesnt start with A, B and C.

SIMPLE SELECT QUERIES


Sorting Records The Sort cell (design grid) enables user to decide how records should be arranged in the output view. Records can be sorted:
Ascending based on a selected field. Descending based on a selected field.

Sorting is useful to make the queries output


helpful to a particular user, or group of users.

SIMPLE SELECT QUERIES


Grouping Records Grouping is a technique for arranging records according to categories. To apply grouping, user needs to choose the GROUP BY option in the Total cell (design grid) for the required field. If Total cell is not shown (design grid), it can be made available by choosing View, Total option from the menu bar.

SIMPLE SELECT QUERIES


Aggregate Functions Aggregate functions are built-in functions for data manipulations in select queries. An aggregate function may take a set of values and return only one value as result. Aggregate functions can be applied to all records in a table, a joined table, or groups in a table. However, they will not affect the actual values or data in the base tables.

SIMPLE SELECT QUERIES


Access supports the following aggregate
functions:

SUM COUNT AVG MAX MIN STDEV VAR FIRST LAST

COMPLEX SELECT QUERIES


Relationships Between Data Sources Data in different tables can be linked by relationships, which can be created via the Relationship window. If a relationship is not available between two tables, Access can still define a relationship if
Both tables contain a common field of the same
name and data type. And, at least one of the fields is a primary key field.

COMPLEX SELECT QUERIES


Joining Tables A join is the process of combining records from two tables. Only tables that are linked can be joined over the common field. A self-join combines records from the same table. The default join of Access is an INNER JOIN, which returns only those records in which the values are the same in the related field.

OPTIMIZING QUERIES
Guidelines: Avoid using asterisk (*) for retrieval.
Specifying all field names instead of using *.

Limit fields.
Specifying the fields needed to be displayed.

Avoid nested queries.


Use subqueries instead of nested queries, if
possible.

Avoid multiple SQL GROUP BY clauses.

S-ar putea să vă placă și