Example
-
Upload
kendall-sutton -
Category
Documents
-
view
16 -
download
0
description
Transcript of Example
©Silberschatz, Korth and Sudarshan2.1Database System Concepts - 5th Edition, Oct 5, 2006
Example
©Silberschatz, Korth and Sudarshan2.2Database System Concepts - 5th Edition, Oct 5, 2006
Example
©Silberschatz, Korth and Sudarshan2.3Database System Concepts - 5th Edition, Oct 5, 2006
Example
©Silberschatz, Korth and Sudarshan2.4Database System Concepts - 5th Edition, Oct 5, 2006
Extended Relational-Algebra-Operations
Generalized Projection Aggregate Functions Outer Join
©Silberschatz, Korth and Sudarshan2.5Database System Concepts - 5th Edition, Oct 5, 2006
Generalized Projection
Extends the projection operation by allowing arithmetic functions to be used in the projection list.
E is any relational-algebra expression
Each of F1, F2, …, Fn are are arithmetic expressions involving constants
and attributes in the schema of E. Given relation credit_info(customer_name, limit, credit_balance), find
how much more each person can spend:
customer_name, limit – credit_balance (credit_info)
)( ,...,,21
EnFFF
©Silberschatz, Korth and Sudarshan2.6Database System Concepts - 5th Edition, Oct 5, 2006
Aggregate Functions and Operations
Aggregation function takes a collection of values and returns a single value as a result.
avg: average valuemin: minimum valuemax: maximum valuesum: sum of valuescount: number of values
Aggregate operation in relational algebra
E is any relational-algebra expression
Each Fi is an aggregate function
Each Ai is an attribute name
)()(,,(),( 2211E
nn AFAFAF
©Silberschatz, Korth and Sudarshan2.7Database System Concepts - 5th Edition, Oct 5, 2006
Aggregate Operation – Example
Relation r:
A B
C
7
7
3
10
g sum(c) (r) sum(c )
27
©Silberschatz, Korth and Sudarshan2.8Database System Concepts - 5th Edition, Oct 5, 2006
pt_works relation
employee_name branch_name salary
adams Mirpur 1500
Brown Mirpur 1300
johnson Dhanmondi 5300
gopal Dhanmondi 1700
Peterson Sadarghat 1600
Find out the total sum of salaries of all the part time employees in the bank.
g sum(salary) (pt_works)
Find out the total no. of branches appearing in the pt_works relation.
g count (distinct(branch_name)) (pt_works)
©Silberschatz, Korth and Sudarshan2.9Database System Concepts - 5th Edition, Oct 5, 2006
Aggregate Functions and Operations
Aggregate operation in relational algebra using group
E is any relational-algebra expression
G1, G2 …, Gn is a list of attributes on which to group (can be empty)
Each Fi is an aggregate function
Each Ai is an attribute name
)()(,,(),(,,, 221121E
nnn AFAFAFGGG
©Silberschatz, Korth and Sudarshan2.10Database System Concepts - 5th Edition, Oct 5, 2006
Aggregate Operation – Example
Relation account grouped by branch-name:
branch_name g sum(balance) (account)
branch_name account_number balance
PerryridgePerryridgeBrightonBrightonRedwood
A-102A-201A-217A-215A-222
400900750750700
branch_name sum(balance)
PerryridgeBrightonRedwood
13001500700
©Silberschatz, Korth and Sudarshan2.11Database System Concepts - 5th Edition, Oct 5, 2006
Example query
Find out the maximum salary for part-time employees at each branch, in addition to the sum of salaries
branch_name g sum(salary), max(salary) (pt_works)
©Silberschatz, Korth and Sudarshan2.12Database System Concepts - 5th Edition, Oct 5, 2006
Aggregate Functions (Cont.)
Result of aggregation does not have a name Can use rename operation to give it a name For convenience, we permit renaming as part of aggregate
operation
branch_name g sum(balance) as sum_balance (account)
©Silberschatz, Korth and Sudarshan2.13Database System Concepts - 5th Edition, Oct 5, 2006
Example
branch (branch_name, branch_city, assets)
customer (customer_name, customer_street, customer_city)
account (account_number, branch_name, balance)
depositor (customer_name, account_number)
Find the accounts held by more than two customers using an aggregate
function.
©Silberschatz, Korth and Sudarshan2.14Database System Concepts - 5th Edition, Oct 5, 2006
Example
Find the company with the most employees Find the company with the smallest payroll
©Silberschatz, Korth and Sudarshan2.15Database System Concepts - 5th Edition, Oct 5, 2006
Example
Find the company with the most employees
Find the company with the smallest payroll