Querying Database
Basic Query Language Operations Selection Projection Join Aggregates: Max, Min, Sum, Avg, Count –Totals and SubTotals –GroupBy Calculated fields
Selection Selection operation retrieves records that satisfy user’s criteria.
Projection Projection operation defines a vertical subset of a table and retrieves only the specified fields.
Join The two tables must have common attributes: –Key and foreign key. Combines two tables to form a new table where records of the two tables are combined if the common attributes have the same value.
Example Faculty File: FIDFname F1Chao F2Smith Student File: SIDSname FID S1PeterF1 S2PaulF2 S3SmithF1 Faculty Join Student =
Aggregate Functions Max, Min, Sum, Count, Avg Ex. Student: SID,Sname, GPA, Sex, Major –How many students in this University? –What is the overall average GPA?
Aggregates by Group –How many students in each major? –Compare male students and female students average GPA.
Examples Customer: CID, Cname, City, Rating Orders: OID, Odate, SalesPerson, CID Queries: –Find customers live in San Francisco. –Produce a customer report that shows CID, Cname, and Rating. –Number of customers in each city City, NumbeOfCustomers –Produce a report that shows the total number of orders for each customer: CID, Cname, TotalNumberOfOrders
Access Query Demo Select fields Rename a field: –NewName:OldName Define a calculated field: –Tax:salary*.15 –Age:Year(Now()) – Year(DOB) –IIF function
Criteria >, >=, Range: BETWEEN 1/1/03 AND 12/31/03 Wildcard: –? – match any one character “K?NG” –* - Match any number of characters “C*”
Complex condition Rating = “A” OR Rating = “B” Cname = “Chao” OR Cname = “Smith” Rating = “A” OR Rating = “B” AND City=“SF” (Rating = “A” OR Rating = “B”) AND City=“SF”
Sorting One field sorting Two fields sorting
Total and Subtotal View/Totals –GroupBY –Other functions Examples: –Total number of customers –Number of customers in each city City, NumbeOfCustomers
Join Customer Join Orders Example: –Produce a report that shows the total number of orders for each customer: CID, Cname, TotalNumberOfOrders
Other Queries Update query Delete query Parameter query CrossTab query
Other Tools File/Get External Data File/Export
Comparsion between Excel and Access
Excel’s Database Tools Data –Sort –Filter –Subtotals –Pivot table/Pivot chart OLAP: On Line Analytical Process –Import external data Import data New web query New database query
Word’s Database Tools Tools/Letter and Mailing –Mail merge