Presentation is loading. Please wait.

Presentation is loading. Please wait.

©Silberschatz, Korth and Sudarshan2.1Database System Concepts - 5 th Edition, Oct 5, 2006 Example.

Similar presentations


Presentation on theme: "©Silberschatz, Korth and Sudarshan2.1Database System Concepts - 5 th Edition, Oct 5, 2006 Example."— Presentation transcript:

1 ©Silberschatz, Korth and Sudarshan2.1Database System Concepts - 5 th Edition, Oct 5, 2006 Example

2 ©Silberschatz, Korth and Sudarshan2.2Database System Concepts - 5 th Edition, Oct 5, 2006 Example

3 ©Silberschatz, Korth and Sudarshan2.3Database System Concepts - 5 th Edition, Oct 5, 2006 Example

4 ©Silberschatz, Korth and Sudarshan2.4Database System Concepts - 5 th Edition, Oct 5, 2006 Extended Relational-Algebra-Operations Generalized Projection Aggregate Functions Outer Join

5 ©Silberschatz, Korth and Sudarshan2.5Database System Concepts - 5 th 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 F 1, F 2, …, F n 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)

6 ©Silberschatz, Korth and Sudarshan2.6Database System Concepts - 5 th 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 value min: minimum value max: maximum value sum: sum of values count: number of values Aggregate operation in relational algebra E is any relational-algebra expression Each F i is an aggregate function Each A i is an attribute name

7 ©Silberschatz, Korth and Sudarshan2.7Database System Concepts - 5 th Edition, Oct 5, 2006 Aggregate Operation – Example Relation r: AB   C 7 3 10 g sum(c ) (r) sum(c ) 27

8 ©Silberschatz, Korth and Sudarshan2.8Database System Concepts - 5 th Edition, Oct 5, 2006 pt_works relation employee_namebranch_namesalary adamsMirpur1500 BrownMirpur1300 johnsonDhanmondi5300 gopalDhanmondi1700 PetersonSadarghat1600 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)

9 ©Silberschatz, Korth and Sudarshan2.9Database System Concepts - 5 th Edition, Oct 5, 2006 Aggregate Functions and Operations Aggregate operation in relational algebra using group E is any relational-algebra expression G 1, G 2 …, G n is a list of attributes on which to group (can be empty) Each F i is an aggregate function Each A i is an attribute name

10 ©Silberschatz, Korth and Sudarshan2.10Database System Concepts - 5 th Edition, Oct 5, 2006 Aggregate Operation – Example Relation account grouped by branch-name: branch_name g sum( balance ) ( account ) branch_nameaccount_numberbalance Perryridge Brighton Redwood A-102 A-201 A-217 A-215 A-222 400 900 750 700 branch_namesum(balance) Perryridge Brighton Redwood 1300 1500 700

11 ©Silberschatz, Korth and Sudarshan2.11Database System Concepts - 5 th 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 )

12 ©Silberschatz, Korth and Sudarshan2.12Database System Concepts - 5 th 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 )

13 ©Silberschatz, Korth and Sudarshan2.13Database System Concepts - 5 th 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.

14 ©Silberschatz, Korth and Sudarshan2.14Database System Concepts - 5 th Edition, Oct 5, 2006 Example Find the company with the most employees Find the company with the smallest payroll

15 ©Silberschatz, Korth and Sudarshan2.15Database System Concepts - 5 th Edition, Oct 5, 2006 Example Find the company with the most employees Find the company with the smallest payroll


Download ppt "©Silberschatz, Korth and Sudarshan2.1Database System Concepts - 5 th Edition, Oct 5, 2006 Example."

Similar presentations


Ads by Google