150 likes | 387 Views
Example. Example. Example. Extended Relational-Algebra-Operations. Generalized Projection Aggregate Functions Outer Join. Generalized Projection. Extends the projection operation by allowing arithmetic functions to be used in the projection list. E is any relational-algebra expression
E N D
Extended Relational-Algebra-Operations • Generalized Projection • Aggregate Functions • Outer Join
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, …, Fnare 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)
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 Fiis an aggregate function • Each Aiis an attribute name
Aggregate Operation – Example • Relation r: A B C 7 7 3 10 • gsum(c) (r) sum(c ) 27
pt_works relation • Find out the total sum of salaries of all the part time employees in the bank. gsum(salary) (pt_works) • Find out the total no. of branches appearing in the pt_works relation. gcount (distinct(branch_name)) (pt_works)
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 Fiis an aggregate function • Each Aiis an attribute name
Aggregate Operation – Example • Relation account grouped by branch-name: branch_name account_number balance Perryridge Perryridge Brighton Brighton Redwood A-102 A-201 A-217 A-215 A-222 400 900 750 750 700 branch_nameg sum(balance) (account) branch_name sum(balance) Perryridge Brighton Redwood 1300 1500 700
Example query • Find out the maximum salary for part-time employees at each branch, in addition to the sum of salaries branch_nameg sum(salary), max(salary) (pt_works)
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_nameg sum(balance) as sum_balance (account)
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.
Example • Find the company with the most employees • Find the company with the smallest payroll
Example • Find the company with the most employees • Find the company with the smallest payroll