Relational Algebra Ex 1
Relational Algebra Ex 1
relation schemas:
passenger ( pid, pname, pgender, pcity)
agency ( aid, aname, acity)
flight (fid, fdate, time, src, dest)
booking (pid, aid, fid, fdate)
Answer the following questions using relational algebra queries;
a) Get the complete details of all flights to New Delhi.
σ destination = “New Delhi” (flight)
------------------------------------------------------------------------------------------------
-----
b) Get the details about all flights from Chennai to New Delhi.
σ src = “Chennai” ^ dest = “New Delhi” (flight)
------------------------------------------------------------------------------------------------
-----
c) Find only the flight numbers for passenger with pid 123 for flights to
Chennai before 06/11/2020.
Π fid (σ pid = 123 (booking) ⨝ σ dest = “Chennai” ^ fdate < 06/11/2020 (flight))
[Hint: Given conditions are pid, dest, and fdate. To get the flight id for a
passenger given a pid, we have two tables flight and booking to be joined with
necessary conditions. From the result, the flight id can be projected]
------------------------------------------------------------------------------------------------
-----
d) Find the passenger names for passengers who have bookings on at least
one flight.
Π pname (passenger ⨝ booking)
------------------------------------------------------------------------------------------------
-----
e) Find the passenger names for those who do not have any bookings in any
flights.
Π pname ((Π pid (passenger) - Π pid (booking)) ⨝ passenger)
[Hint: here applied a set difference operation. The set difference operation
returns only pids that have no booking. The result is joined with passenger
table to get the passenger names.]
------------------------------------------------------------------------------------------------
-----
f) Find the agency names for agencies that located in the same city as
passenger with passenger id 123.
Π aname (agency ⨝ acity = pcity (σ pid = 123 (passenger)))
[Hint: we performed a theta join on equality conditions (equi join) here. This
is done between details of passenger 123 and the agency table to get the valid
records where the city values are same. From the results, aname is projected.]
------------------------------------------------------------------------------------------------
-----
g) Get the details of flights that are scheduled on both dates 01/12/2020 and
02/12/2020 at 16:00 hours.
(σ fdate = 01/12/2020 ^ time = 16:00 (flight)) ∩ (σ fdate = 02/12/2020 ^ time = 16:00 (flight))
[Hint: the requirement is for flight details for both dates in common.
Hence, set intersection is used between the temporary relations
generated from application of various conditions.]
------------------------------------------------------------------------------------------------
-----
h) Get the details of flights that are scheduled on either of the dates
01/12/2020 or 02/12/2020 or both at 16:00 hours.
(σ fdate = 01/12/2020 ^ time = 16:00 (flight)) ∪ (σ fdate = 02/12/2020 ^ time = 16:00 (flight))
------------------------------------------------------------------------------------------------
-----
i) Find the agency names for agencies who do not have any bookings for
passenger with id 123.
Π aname (agency ⨝ (Π aid (agency) – Π aid (σ pid = 123 (booking)))
------------------------------------------------------------------------------------------------
-----
j) Find the details of all male passengers who are associated with Jet
agency.
Π passengers.pid, pname, pcity (σ pgender = “Male” ^ aname = ‘Jet’ (passengers ⨝ booking ⨝ agency))
[Hint: To get the link between passengers and agency, we need to join all
three tables passengers, booking, and agency with necessary condition. Here,
agency links both passengers and agency. As we have performed natural join
operation between all three tables, the degree of the result will consist of all
attributes from all the three tables. Hence, we project only passengers details
as these are mentioned as required.]
First Schema
Suppliers(sID, sName, address)
Parts(pID, pName, colour)
Catalog(sID, pID, price)
1. Find the names of all red parts.
Answer: ΠpName(σcolour=“red”Parts)
2. Find all prices for parts that are red or green. (A part may have different prices from