You are on page 1of 65

Access - Query

One of the most important benefits of


DBMS is the capability of easily asking
questions concerning the data in the
database and rapidly obtaining the
answers.
Access - Query
When you pose a question to Access, or
any other database management system,
the question is called a query ( 查詢 ). A
query is simply a question represented in
a way that Access (DBMS) can
understand.
Access - Query
Query – Limits the data displayed to that
which meets certain criteria (condition 條
件 ).
Access – Query
4 Main Types of Query
1.Select Query ( 選取查詢 )
2.Insert Query ( 新增查詢 )
3.Update Query ( 更新查詢 )
4.Delete Query ( 刪除查詢 )
Access – Query in Access

SQL – Structured Query Language


Standard – Typing Statement.

QBE – Query By Example


Access – Filling Forms.
Access – Query (Q01)

Find out the boats (with marina


number and name) which are
registered in the city Burton.
Access – Query (Q01)

1.Create 2. Query Disgn


Access – Query (Q01)

Select the database table i.e. Marina


Access – Query (Q01)

Select the fields which needs to be displayed


i.e. Marina Number and Name
Access – Query (Q01)

Set up the filtering criteria ( 篩選條件 ) by


selecting the City and entering Burton in
Criteria 準則 .
Access – Query (Q01)

Click “!” to Run the Query


Access – Query (Q01)

Now you can see the result of the query


Access – Query (Q01)

Click X to close and save your query


Access – Query (Q01)

Type Q01 for your query


Access – Query (Q02)

Find out the records (with all


the fields) which are registered
in the city Burton.
Access – Query (Q02)

Click Create-Query design for a new query

Select * to display all fields in the record

Type Burton in the criteria and unclick


display so that the city field with not
display again
Access – Query (Q03)

Find out the records in which


city are either Burton or Torino.
Access – Query (Q03)

Type Burton in the Criteria


and Torino in OR 或 for the City
Access – Query (Q04)

Find out the records in which


the warranty are more than
500.
Access – Query (Q04)

Type > 500 for the Warranty


Access – Query (Q05)

Find out the records in which


the warranty are less than 600
and are registered in Burton.
Access – Query (Q05)

Type < 600 for the warranty


and Burton for the City
Access – Query (Q06)

Find out the records in which


the warranty are between 600
and 900.
Access – Query (Q06)

Type >=600 And <=900 for the warranty


Access – Query (Q07)

List out all records (with marina


number, name and warranty) in
ascending order of warranty.
Access – Query (Q07)

Select the MarinaNumber, Name and Warranty


Select Ascending 遞增 in Sorting 排序
Access – Query (Q08)

Find out the records in which


warranty is larger than non-
warranty.
Access – Query (Q08)

Type >[non-warranty] in the Criteria


of Warranty
Access – Query (Q09)

Find out the name of the


technicians of each boat
(marina number and marina
name).
Access – Query (Q09)

Select Both of the data tables


Access – Query (Q09)

Select MarinaNumber and Name from


the Marina Table
Last Name and First Name from the
Technician Table
Access – Query (Q10)

Find out the difference


between warranty and non-
warranty of each boats.
Access – Query (Q10)

Type Difference: [Warranty]-[Non-warranty]


in the field 欄位
Access – Query (Q11)

Find out the boats which names


starting with the letter F.
Hint: Using Wildcard Character.
? – represent a single character.
* - represent many characters.
Access – Query (Q11)

Type Like "F*“ in the Marina Name


Access – Query (Q12)

List out the records which


Warranty are greater than a
certain amount (Input by user).
Access – Query (Q12)

Type >[Warranty Greater Than] in the


Criteria of Warranty
Access – Query (Q13)

The manager considers to


increase the Warranty by 10%.
Create a query to view the
change without updating the
records.
Access – Query (Q13)

Type Warranty increased by 10%: [Warranty]*1.1


Access – Query (Q14)

The manager wants to give


10% discount to the non-
warranty if it is greater than
600. Create a query to view
the change without updating
the records.
Access – Query (Q14)

Type 10% Discount: [Non-warranty]*0.9

Type >600 for Non-Warranty


Access – Query Σ
(Calculating Statistics)
Group By 群組 StDev 標準差
Sum 總計 Var 變異數
Avg 平均 First 第一筆
Min 最小值 Last 最後一筆
Max 最大值 Expression 運算式
Count 筆數 Where 條件
Access – Query Σ (Q15)

Find out the average warranty


of all records.
Access – Query Σ (Q15)

Click Σ to change to statistic


mode and you will see a
“Totals 合計” row come out
Access – Query Σ (Q15)

Select Average 平均 from the Totals of the Warranty


Access – Query Σ (Q16)

Find out the total warranty of


the boats in Burton.
Access – Query Σ (Q16)

Select Sum 總計 in Totals 合計 for the


Warranty and type Burton in the Criteria
of City
Access – Query Σ (Q17)

Find out the no. of boats which


the City field are Torino.

[Hint: Used 2 field columns.]


Access – Query Σ (Q17)

Select Count 筆數 in the totals of Marina Number


and type Torino in the Criteria of City
Access – Query Σ (Q18)

Find out the total warranty of


each city.
i.e. Burton 1897
Cavela 923.2
Glenview 2369
… …
Access – Query Σ (Q18)

Select Group by 群組 in the Totals of City


and Sum in the Totals of Warranty
Access – Update Query (Q19)

The city council of Torino


decides to change the name of
the city to “Torinana”. Create a
query to update the records.
There is no “UNDO” function for the changed
records. Please make a backup copy of the
database (.mdb file) before testing the query.
Access – Update Query (Q19)

Click update to change the query


from select query to update query
Access – Update Query (Q19)

Select City field, type “Torinana“ in updated to 更新至


and "Torino“ in the Criteria
Access – Update Query (Q19)

Access found 2 records need to be updated


Answer YES to update the records
Access – Update Query (Q20)

The manager decides to give


3% discount to non-warranty if
it is more than 600. Create a
query to update the records.
Access – Update Query (Q20)

Select Non-warranty
Type [Non-warranty]*0.97 in updated to
and >600 in Criteria
Access – Update Query (Q21)

Create a query to delete all


records that the city is
Glenview.
Access – Update Query (Q21)

Click Delete to change to Delete Query


Access – Update Query (Q21)

Select City
Type "Glenview“ in the Criteria
Access – SQL

Structured Query Language


Select Query
SELECT Field 1, Field 2, …
FROM Table 1, Table 2, …
WHERE Conditons;
Access – SQL (Example 1)

SELECT [Marina Number], Name


FROM Marina;

Use of [] here is not standard SQL. Many


(DBMS) database software do not allow using
space in field name.
Access – SQL (Example 2)

SELECT *
FROM Marina
Where City = 'Torino';;
Access – SQL (Example 3A)

SELECT Marina.[Marina Number],


Marina.Name,
Technician.[Last Name],
Technician.[First Name]
FROM Marina , Technician
WHERE Marina.[Tech Number] =
Technician.[Tech Number];
Access – SQL (Example 3B)

SELECT M.[Marina Number], M.Name,


T.[Last Name], T.[First Name]
FROM Marina M , Technician T
WHERE M.[Tech Number] =
T.[Tech Number];

You might also like