0

Tables:

  • PERSON (ssn, Name)
  • Has_Visited (ssn, city)
  • Belongs_To (City, country)

Based on following information, create this question:

What is the name of the persons that have visited the city "London"?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

2 Answers2

0
Select p.Name from person p,has_visited hs 
where p.ssn= hs.ssn and hs.city= 'LONDON'
Mohamad Shiralizadeh
  • 8,329
  • 6
  • 58
  • 93
akhil vangala
  • 1,043
  • 1
  • 10
  • 11
0

Just use the in operator:

SELECT name
FROM   person
WHERE  ssn IN (SELECT ssn
               FROM   has_visited
               WHERE  city = 'London');
Mureinik
  • 297,002
  • 52
  • 306
  • 350
  • I'm curious, why did you prefer this to join ? Is this a performance matter or just a preference ? – Jean-François Savard Feb 25 '15 at 17:40
  • 1
    It's a readability issue - this query declares that it's selecting each person (no more than) once, not some combination of people and visits. Consider if the schema would be extended to list **when** a person visited a city, and allow multiple visits. A query using a join would return that person twice. This one would continue functioning properly. – Mureinik Feb 25 '15 at 17:43