By submitting your personal information, you agree to receive emails regarding relevant products and special offers from TechTarget and its partners. You also agree that your personal information may be transferred and processed in the United States, and that you have read and agree to the Terms of Use and the Privacy Policy.

Flights(flight#,from,to)

Its data may include:

('A1','Washington','Paris')
Flight A1 from Washington to Paris
('A2','Toronto','London')
('A2','London','Dubai')
Flight A2 from Toronto to Dubai with
a stop in London
('A3','Toronto','Paris')
('A3','Paris','Dubai')
('A3','Dubai','Tehran')
Flight A3 from Toronto to Tehran
with stops in Paris and Dubai.

We may have multiple stops. The question is:
Write a query that gets from (departure) and to (destination) cities and lists all flight numbers that travel from destination to departures without regard to number of stops during flight. For example for from='Toronto' and to='Tehran' give 'A3.' We are also permitted to use DB2 SQL extension if required.

I believe the key to this problem is knowing that the origin of a particular flight will be the only city at which no legs of that flight will arrive. On the same token, the final destination of a flight will be a city from which no legs depart. If these conditions were not the case, it would be in a situation such as one where a flight might loop back to the city of its origin. In this case, more information would be required in order to determine which city was the origin. For this example, we will assume the aforementioned conditions, that is, that flights will not loop back to their origins. Here is our table with its data:

This can be extended to find the final destinations for the flights by selecting all those legs where the ToCity is not found as a FromCity in any other legs. By using a self-join to combine these two conditions together, we get our solution:

0 comments

E-Mail

Username / Password

Password

By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy