Relational ALgebra (Summer+Spring-2021)
Relational ALgebra (Summer+Spring-2021)
Relational ALgebra (Summer+Spring-2021)
Solution:
Relational algebra is a procedural query language, which takes
instances of relations as input and yields instances of relations as
output. It
uses
operators to perform queries. An operator can be either
unaryorbinary.
Thefundamentalo perationso frelationala lgebraa rea sfollows−
● Select(ROW)
● Project(COLUMN)
● Union
● Setdifferent
● Cartesianproduct
● Rename
ExampleTable:
Customer(Customer_name,Customer_Id,Cus
tomer_city,Customer_street,Total_Bill,Date)
Depositor(Customer_name,Branch_name,Ac
count_number,amount);
Borrower(Customer_name,amount,C_d,C-S)
;
CUSTOMERTABLE
Customer_ Customer_I Customer_ Customer_s Total_Bill Date
Name d City treet
1.Findouttheinformationofthecustomerwho
hasshoppedinthispresentyearfromtheshop.
Select*fromCustomer
hereDate BETWEEN1/1/2021AND
W
31/12/2021;
𝛔 Date>=1/1/2021 OR Date<=31/12/2021(Customer) ;
𝛔 ′1/1/2021′<=Date<=′31/12/2021′ (Customer)
SelectOperation(σ):
2.Showtheinformationofthecustomer
livinginSenatorstreetofBrooklyn.
Select* fromc ustomerWHERECustomer_City=‘Brooklyn’AND
Customer_Street=‘Senator’;
ProjectOperation(∏):
1.Showthenamesofthecustomers.
π Customer−name (Customer);
2.Showthename&city ofthecustomers.
π Customer−name,Customer−city (Customer);
1.FindouttheTotal_Billofallthecustomersinthecustomer
table.
SelectTotal_billfromCustomer;
π T otal −Bill (Customer);
2.FindouttheTotal_Billwiththecustomer'snamefromthe
customertable.
SelectCustomer_name,Total_BillfromCustomer;
π T otal−Bill,Customer−Name (Customer);
π T otal −Bill,Customer −N ame,Customer −city (Customer);
π
T otal−Bill,Customer−N ame,Customer−city,Customer−Address (Customer);
3.FindouttheCustomer’sBillwhichismorethan200$.
π T otal−Bill>200 (Customer);
4.FindouttheCustomer'scityeitherinBerlinorBrooklyn.
π Customer−city=′Berlin′ OR ′Brooklyn′ (Customer);
5.FindouttheIdofthecustomerwhichisin
between400to120.
π 120<=Id<=400 (Customer);
5.FindouttheCustomer’sName,Customer_IdWhose billis
within200to1000withthecityin“Brooklyn”or“Berlin”.
π Customer−N ame,Customer−id (
�� (200<=T otal−Bill<=1000) AN D (Customer−City=′Brooklyn′ OR ′Berlin′)
SELECTCustomer_Name(PROJECT)fromCustomer
WHERETotal_bill(SELECT)BETWEEN200AND400;
(PROJECT+SELECT)
SELECTTotal_BillfromCustomer
WHERETotal_billBETWEEN200AND400;(PROJECT)
π 200<=T otal−Bill<=1000 (Customer);
π Customer−N ame (�� 200<=T otal−Bill<=1000 (Customer));
6.FindouttheCustomer’sName&
CustomerIdWhose billwithin200to1000.
π Customer−Name,Customer−Id (�� 200<=T otal−Bill<=1000 (Customer));
ClassWork:
1.Findoutthecustomer’sName&Addresswhohas
shoppedmorethan300$inthepreviousmonth&liveseither
inBrooklynorMiami.
2.TheSupershopinfloridawantsasurveyontheregular
customer’slisttofindoutthecustomer'sId,Name&Address
whohasatotalbilllessthan500$andlives nearby
theshopandwritetheoutputfromthetablewithSQL&
RelationalAlgebra.
CLASSWORK(20/2/2021):
1.Findouttheemployeenamewhohasthe
maximumsalary.
SELECTEmployee_NameFROMCustomerWHERE
Employee-salaryIN(SELECTMAX(Employee_salary)fromCustomer);
π Employee−name (�� (Customer)) ;
??
1.FindouttheEmployeeNamewhoarelivinginthesamecountry.
SQL:
SelectEmployee_Namefrome mployee
GroupBYCountry;
Instance
RelationalAlgebra:
π Employee−name (�� A.Country=B.Country (Customer)) ;
π
Age (�� Country=′Bangladesh′ AND Employee−salary=1200 OR Employee−salary=1200 OR Employee−salary=1200 (Employee));
6.Findoutthee mployees e itherlivinginBangladeshorGermanyand
havingagenotmorethan35.
𝛔 C ountry=′Bangladesh ′ OR ′Germany′ AN D Age <=35 (Employee);
7.Findoutthee mployeese itherlivinginBangladeshandGermanyor
havingagenotmorethan35.
𝛔 C ountry=′Bangladesh′ AN D ′Germany′ OR Age <=35 (Employee);
8.Findoutthee mployeeinformationwhodoesliveinBangladeshorUSA.
𝛔 C ountry=′Bangladesh ′ OR ′U SA′ (Employee);
MIDTERMENDS
UNION:
A={1,2,3},B={a,b,3}
A-B={1,2}
B-A={a,b}
AUB={1,2,3,a,b}
BUA={1,2,3,a,b}
A∩B={3}
B∩A={3}
AXB={(1,a),(2,a),(3,a),(1,b),(2,b),(3,b),(1,3),(2,3),(3,3)}
Customer(Customer_name,Customer_Id,Customer_
city,Customer_street,Total_Bill,Date)
Depositor(Customer_name,Branch_name,Account_n
umber,amount);
Borrower(Customer_name,amount,C_d,C-S);
1.Findthenamesofcustomerswhohaveanaccountwiththe
bank.
π Customer −name (Customer) ∩
π Customer −name (Depositor);
2.Findthenameofthecustomerwhohasaloan.
πCustomer −name (Customer) ∩
π Customer −name (Borrower);
3.Findthenameofthecustomerswhohaveeitheraloanor
anaccountorboth.
πCustomer −name (Depositor) U
π Customer −name (Borrower);
4.Findthenameofthecustomerwhohasonlyadeposit
account.
( π Customer −name (Customer) ∩
π Customer −name (Depositor) )-
π Customer −name (Borrower);
Anna,Carl,Penny,Jenny.Marco,Jacob(Customer)
Carl,Penny,Jacob,Jenny(Depositor)
Anna,Marco,Carl,Penny(Borrower)
OUTPUT:
Jacob,Jenny
Customer(Cust_id,Cust_Name)
Account(Cust_id,Branch)
Loan(Cust_id,Branch)
1A 2NewYork
2B 2Berlin
3C 3Virginia
OUTPUT:B
i)CartesianProduct(Cust_id)
ii)SelectNewYork
iii)ProjectCustomer_Name
5.Findoutthenameofthecustomerswhohaveanaccountin
“Newyork”Branch.
π Customer −Name (�� Branch−Name=""NEW Y ORK"(
�� Customer.Cust−id=Account.Cust−id (Customer X Account));
6.Findthenamesofallcustomerswhohavealoanatthe
NewYorkbranchbutdonothaveanaccountatanybranch
ofthebank.
π Customer −Name (�� Branch−Name=""NEW Y ORK"(
�� Customer.Cust−id=Account.Cust−id (Customer X Loan)) −
π Customer −name (Account);
RelationalAlgebra:
Noticethefollowingschemas,
● Student(student_name,student_id,Address,email,phone,
Course_name,cgpa,teacher_name);
● Course(Course_name,Course_id,CreditHour,ContactHo
ur);
● Teacher(teacher_name,teacher_id,joiningDate,yearsOfe
xperience,Course_name);
Nowwritethefollowingrelationalalgebra,
1.ShowtheinformationofthestudentslivinginDhaka.
𝛔 Student.address="Dhaka"(Student) ;
2.Showthenameoftheteacherstakingthecourse
“CSE-235”.(Nestedquery)
π
T eacher.teacher −name (�� T eacher.Course−name="CSE−235"(T eacher));
3.Showthestudents'namesalongwiththeircgpa&student
id.
π Student.student−name,Student.student−id,Student.cgpa (Student);
4.Showthenameoftheteacherhavingexperienceover30
years.(nestedquery);
π T eacher.teacher −name (�� T eacher.experience>30 (T eacher));
5.Findoutthenameofthestudentswhohaveacourseof
credit3.0.
π Student.Student−name (Student) U
π Course.Course−name (�� Course.creditHour="3.00"(Course));
6.Findoutthenameoftheteacherwhohasastudent
residingin“Barisal”.
π
T eacher.teacher −name (�� Student.teacher −name==T eacher.teacher −name,Student.address=
( StudentXTeacher)
7.Findoutthenameofthecourseswhicharebeingteached
byteacher“X”.(Hometask)
8.Findoutthethenameoftheteacherwhodoesn’tteachany
subject.(Hometask)
CLASSWORK(4/4/2021(5/4/2021)):
Branch(branch_name,branch_city,assets)
Customer(customer_name,customer_street,
customer_city)
Account(account_number,branch_name,balance)
Loan(loan_number,branch_name,amount)
Depositor(customer_name,account_number)
Borrower(customer_name,loan_number)
SELECT:
1.Findouttheinformationofthecustomerwholivesin
“Dhaka”.
�� Customer.Customer −city="Dhaka"(Customer);
PROJECT:
1.Findoutthenamesofthecustomerswhohaveloans.
π Borrower.Customer −name (Borrower) ;
2.Findoutthenameofthecustomerwholivesin“Dhaka”.
π
Customer.Customer −name (�� Customer.Customer −city="Dhaka"(Customer));
3.Findouttheaccountbalancewithin500taka.
π Account.Balance>=500 (Account);
4 .Findouttheaccountinformation whosebalanceisnot
morethan1000taka.
�� Account.Balance>=1000 (Account);
5.Findouttheaccountbalancewhichisnotmorethan
1000taka.
π Account.Balance>=1000 (Account);
6.Findouttheaccountnumberwhichbalanceisnotmore
than1000taka.
π Account.Account−number (�� Account.Balance>=1000 (Account));
UNION:
1.Findouttheinformationofthecustomerwhohasaloan
andanaccountorboth.
π depositor.customer −name,depositor.account−number (depositor) U
π borrower.customer −name,borrower.loan−number (borrower) ;
2.Findoutthenameofthecustomerwhohasaloanandan
accountorboth.
π depositor.customer −name (depositor) U
π borrower.customer −name (borrower) ;
3.Findoutthebranchnameswherethecustomerhasaloan
andanaccountboth.
π Account.branch−name (Account) U
π Loan.branch−name (Loan) ;
CartesianProduct:
1.Findthenamesofallcustomerswhohavealoan
attheDhakabranch.
π
borrower.Customer−name ((�� loan.branch="Dhak a" (�� loan.loan−number==borrower.loan−number (loan X bor
2.Findoutthecustomernamewhohasbothaloanandan
accounton“DHAKA”branch.
[ 1U2 ]
3)Findoutthenameofthecustomerwhohasanaccount
onthe“Dhaka”branch.
𝜫 Depositor.Customer−name ( 𝞼
Account.Branch−name="Dhak a" (�� Account.Account−number==Depositor.Account−number (Depositor X Account))
SetDifference:
1.Findoutthebranchnameswherethecustomeronlyhasa
loannotanaccount.
A={1,2,3,4},B={2,3,6,7,8}
A-B={1,4}
B-A={6,7,8}
π Loan.Branch−name (Loan);
π
Loan.Branch−name (π Loan.Branch−name==Branch.Branch−name (Loan X Branch)) −
π Account.Branch−name (Account);
OR
π Loan.Branch−name (Loan) -
π Account.Branch−name (Account);
Loan→Khilgaon,Dhanmondi,Gulshan
Branch→Khilgaon,Gulshan,Badda,Banani,Mohakhali
Account→ Badda,Wari,Banani,Khilgaon
2.Findthenamesofallcustomerswhohavealoanatthe
Gulshanbranchbutdonothaveanaccountatanybranchof
thebank.
π Borrower.Customer −name (�� Loan.Branch−name="Gulshan"
((�� Loan.loan−number==Borrower.loan−number (Borrower X Loan)
)
- π Depositor.Customer−name (Depositor);
Rename:(MIN,MAX,AVG,SUM,COUNT)
1.Findthelargestaccountbalance.
π Account.Balance (
(�� Account.Balance < new.Balance (Account X ρ new (Account))
));{MAX}
π Account.Balance (
(�� Account.Balance > new.Balance (Account X ρ new (Account))
));[MIN]
π Account.Balance ( (�� Account.Balance = new.Balance+new2.Balance ((
Account X ρ new (Account) X ρ new2 Account) ));[SUM]