2

There are two tables named

Passenger (ID_psg, name)
Pass_in_trip (trip_no, date, ID_psg, place)

There can be several passengers bearing the same first name and surname (for example, Bruce Willis). My goal is to find the names of different passengers that ever travelled more than once occupying seats with the same number. It's exercise 63 given at sql-ex.ru

My Query at first try was

SELECT p.name
FROM pass_in_trip  pt LEFT JOIN passenger p
    ON pt.id_psg = p.id_psg
GROUP BY pt.id_psg, pt.place,p.name
HAVING COUNT(*) > 1

I am unable to find out the reason why it did not return the desired result. I know it can be done using subquery but I want to know where I am going wrong. The place column represent the seat number. You can take sql-server as database. Desired result should look something like this

  name
--------
Bruce Willis        
Nikole Kidman       
Mullah Omar  
13
  • Do you get a result when you remove the group by? Commented Jan 20, 2017 at 17:35
  • What do your data sets look like? Or rather, what's your input/output expected to be? Commented Jan 20, 2017 at 17:36
  • 1
    Which field represents "seats". Commented Jan 20, 2017 at 17:38
  • 2
    Also - it looks like sql-ex.ru has terms in there that specifically ask you not to seek outside help or look for solutions elsewhere.... Commented Jan 20, 2017 at 17:40
  • 1
    An inner join should be sufficient for this. Commented Jan 20, 2017 at 17:40

1 Answer 1

1

This will get you the people who are in the pass_in_trip table more than once

SELECT p.name
FROM pass_in_trip  pt 
INNER JOIN passenger p
    ON pt.id_psg = p.id_psg
GROUP BY p.name
HAVING COUNT(*) > 1

This will get you the people with the same name who have the same seat more than once. Note that this will include two people with different ids who have the same name.

SELECT p.name
FROM pass_in_trip  pt 
INNER JOIN passenger p
    ON pt.id_psg = p.id_psg
GROUP BY pt.place,p.name
HAVING COUNT(*) > 1

This will get you the individuals with different id_psgs who have the same seat (so two different people named Bruce Willis who each have used the same seat multiple times who show up in the results and you would see Brice Willis twice).

SELECT p.name
FROM pass_in_trip  pt 
INNER JOIN passenger p
    ON pt.id_psg = p.id_psg
GROUP BY pt.id_psg,pt.place,p.name
HAVING COUNT(*) > 1

Now when trying to debug code of this nature, it is always best to show all of the items in the group by in the select part of the query even if you intend to remove them later. Had you done this:

SELECT pt.id_psg,pt.place,p.name
FROM pass_in_trip  pt 
INNER JOIN passenger p
    ON pt.id_psg = p.id_psg
GROUP BY pt.id_psg,pt.place,p.name
HAVING COUNT(*) > 1

Then you would have seen why two Bruce Willis's show up and you would have known what the problem was.

Sign up to request clarification or add additional context in comments.

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.