Querying Database ISYS 363. Basic Query Language Operations Selection Projection Join Aggregates:...

21
Querying Database ISYS 363
  • date post

    19-Dec-2015
  • Category

    Documents

  • view

    224
  • download

    0

Transcript of Querying Database ISYS 363. Basic Query Language Operations Selection Projection Join Aggregates:...

Page 1: Querying Database ISYS 363. Basic Query Language Operations Selection Projection Join Aggregates: Max, Min, Sum, Avg, Count –Totals and SubTotals –GroupBy.

Querying Database

ISYS 363

Page 2: Querying Database ISYS 363. Basic Query Language Operations Selection Projection Join Aggregates: Max, Min, Sum, Avg, Count –Totals and SubTotals –GroupBy.

Basic Query Language Operations

• Selection

• Projection

• Join

• Aggregates: Max, Min, Sum, Avg, Count– Totals and SubTotals– GroupBy

• Calculated fields

Page 3: Querying Database ISYS 363. Basic Query Language Operations Selection Projection Join Aggregates: Max, Min, Sum, Avg, Count –Totals and SubTotals –GroupBy.

Selection

• Selection operation retrieves records that satisfy user’s criteria.

Page 4: Querying Database ISYS 363. Basic Query Language Operations Selection Projection Join Aggregates: Max, Min, Sum, Avg, Count –Totals and SubTotals –GroupBy.

Criteria

• >, >=, <, <=, =, <>

• Range: BETWEEN 1/1/03 AND 12/31/03

• Wildcard:– ? – match any one character

• “K?NG”

– * - Match any number of characters• “C*”

Page 5: Querying Database ISYS 363. Basic Query Language Operations Selection Projection Join Aggregates: Max, Min, Sum, Avg, Count –Totals and SubTotals –GroupBy.

Projection

• Projection operation defines a vertical subset of a table and retrieves only the specified fields.

• Example: Customer table: CID, Cname, City, Rating

– Create a query to show CID, Cname, Rating– Create a query to show CID, Cname, City

Page 6: Querying Database ISYS 363. Basic Query Language Operations Selection Projection Join Aggregates: Max, Min, Sum, Avg, Count –Totals and SubTotals –GroupBy.

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.

Page 7: Querying Database ISYS 363. Basic Query Language Operations Selection Projection Join Aggregates: Max, Min, Sum, Avg, Count –Totals and SubTotals –GroupBy.

Join Example

Faculty File: FID FnameF1 ChaoF2 SmithF5 Boxer

Student File:SID Sname FIDS1 Peter F1S2 Paul F2S3 Smith F1

Faculty Join Student =

Page 8: Querying Database ISYS 363. Basic Query Language Operations Selection Projection Join Aggregates: Max, Min, Sum, Avg, Count –Totals and SubTotals –GroupBy.

Join Example

Student File:SID Sname FIDS1 Peter

F1S2 Paul

F2S3 Smith

F1

StudentCourse File:SIDCIDS1ISYS263S1Acct101S3ISYS363S2ISYS263S2Fin350S2Acct101

Course File:CID Cname

UnitsISYS263 IS Intro

3ISYS363 MIS Intro

3Acct101 accounting

3Fin350 Finance Intro

3

(Student Join StudentCourse) Join Course

Page 9: Querying Database ISYS 363. Basic Query Language Operations Selection Projection Join Aggregates: Max, Min, Sum, Avg, Count –Totals and SubTotals –GroupBy.

Aggregate Functions

• Max, Min, Sum, Count, Avg

• QueryTools/Totals

• Ex. Employee: EmpID,Ename, Sex, Race, Birthdate, Hiredate, Salary– How many employees in this company?– What is the overall average salary?

Page 10: Querying Database ISYS 363. Basic Query Language Operations Selection Projection Join Aggregates: Max, Min, Sum, Avg, Count –Totals and SubTotals –GroupBy.

Aggregates by Group

– Compute the average salary by race.– Compute the average salary by sex.– Compute the average salary by Race and

Sex.– How many employees in each race group?

Page 11: Querying Database ISYS 363. Basic Query Language Operations Selection Projection Join Aggregates: Max, Min, Sum, Avg, Count –Totals and SubTotals –GroupBy.

University Database

Page 12: Querying Database ISYS 363. Basic Query Language Operations Selection Projection Join Aggregates: Max, Min, Sum, Avg, Count –Totals and SubTotals –GroupBy.

ERD Notations

Student Account

Faculty Course

Has1 1

EnrollM MAdvise

M

1

SIDSname Major SID Balance

FID Fname CID CnameUnitsPhone

Grade

GPA

Page 13: Querying Database ISYS 363. Basic Query Language Operations Selection Projection Join Aggregates: Max, Min, Sum, Avg, Count –Totals and SubTotals –GroupBy.

Query Examples• Q1: Display students’ ID, name and account

balance who owe university more than $2000.

• Q2: Display student’s ID, name and total units.

• Q3: Find students taking at least 9 units and display their ID, Name and total units.

• Q4: Display CID, Cname, SID, Sname• Q5: Display CID, Cname, number of

students in each course.• Q6: Display faculty’s name and phone if the

faculty advises at least three students.

Page 14: Querying Database ISYS 363. Basic Query Language Operations Selection Projection Join Aggregates: Max, Min, Sum, Avg, Count –Totals and SubTotals –GroupBy.

Top n query: Example Top 3 GPA1. Sort GPA in descending order2. From Query/Design view, click Property Sheet and select Top Value

Page 15: Querying Database ISYS 363. Basic Query Language Operations Selection Projection Join Aggregates: Max, Min, Sum, Avg, Count –Totals and SubTotals –GroupBy.

Order Processing Database

Page 16: Querying Database ISYS 363. Basic Query Language Operations Selection Projection Join Aggregates: Max, Min, Sum, Avg, Count –Totals and SubTotals –GroupBy.

Query Examples

– 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 number of orders for each customer:

• CID, Cname, TotalNumberOfOrders

– Total sales by product– Total amount for each order– MIS report– Criteria applied to subtotal

Page 17: Querying Database ISYS 363. Basic Query Language Operations Selection Projection Join Aggregates: Max, Min, Sum, Avg, Count –Totals and SubTotals –GroupBy.

Calculated Fields

• Rename a field:– NewName:OldName

• Define a calculated field:– Tax:salary*.15– Age:Year(Now()) – Year(DOB)– IIF function

Page 18: Querying Database ISYS 363. Basic Query Language Operations Selection Projection Join Aggregates: Max, Min, Sum, Avg, Count –Totals and SubTotals –GroupBy.

Sorting

• One field sorting

• Two fields sorting

Page 19: Querying Database ISYS 363. Basic Query Language Operations Selection Projection Join Aggregates: Max, Min, Sum, Avg, Count –Totals and SubTotals –GroupBy.

Other Queries

• Update query:– Query tools/Design/Query type/Update

• Delete query

• Parameter query

• CrossTab query– Crosstab row

Page 20: Querying Database ISYS 363. Basic Query Language Operations Selection Projection Join Aggregates: Max, Min, Sum, Avg, Count –Totals and SubTotals –GroupBy.

Access Tools for Import/Export

• Import from Excel:– The first row of Excel’s list should contain field

names– External Data/Import/Excel

• Export to Excel:– External Data/Export/Excel

Page 21: Querying Database ISYS 363. Basic Query Language Operations Selection Projection Join Aggregates: Max, Min, Sum, Avg, Count –Totals and SubTotals –GroupBy.

Open Database Connectivity (ODBC)

• Provide a standard to retrieve data from a database.

• It manages one or more "database drivers“ that enables the communication between database and applications.

• To access a database, we use ODBC facilities to define a ODBC data source name for the database.