Mod 2
Mod 2
Mod 2
2 Smith 2500 1
3 Blake 3700 3
4 Xyp 2900 3
5 Pst 3785 5
Write the relational algebra expression to find the details of the emp ( eno, ename, sal,
dno) whose salary is 2500.
Ans: σ(sal = 2500)(emp) eno ename sal dno
2 Smith 2500 1
2 Smith 2500 1
3 Blake 3700 3
4 Xyp 2900 3
5 Pst 3785 5
Write the relational algebra expression to find the details of the emp ( eno, ename, sal,
dno) of department 3.
Ans: σ(dno = 3)(emp) eno ename sal dno
3 Blake 3700 3
4 Xyp 2900 3
2 Smith 2500 1
3 Blake 3700 3
4 Xyp 2900 3
5 Pst 3785 5
Write the relational algebra expression to find the details of the emp ( eno, ename, sal,
dno) whose name is jones.
Ans: σ(ename = “jones”)(emp) eno ename sal dno
1 Jones 1000 5
3 Blake 3700 3
4 Xyp 2900 3
5 Pst 3785 5
Write the relational algebra expression to find the details of the emp ( eno, ename, sal,
dno) whose sal > 3000 and dno = 5
Ans: σ(sal> 3000 ^ dno = 5)(emp) eno ename sal dno
5 Pst 3785 5
A B C D
α α 1 7
β β 23 10
S2
A C
α 1
β 1
2 Smith 2500 1
3 Blake 3700 3
4 Xyp 2900 3
5 Pst 3785 5
Write the relational algebra expression to find employee name whose salary is greater
than 2500. ename
Ans: πename (σ(sal > 2500)(emp) )
Blake
Xyp
Pst
External Query: select ename from emp where sal > 2500
2 Smith 2500 1
3 Blake 3700 3
4 Xyp 2900 3
5 Pst 3785 5
Write the relational algebra expression to find employee no and salary whose salary is
greater than 3000 and dno = 3. ename sal
Ans: πename,sal (σ(sal > 3000s ^ dno=3)(emp) )
Blake 3700
πSNO(σSTATE=‘NY’ (S))
O/P :
NAME SYMBOL
UNION U
INTERSECTION ∩
SET DIFFERENCE -
CARTESIAN PRODUCT ×
• Denoted by x
• Let two relational schema r(A1, A2,….., An) and s(B1, B2,….., Bm)
• Notation r x s
• Defined as: r x s = {t q | t r and q s}
– Assumption is that the attributes of r and s are disjoint(r Ոs = Φ)
• Each row of r combined with each row of s
• Result is a relation Q with degree n + m attributes:
• Q(A1, A2,….., An, B1, B2,….., Bm), in that order.
• Hence, if r has nr tuples (denoted as |r| = nr ), and s has ns tuples,
then r x s will have nr * ns tuples or │r x s│= nr * ns
• Also known as cross join
• It is not a meaningful operation unless it is associated with some
condition.
R1 X S1
A B C D E
α 1 α 10 a A B C D E
10 a
1
α 1 β 10 a 10 a
α 1 β 20 b 2 20 b
r 10 b
α 1 10 b
r x s: A=C(r x s) s
β 2 α 10 a
β 2 β 10 a A B C D E
β 2 β 20 b α 1 α 10 a
β 2 20 b β 2 β 10 a
β 2 β 20 b
Dr. Pradyumna Kumar Tripathy 27
Cartesian Product or Cross Product
A B C D E
α 1 α 10 a A B C D E
10 a
1
α 1 β 10 a 10 a
α 1 β 20 b 2 20 b
r 10 b
α 1 10 b
r x s: A=C(r x s) s
β 2 α 10 a
β 2 β 10 a A B C D E
β 2 β 20 b α 1 α 10 a
β 2 20 b β 2 β 10 a
β 2 β 20 b
Dr. Pradyumna Kumar Tripathy 28
Cartesian Product or Cross Product
stud result
ID NAME ID1 CGPA
1 XYZ 1 9.2
2 PQR 2 8.5
Stud X result
ID NAME ID1 CGPA
1 XYZ 1 9.2
1 XYZ 2 8.5
Not Valid
2 PQR 1 9.2
2 PQR 2 8.5
Write the relational algebra expression to find student ID, name and CGPA.
Ans: πNAME,ID,CGPA (σ(ID = ID1)(stud x result) ) ID NAME CGPA
1 XYZ 1 9.2
2 PQR 2 8.5
2 PQR 2 8.5
π NAME (σcgpa > 9.0( stud × result) )
σcgpa >9.0( stud × result)
stud × result
Dr. Pradyumna Kumar Tripathy 30
stud result
ID NAME ID1 CGPA
1 XYZ 1 9.2
2 PQR 2 8.5
• The correct relational algebra expression to find the name of those student whose
CGPA is >9.0 is
Ename
Smith
Blake
When the join condition c contains only comparison operator = then such type of
join is called as EQUIJOIN.
Ex.
The result of equi join we have one or more pair of attributes that have identical
values in every tuple.
Dr. Pradyumna Kumar Tripathy 37
•Consider the following relational schema
Emp(eno,ename,dno,sal)
Dept(deptno,dname,dloc)
Que-Write the relational algebra expression to find the name and salary of all the
employees who belong to MCA department. emp
eno ename sal dno
1- Emp dno =deptno Dept 1 Smith 5000 10
Q R(A,B,C,D) * S(C,D,E)
The implicit join condition includes each pair of attributes with
the same name, “AND” ed together: R.C=S.C AND R.D=S.D. Result
keeps only one attribute of each such pair: Q(A,B,C,D,E)
Example:
r = (A, B, C, D)
s = (E, B, D)
t = (A, B, C, D, E)
t= r s is defined as:
r.A, r.B, r.C, r.D, s.E (r.B = s.B r.D = s.D (r x s))
Dr. Pradyumna Kumar Tripathy 40
emp emp.deptno = dept.deptno dept
r
s r *s
Q) Find the name, loan no and loan amount of all customers who have a loan at the bank
Q) Find names of all customers who have both a loan and deposit account at the bank.
s = σ
•Theta can be any general Boolean expression on r (r x s)
the attributes of R and S: θ
• R.Ai <S.Bj AND (R. Ak=S. Bl OR R. Ap<S. Bq)
Equi join is special type of θ join where join condition uses only = as the comparison
operator.
The left outer join operation keeps every tuple in the first or left relation R in R S; if
no matching tuple is found in R, then the attributes of S in the natural join result are filled
or “padded” with null values.
The left outer join operation, keeps every tuple in the second or right relation S in the
result of R S. if no matching tuple is found in S, then the attributes of R in the natural
join result are filled or “padded” with null values.
keeps all tuples in both the left and the right relations when no matching tuples are
found, padding them with null values as needed.
loan * borrower
loan_number branch_name amount customer_name
L-170 Downtown 3000 Jones
L-230 Redwood 4000 Smith
R RꓴS
S
A B
A B A B
α 1
α 2 α 1
α 2
β 3 α 2
β 2
β 2
β 3
Smita
110 Drew 110 Drew
Byran
112 Smita
Write the relational algebra expression to find name of those customer who
have account or loan or both.
ΠName (Account) ꓴ ΠName (Loan)
Write the relational algebra expression to find name of those customer who
have both an account and loan.
ΠName (Account) ՈΠName (Loan)
Write the relational algebra expression to find name of those customer who
have but not taken a loan.
ΠName (Account) − ΠName (Loan)
Query 2: Find the name of sailors who have reserved boat number 2
Π sname (σbid = 2(Sailors * Reserve))
Query-3 Find the names of sailors who have reserved a red boat.
Πsname (σcolor = ‘Red’ (Boats *Reserve * Sailors) )
Query-4 Find the colors of the boats reserved by sailor ‘xyz’
Πcolor (σsname = ‘xyz’ (Sailors * Reserve * Boats) )
x(a1,a2,a3,a4) emp 68
Dr. Pradyumna Kumar Tripathy
RENAME OPERATION (ρ)
OR
Write relational algebra expression to display name of all employees and their
supervisors.
y ← ρd (emp)
z ← π emp.Fname,emp.Mname,emp.Lname,y.Fname,y.Mname,y.Lname (emp emp.ssn = y.SuperSSn y )
F , F ,...,
1 2 Fn
(E )
– where, E is any relational-algebra expression
– each of F1, F2, …, Fn are arithmetic expressions involving
constants and attributes in the schema of E.
Example :
Given relation Employee (emp_name, SSN, desgn, basic_sal, TA)
Find the total salary of each employee:
emp_name, (basic_sal + TA) (Employee)
Write relational algebra expression to retrieve name and annual salary of employees.
ρ(ename,Annual Salary) (Π (ename, 12 * sal) (emp))
Syntax :
G1 ,G2 ,…,G
E is any relational-algebra expression
– G1, G2 …, Gn is a list of attributes known as grouping attributes (can be empty)
– Each Fi is an aggregate function
– Each Ai is an attribute name
• The operator is pronounced as script F.
• The resulting relation has the grouping attributes plus one attribute for
each element in the function list.
• However, NULL values are not considered in the aggregate function
Dr. Pradyumna Kumar Tripathy 75
– g max( Salary) (EMPLOYEE) retrieves the maximum salary value
from the EMPLOYEE relation
– g min( Salary)(EMPLOYEE) retrieves the minimum Salary value
from the EMPLOYEE relation
– g sum( Salary)(EMPLOYEE) retrieves the sum of the Salary from
the EMPLOYEE relation
– g count( SSN),avg(Salary) (EMPLOYEE) computes the count (number)
of employees and their average salary
7
7
3
10
27
If no grouping attributes are specified, the functions are applied to all the tuples
in the relation, so the resulting relation has a single tuple only.
Dr. Pradyumna Kumar Tripathy 77
• Write the relational algebra expression to find
the maximum salary of employees.
emp
eno sal ename deptno
1 1000 xyz d1
2 5000 pqr d2
3 7000 abc d1
deptno max(sal)
deptno g max(sal) (emp) d1 7000
d2 5000
E3 John 25000 4
E4 Jenifer 43000 4
E5 Rames 38000 5
h
E6 Joyce 25000 5
E7 Ahmad 25000 4
E8 James 55000 1
g count(SSN),avg(salary) (employee)
Schema :
Query 1 :
Solution :
Schema :
Query 3 :
Solution :
– Deletion
– Insertion
– Updating
Delete emp smith record from emp(eno, ename, sal, dno) relation
emp ← emp – σ ename = “smith” (emp)
Delete all employees record whose salary >= 5000nfrom emp(eno, ename, sal, dno)
relation
emp ← emp – σ sal ≥ 5000s (emp)
Solution : A Account)
customer_name, branch_name (depositor *
Result A B
Retrieve the names of employees who work on all the projects that ‘John
Smith’ works on.
SMITH ← σ Fname=‘John’ AND Lname=‘Smith’ (EMPLOYEE)
SMITH_PNOS ← πPno(WORKS_ON Essn=Ssn SMITH)
SSN_PNOS ← πEssn, Pno(WORKS_ON)
y ← SSN_PNOS ÷ SMITH_PNOS
RESULT ← π Fname, Lname (y * EMPLOYEE)
• Find the names and cities of residence of all employees who work for
First Bank Corporation.
works)
• Find the names of all employees in this database who live in the
same city as the company for which they work.
• Πperson-name (employee * works * company)
• Q4
{S | SSailors
BBoats (RReserves
Find all sailors S such that… (S.sid = R.sid
For all tuples B in Boats… B.bid = R.bid))}
There is at least one tuple in Reserves…
showing that sailor S has reserved B.
Dr. Pradyumna Kumar Tripathy 126
Example
• Write the trc query to retrieve the name and address of all employees
who work for the ‘Research’ department.
– {t.FNAME, t.LNAME, t.ADDRESS | t ϵ EMPLOYEE AND ( d) ( d ϵ
DEPARTMENT AND d.DNAME=‘Research’ AND d.DNUMBER=t.DNO) }
– In above query, t is the only free variable;
– If a tuple satisfies the conditions specified in the query, the attributes
FNAME, LNAME, and ADDRESS are retrieved for each such tuple.
– The conditions t ϵ EMPLOYEE and d ϵ DEPARTMENT specify the range
relations for t and d.
– The condition d.DNAME = ‘Research’ is a selection condition and
corresponds to a SELECT operation in the relational algebra, whereas
the condition d.DNUMBER = t.DNO is a JOIN condition.
• Write the trc query to retrieve every project located in ‘Stafford’, list the
project number, the controlling department number, and the
department manager’s last name, birth date, and address.
• {p.Pnumber, p.Dnum, m.Lname, m.Bdate, m.Address | p ϵ PROJECT AND m
ϵ EMPLOYEE AND p.Plocation=‘Stafford’ AND ((∃d)(d ϵ DEPARTMENT AND
p.Dnum=d.Dnumber AND d.Mgr_ssn=m.Ssn))}
• Retrieve the employee’s first and last name and the first and last name
of his or her supervisor
– {e.Fname, e.Lname, s.Fname, s.Lname | e ϵ EMPLOYEE AND s ϵ
EMPLOYEE AND e.Super_ssn=s.Ssn}