Relational Algebra
Query Languages Language in which user requests information from the database. Pure languages form underlying basis of query languages that people use.
Relational Algebra Basic operators Select Project Union Intersection Set difference Cartesian product Rename Assignment Join Division Generalized Project Aggregate functions The operators take one or more relations as inputs and give a new relation as a result.
Select Operation Notation: p(r) p is called the selection predicate Example of selection: branch-name=“Perryridge” (account)
Select Operation – Example Account
Select Operation – Example The result is the relation: Account-number Branch-name balance A-101 perryridge 400 6
Select Operation – Example Balance >“700” (account) Account-number Branch-name balance A-201 Brighton 900 A-217 750 7
Project Operation Notation: A1, A2, …, Ak (r) where A1, A2 are attribute names and r is a relation name. The result is defined as the relation of k columns obtained by erasing the columns that are not listed
Project Operation E.g. To eliminate the branch-name attribute of account account-number, balance (account) The result relation is:
Project Operation Account-number Balance A-101 500 A102 400 A-201 900 700 A-217 750 A-222 A-305 350 10
Union Operation Notation: r s Defined as: r s = {t | t r or t s} For r s to be valid. 1. r, s must have the same number of attributes 2. The attribute domains must be compatible (e.g., 2nd column of r deals with the same type of values as does the 2nd column of s)
Union Operation E.g. to find all customers with either an account or a loan customer-name (depositor) customer-name (borrower) Customer-name Account-no. Ali A-101 Mahmood A-201 Ahmid A-217 Linda A-222 Rana A-305 Customer-name Loan-no. Ali L-11 Kasim Ahmid L-25 Linda Rana L-34 depositor borrower
Union Operation Customer-name Ali Mahmood Ahmid Linda Rana …. customer-name (depositor) customer-name (borrower) Customer-name Ali Mahmood Ahmid Linda Rana …. Customer-name Ali Kasim Ahmid Linda Rana …..
Union Operation Customer-name Ali Mahmood Ahmid Linda Rana Kasim The result relation is: Customer-name Ali Mahmood Ahmid Linda Rana Kasim
Intersection Operation Notation: r s Defined as: r s ={ t | t r and t s } Assume: r, s have the same number of attributes attributes of r and s are compatible
Intersection Operation E.g. to find all customers with an account and a loan customer-name (depositor) customer-name (borrower) Customer-name Account-no. Ali A-101 Mahmood A-201 Ahmid A-217 Linda A-222 Rana A-305 Customer-name Loan-no. Ali L-11 Kasim Ahmid L-25 Linda Rana L-34 depositor borrower
Intersection Operation customer-name (depositor) customer-name (borrower) Customer-name Ali Mahmood Ahmid Linda Rana …. Customer-name Ali Kasim Ahmid Linda Rana …..
Intersection Operation The result relation is: Customer-name Ali Ahmid Linda Rana
Set Difference Operation Notation r – s Defined as: r – s = {t | t r and t s} Set differences must be taken between compatible relations. r and s must have the same number of attributes. attribute domains of r and s must be compatible.
Set Difference Operation – Example For example, find all customers who have an account and haven't a loan. customer-name (depositor) - customer-name (borrower) Customer-name Account-no. Ali A-101 Mahmood A-201 Ahmid A-217 Linda A-222 Rana A-305 Customer-name Loan-no. Ali L-11 Kasim Ahmid L-25 Linda Rana L-34 depositor borrower 20
Set Difference Operation – Example customer-name (depositor) customer-name (borrower) Customer-name Ali Mahmood Ahmid Linda Rana …. Customer-name Ali Kasim Ahmid Linda Rana …..
Set Difference Operation – Example The result relation is: Customer-name Mahmood
Set Difference Operation – Example For example, find all customers who have a loan, but they haven't an account. customer-name (borrower) - customer-name (depositor) Customer-name Account-no. Ali A-101 Mahmood A-201 Ahmid A-217 Linda A-222 Rana A-305 Customer-name Loan-no. Ali L-11 Kasim Ahmid L-25 Linda Rana L-34 borrower depositor 23
Set Difference Operation – Example customer-name (borrower) customer-name (depositor) Customer-name Ali Kasim Ahmid Linda Rana ….. Customer-name Ali Mahmood Ahmid Linda Rana ….
Set Difference Operation – Example The result relation is: Customer-name Kasim
Cartesian-Product Operation Notation r x s Defined as: r x s = {t q | t r and q s} Assume that attributes of r(R) and s(S) are disjoint. (That is, R S = ). If attributes of r(R) and s(S) are not disjoint, then renaming must be used.
Cartesian-Product Operation Relations r and s : r s C D E c1 d1 1 c2 d2 2 c3 d3 3 c4 d4 A B a1 1 a2 2
Cartesian-Product Operation The relation r x s is: A B C D E a1 1 c1 d1 c2 d2 2 c3 d3 3 c4 d4 a2 28
Rename Operation Allows us to name, and therefore to refer to, the results of relational-algebra expressions. Allows us to refer to a relation by more than one name. Example: x (E) returns the expression E under the name X If a relational-algebra expression E has a number of attributes n, then x (A1, A2, …, An) (E) returns the result of expression E under the name X, and with the attributes renamed to A1, A2, …., An.
Assignment Operation The assignment operation () provides a convenient way to express complex queries. Write query as a sequential program consisting of a series of assignments. followed by an expression whose value is displayed as a result of the query. Assignment must always be made to a temporary relation variable.
Assignment Operation Example: Write customer-name (borrower)-customer-name (depositor) temp1 customer-name (borrower) temp2 customer-name (depositor) result = temp1 – temp2 The result to the right of the is assigned to the relation variable on the left of the . May use variable in subsequent expressions.
Relational Schema for the Banking Enterprise branch (branch-name, branch-city, assets) account (account-number, branch-name, balance) loan (loan-number, branch-name, amount) depositor (customer-name, account-number) borrower (customer-name, loan-number)
loan-number (amount > 1200 (loan)) Example Queries Find all loans of over $1200. amount > 1200 (loan) Find the loan number for each loan of an amount greater than $1200. loan-number (amount > 1200 (loan))
Example Queries Find the names of all customers who have a loan, an account, or both, from the bank. customer-name (borrower) customer-name (depositor) Find the names of all customers who have a loan and an account at bank. customer-name (borrower) customer-name (depositor)
Example Queries Find the names of all customers who have a loan at the Perryridge branch. customer-name (branch-name=“Perryridge” (borrower.loan-number = loan.loan-number(borrower x loan))) OR customer-name(loan.loan-number = borrower.loan-number( (branch-name = “Perryridge”(loan)) x borrower)) OR customer-name (branch-name=“Perryridge” and borrower.loan-number = loan.loan-number (borrower x loan))
Example Queries Find the names of all customers who have a loan at the Perryridge branch but they are not depositors. customer-name (branch-name = “Perryridge” (borrower.loan-number = loan.loan-number(borrower x loan))) – customer-name(depositor)
Example Queries Find the largest account balance Rename account relation as d The query is: balance(account) - account.balance (account.balance < d.balance (account x rd (account)))
Natural-Join Operation Notation: r s Natural join () is an operator that is written as (R S) where R and S are relations. The result of the natural join is the set of all combinations of tuples in R and S that are equal on their common attribute names.
Natural-Join Operation Notation: r s Example: R = (A, B, C, D) S = (E, B, D) Result schema = (A, B, C, D, E) r s is defined as: r.A, r.B, r.C, r.D, s.E (r.B = s.B and r.D = s.D (r x s)) It is a special case of the “Cartesian Product”
Natural Join Operation – Example Relations r, s: B D E A B C D 1 3 2 a b 1 2 4 a b r s r s A B C D E 1 2 a b
Division Operation r s Suited to queries that include the phrase “for all”. The division is a binary operation that is written as R ÷ S. The result consists of the restrictions of tuples in the header of R but not in the header of S, and all their combinations with tuples in S are present in R.
Division Operation Example
Division Operation – Example B B Relations r, s: 1 2 3 4 6 1 2 s r s: A r
Division Operation Example Relations r, s: A B C D E D E a a b 1 3 a b 1 s r A B C r s: a
Example Queries (1) Find all customers who have an account from at least the “Downtown” and the Uptown” branches. where CN denotes customer-name and BN denotes branch-name. Query 1 CN(BN=“Downtown”(depositor account)) CN(BN=“Uptown”(depositor account))
Example Queries (2) Find all customers who have an account from at least the “Downtown” and the Uptown” branches. Query 2 customer-name, branch-name (depositor account) temp(branch-name) ({(“Downtown”), (“Uptown”)})
Example Queries (3) Find all customers who have an account at all branches located in Brooklyn city. customer-name, branch-name (depositor account) branch-name (branch-city = “Brooklyn” (branch))
Generalized Projection Extends the projection operation by allowing arithmetic functions to be used in the projection list. F1, F2, …, Fn(E) E is any relational-algebra expression Each of F1, F2, …, Fn are are arithmetic expressions involving constants and attributes in the schema of E.
Generalized Projection Given relation credit-info(customer-name, limit, credit-balance). find how much more each person can spend: customer-name, limit – credit-balance (credit-info)
Generalized Projection Credit-info customer-name Limit credit-balance Ali 1500 380 Ahmed 2000 1400 Rana 1000 500 Kasim 3500
Generalized Projection The result relation is: customer-name Limit-credit-balance Ali 1220 Ahmed 600 Rana 500 Kasim 2100
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
Aggregate Functions and Operations G1, G2, …, Gn g F1( A1), F2( A2),…, Fn( An) (E) 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
Aggregate Operation – Example B C Relation r: 7 3 10 sum-C g sum(c) (r) 27
Aggregate Operation – Example account branch-name account-number balance Perryridge Brighton Redwood A-102 A-201 A-217 A-215 A-222 400 900 750 700 branch-name g sum(balance) (account) branch-name balance Perryridge Brighton Redwood 1300 1500 700
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) Branch-name Sum-balance Perryridge 1300 Brighton 1500 Redwood 700
More Examples
Relational Instances for the Purchasing System The Supplier relation: S-number S-name S-city S100 Ahmed Amman S200 Ali Jarash S300 Kasim Irbid S400 Jasim Aqaba S500 Rana
The Part relation: P-number P-name Color Price P-city P1 TV Silver 300 Amman P2 Camera Black 100 Jarash P3 Video 200 P4 PC 400 Irbid P5 Printer Red P6 Scanner silver 150 59
The shipment relation: S-number P-number Quantity S100 P1 100 P2 150 P3 200 P4 160 S200 S300 400 S400 80 S500 60
Q1- Find The cities for all suppliers . S-city (Supplier)
Q1- Find The cities for all suppliers . S-city (Supplier) S-city Amman Jarash Irbid Aqaba The result relation
Q2- Find city for Ahmed.
Q2- Find city for Ahmed. Temp1 S-name = ‘Ahmed’ (Supplier)
Q2- Find city for Ahmed. Temp1 S-name = ‘Ahmed’ (Supplier) S-number S-name S-city S100 Ahmed Amman Temp1
Q2- Find city for Ahmed. Temp1 S-name = ‘Ahmed’ (Supplier) S-number S-name S-city S100 Ahmed Amman Temp1 Result S-city (Temp1) S-city Amman Result
Q3- Find the number and name for suppliers in Amman.
Q3- Find the number and name for suppliers in Amman. Temp1 S-city= ‘Amman’ (Supplier)
Q3- Find the number and name for suppliers in Amman. Temp1 S-city= ‘Amman’ (Supplier) S-number S-name S-city S100 Ahmed Amman S500 Rana Temp1
Q3- Find the number and name for suppliers in Amman. Temp1 S-city= ‘Amman’ (Supplier) S-number S-name S-city S100 Ahmed Amman S500 Rana Temp1 Result S-number, S-name (Temp1)
Q3- Find the number and name for suppliers in Amman. Temp1 S-city= ‘Amman’ (Supplier) S-number S-name S-city S100 Ahmed Amman S500 Rana Temp1 Result S-number, S-name (Temp1) S-number S-name S100 Ahmed S500 Rana Result
Q4- For each part shipped, find the part names and the names of all cities storing these parts.
Q4- For each part shipped, find the part names and the names of all cities storing these parts. Temp1 P-number ( Shipment )
Q4- For each part shipped, find the part names and the names of all cities storing these parts. Temp1 P-number ( Shipment ) P-number P1 P2 P3 P4 Temp1
Q4- For each part shipped, find the part names and the names of all cities storing these parts. Temp1 P-number ( Shipment ) Temp2 Part Temp1
Q4- For each part shipped, find the part names and the names of all cities storing these parts. Temp1 P-number ( Shipment ) Temp2 Part Temp1 P-number P-name Color Price P-city P1 TV Silver 300 Amman P2 Camera Black 100 Jarash P3 Video 200 P4 PC 400 Irbid Temp2
Q4- For each part shipped, find the part names and the names of all cities storing these parts. Temp2 Part Shipment Result P-name, p-city(Temp2) P-name P-city TV Amman Camera Jarash Video PC Irbid Result
Q5- For each part shipped, find the part numbers and names of all cities supplying the parts.
Q5- For each part shipped, find the part numbers and names of all cities supplying the parts. Temp1 Shipment Supplier
Q5- For each part shipped, find the part numbers and names of all cities supplying the parts. Temp1 Shipment Supplier S-number P-number Quantity S-name S-city S100 P1 100 Ahmed Amman P2 150 P3 200 P4 160 S200 Ali Jarash S300 400 Kasim Irbid S400 Jasim Aqaba 80 S500 Rana Temp1
Result P-number, S-city ( Temp1 ) Q5- For each part shipped, find the part numbers and names of all cities supplying the parts. Temp1 Shipment Supplier P-number S-city P1 Amman P2 P3 P4 Jarash Irbid Aqaba Result P-number, S-city ( Temp1 ) Result
Q6- Get supplier numbers for suppliers who supply part p2
Q6- Get supplier numbers for suppliers who supply part p2 Temp1 p-number = ‘p2’ (Shipment)
Q6- Get supplier numbers for suppliers who supply part p2 Temp1 p-number = ‘p2’ (Shipment) S-number P-number Quantity S100 P2 150 S200 S300 400 S400 Temp1
Q6- Get supplier numbers for suppliers who supply part p2 Temp1 P-number = ‘p2’ (Shipment) Result S-number ( Temp1 ) S-number S100 S200 S300 S400 Result
Example Queries Q7- Get names of suppliers who supply at least one silver part.
Example Queries Q7- Get names of suppliers who supply at least one silver part. Temp1 P-color = ‘silver’ (Part)
Example Queries Q7- Get names of suppliers who supply at least one silver part. Temp1 P-color = ‘silver’ (Part) P-number P-name Color Price P-city P1 TV Silver 300 Amman P4 PC 400 Irbid P6 Scanner silver 150 Jarash Temp1
Example Queries Q7- Get names of suppliers who supply at least one silver part. Temp1 P-color = ‘silver’ (Part) Temp2 Shipment Temp1
Example Queries Q7- Get names of suppliers who supply at least one silver part. Temp1 P-color = ‘silver’ (Part) Temp2 Shipment Temp1 S-number P-number Quantity P-name Color Price P-city S100 P1 100 TV Silver 300 Amman P4 160 PC 400 Irbid S200 200 S400 80 S500 Temp2
Example Queries Q7- Get names of suppliers who supply at least one silver part. Temp1 P-color = ‘silver’ (Part) Temp2 Shipment Temp1 Temp3 S-number (Temp2) S-number S100 S200 S400 S500 Temp3
Example Queries Q7- Get names of suppliers who supply at least one silver part. Temp1 P-color = ‘silver’ (Part) Temp2 Shipment Temp1 Temp3 S-number (Temp2) Temp4 Supplier Temp3 S-number S-name S-city S100 Ahmed Amman S200 Ali Jarash S400 Jasim Aqaba S500 Rana Temp4
Example Queries Q7- Get names of suppliers who supply at least one silver part. Temp1 P-color = ‘silver’ (Part) Temp2 Shipment Temp1 Temp3 S-number (Temp2) Temp4 Supplier Temp3 Result S-name (Temp4) S-name Ahmed Ali Jasim Rana Result
Example Queries Q8- Get the names and cities of suppliers who supply parts stored in Irbid.
Example Queries Q8- Get the names and cities of suppliers who supply parts stored in Irbid. Temp1 P-city = ‘Irbid’ (Part)
Example Queries Q8- Get the names and cities of suppliers who supply parts stored in Irbid. Temp1 P-city = ‘Irbid’ (Part) P-number P-name Color Price P-city P4 PC Silver 400 Irbid P5 Printer Red 100 Temp1
Example Queries Q8- Get the names and cities of suppliers who supply parts stored in Irbid. Temp1 P-city = ‘Irbid’ (Part) Temp2 Shipment Temp1 Temp2 S-number P-number Quantity P-name Color Price P-city S100 P4 160 PC Silver 400 Irbid P5 50 Printer Red 100 S400 80 S500
Example Queries Q8- Get the names and cities of suppliers who supply parts stored in Irbid. Temp1 P-city = ‘Irbid’ (Part) Temp2 Shipment Temp1 Temp3 S-number (Temp2) S-number S100 S400 S500 Temp3
Example Queries Q8- Get the names and cities of suppliers who supply parts stored in Irbid. Temp1 P-city = ‘Irbid’ (Part) Temp2 Shipment Temp1 Temp3 S-number (Temp2) Temp4 Supplier Temp3
Example Queries Q8- Get the names and cities of suppliers who supply parts stored in Irbid. Temp1 P-city = ‘Irbid’ (Part) Temp2 Shipment Temp1 Temp3 S-number (Temp2) Temp4 Supplier Temp3 S-number S-name S-city S100 Ahmed Amman S400 Jasim Aqaba S500 Rana Temp4
Example Queries Q8- Get the names and cities of suppliers who supply parts stored in Irbid. Temp1 P-city = ‘Irbid’ (Part) Temp2 Shipment Temp1 Temp3 S-number (Temp2) Temp4 Supplier Temp3 Result S-name , S-city (Temp4)
Example Queries Q8- Get the names and cities of suppliers who supply parts stored in Irbid. Temp1 P-city = ‘Irbid’ (Part) Temp2 Shipment Temp1 Temp3 S-number (Temp2) Temp4 Supplier Temp3 Result S-name , S-city (Temp4) S-name S-city Ahmed Amman Jasim Aqaba Rana Result
Example Queries Q9- Find supplier names for suppliers who supply all parts.
Example Queries Q9- Find supplier names for suppliers who supply all parts. Temp1 P-number (Part)
Example Queries P-number Q9- Find supplier names for suppliers who supply all parts. Temp1 P-number (Part) P-number P1 P2 P3 P4 P5 P6 Temp1
Example Queries Q9- Find supplier names for suppliers who supply all parts. Temp1 P-number (Part) Temp2 S-number , P-number (Shipment)
Example Queries Q9- Find supplier names for suppliers who supply all parts. Temp1 P-number (Part) S-number P-number S100 P1 P2 P3 P4 P5 P6 S200 S300 S400 S500 Temp2 S-number , P-number (Shipment) Temp2
Example Queries Q9- Find supplier names for suppliers who supply all parts. Temp1 P-number (Part) Temp2 S-number , P-number (Shipment) Temp3 Temp2 Temp1
Example Queries S-number S100 Q9- Find supplier names for suppliers who supply all parts. Temp1 P-number (Part) Temp2 S-number , P-number (Shipment) Temp3 Temp2 Temp1 S-number S100 Temp3
Example Queries Q9- Find supplier names for suppliers who supply all parts. Temp1 P-number (Part) Temp2 S-number , P-number (Shipment) Temp3 Temp2 Temp1 Temp4 Supplier Temp3
Example Queries Q9- Find supplier names for suppliers who supply all parts. Temp1 P-number (Part) Temp2 S-number , P-number (Shipment) Temp3 Temp2 Temp1 Temp4 Supplier Temp3 Temp4 S-number S-name S-city S100 Ahmed Amman
Example Queries Q9- Find supplier names for suppliers who supply all parts. Temp1 P-number (Part) Temp2 S-number , P-number (Shipment) Temp3 Temp2 Temp1 Temp4 Supplier Temp3 Result S-name (Temp4)
Example Queries Q9- Find supplier names for suppliers who supply all parts. Temp1 P-number (Part) Temp2 S-number , P-number (Shipment) Temp3 Temp2 Temp1 Temp4 Supplier Temp3 Result S-name (Temp4) S-name Ahmed Result
Example Queries Q10- How many parts they have?
Example Queries P-number 6 Q10- How many parts they have? g count (P-number) (Part) P-number 6
Example Queries Q11- Find the total quantities supplied by each supplier.
Example Queries Q11- Find the total quantities supplied by each supplier. S-number g sum (Quantity) (Shipment)
Example Queries Q11- Find the total quantities supplied by each supplier. S-number g sum (Quantity) (Shipment) S-number Quantity S100 730 S200 350 S300 400 S400 250 S500 100
Example Queries Q11- Find the total quantities supplied by each supplier. S-number g sum (Quantity) as ( sum-quantity) (Shipment) S-number Sum-Quantity S100 730 S200 350 S300 400 S400 250 S500 100
END