0

I am trying to get the flights no whose seats are more 0. I have 3 table in MySQL

1. Sector
2. Flights
3. Aircraft

please see the image for structure of table in sequence of 1,2 and 3 enter image description here

I am writting this SQL

select * from aircraft 
where aircrafttypeID=
       (select aircrafttypeID 
            from sector,flights 
             where source like 'Kolkata' 
             and destination like 'Ahmedabad' 
             and sector.sectorID=flights.sectorID) 
and bseats>0

This query is giving error -

Subquery returns more than 1 row

because subquery is retuning multiple flight numbers. So I need some help how can get those Flights Number whose seats are more than 0

3
  • 4
    Try replacing = with in ... Commented Jun 14, 2013 at 8:09
  • @MeherzadThanks I forgot IN yes its done Commented Jun 14, 2013 at 8:16
  • @Pheonix answer is a better fix for you in my view. The join is better and cleaner than the subquery to use in this case. Commented Jun 14, 2013 at 8:32

2 Answers 2

3

Try this

SELECT  *
FROM    aircraft
WHERE   aircrafttypeID IN ( SELECT   aircrafttypeID
                            FROM     sector ,
                                     flights
                            WHERE    source LIKE 'Kolkata'
                                     AND destination LIKE 'Ahmedabad'
                                     AND sector.sectorID = flights.sectorID
                     )
    AND bseats > 0
Sign up to request clarification or add additional context in comments.

Comments

2

Just a suggestion. You can use join instead on subquery.

select * 
from aircraft a 
inner join flights f
on a.aircrafttypeID=f.aircrafttypeID
inner join source s 
on s.sectorID=f.sectorID
and s.source LIKE 'Kolkata'
and s.destination LIKE 'Ahmedabad'
and a.bseats>0

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.