100% found this document useful (1 vote)
152 views3 pages

Relational Algebra Exercises and Solutions

Uploaded by

ishitaag2003
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
152 views3 pages

Relational Algebra Exercises and Solutions

Uploaded by

ishitaag2003
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Relational algebra – solved exercise

Question:
Consider the following relational database schema consisting of
the four 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

Π fid (σ pid = 123 (booking) ⨝ σ dest = “Chennai” ^ fdate <


for flights to Chennai before 06/11/2020.

06/11/2020 (flight))
d) Find the passenger names for passengers who have

Π pname (passenger ⨝ booking)


bookings on at least one flight.

-------------------------------------------------------------------------------------------
----------
e) Find the passenger names for those who do not have
any bookings in any flights.

Π pid (booking)) ⨝ passenger)


Π pname ((Π pid (passenger) -

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)))


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

(σ fdate = 01/12/2020 ^ time = 16:00 (flight)) ∪ (σ fdate =


hours.

02/12/2020 ^ time = 16:00 (flight))


-------------------------------------------------------------------------------------------
----------

i) Find the agency names for agencies who do not have

Π aname (agency ⨝ (Π aid (agency) – Π aid (σ pid =


any bookings for passenger with id 123.

123 (booking)))
--------------------------------------------------------------------------------------------
---------

j) Find the details of all male passengers who are


associated with Jet agency.

‘Jet’ (passengers ⨝ booking ⨝ agency))


Π [Link], pname, pcity (σ pgender = “Male” ^ aname =

Common questions

Powered by AI

To determine agencies located in the same city as a particular passenger using relational algebra, use a natural join between the agency relation and the result of a selection operation on the passenger relation. The process involves matching the city field of agencies with the city of the specific passenger. The relational algebra query is: Π aname (agency ⨝ acity = pcity (σ pid = 123 (passenger))). This extracts agency names where the agency's city matches the city of the passenger with ID 123.

To identify all flights to a specific destination using relational algebra, a selection operation is performed on the flight relation with the condition set to match the desired destination. For flights destined for New Delhi, the query is: σ destination = “New Delhi” (flight). This filters the set of all flights to retrieve only those where the destination attribute is 'New Delhi'.

The expression to find flights from a specific source to a specific destination using relational algebra involves using the selection operation to filter the flight relation based on both source and destination criteria. For example, to find flights from Chennai to New Delhi, the query used is: σ src = “Chennai” ^ dest = “New Delhi” (flight). This applies a selection on the flight details where the source is 'Chennai' and the destination is 'New Delhi'.

To retrieve flight details scheduled on either or both of two specific dates at a given time using relational algebra, the set union operator is used. This combines flights for both dates into a single result set. The expression is: (σ fdate = 01/12/2020 ^ time = 16:00 (flight)) ∪ (σ fdate = 02/12/2020 ^ time = 16:00 (flight)). This gives all flights scheduled at 16:00 hours either on 01/12/2020, 02/12/2020, or both.

To find flights that operate on both of two specific dates at a certain time using relational algebra, the set intersection operator is used. This logic is implemented by first selecting flights that operate on each of the required dates and times, creating two separate sets, and then intersecting these sets to find common elements. The query is: (σ fdate = 01/12/2020 ^ time = 16:00 (flight)) ∩ (σ fdate = 02/12/2020 ^ time = 16:00 (flight)). This selects flights scheduled at 16:00 hours on both 01/12/2020 and 02/12/2020.

To determine male passengers associated with the 'Jet' agency, relational algebra can be used to filter and join multiple tables. First, select male passengers, then join this with booking and agency tables, and apply selection for the agency 'Jet'. The query is: Π passengers.pid, pname, pcity (σ pgender = “Male” ^ aname = ‘Jet’ (passengers ⨝ booking ⨝ agency)). This first filters male passengers, joins with bookings and agency, and further filters results to include only those associated with the agency named 'Jet'.

To find passengers who do not have any bookings, you can use the difference operator in relational algebra to identify passengers from the passenger relation who do not appear in the booking relation. The relational algebra query is: Π pname ((Π pid (passenger) - Π pid (booking)) ⨝ passenger). This subtracts the set of passenger IDs in the booking table from the passenger table and joins it back with the passenger table to get the corresponding names.

To find agencies without bookings for a specific passenger using relational algebra, perform a difference operation between all agencies and those that have bookings for the given passenger. Specifically, derive the agency IDs that have bookings for the passenger, and subtract these from all agency IDs. The query is: Π aname (agency ⨝ (Π aid (agency) – Π aid (σ pid = 123 (booking)))). This yields agency names that are not in the set obtained from bookings by passenger ID 123.

To find flight numbers for a passenger with a specific ID traveling to a destination before a given date using relational algebra, you would need to perform a natural join between the booking and flight relations and apply the necessary selection conditions. In the given example, this is achieved by: Π fid (σ pid = 123 (booking) ⨝ σ dest = “Chennai” ^ fdate < 06/11/2020 (flight)). This query first selects bookings for passenger ID 123, joins them with flights where the destination is Chennai, and where the flight date is before 06/11/2020.

To extract passenger names with bookings using relational algebra, you need to perform a natural join between the passenger and booking tables and then project on the passenger name attribute. The query for this operation is: Π pname (passenger ⨝ booking). This results in a list of passenger names who have made at least one booking.

You might also like