来自关系的SQL查询

时间:2014-08-20 23:05:22

标签: sql

如果我有这些关系:

  • 种族( raceId ,说明)
  • 结果( horseName raceId ,finishTime,prizeMoney)
  • 马匹( horseName motherName fatherName
  • 所有者( personName,horseName
  • 者(的 PERSONNAME

我希望找到所有拥有没有参加比赛的马匹的所有者的名字。

这是我的解决方案:

select personName
from Owners
where horseName not in (select horseName from Results);

然而根据答题纸,他们喜欢这样:

select distinct personName
from Owners natural join Horses
left outer join Results on Horses.horseName = Results.horseName
where prizeMoney is null;

1 个答案:

答案 0 :(得分:0)

你很好。他们的答案在很多方面都很糟糕 - 做一个自然的连接是不必要的,对性能不利,等等。

但是,您可能希望包含不同的内容,因为您撰写的查询可以(并且可能会)导致同一所有者被多次返回(对于他们拥有的每匹马而言,它都没有参加比赛)。