question

uraza avatar image
uraza asked

Simple SQL query from Relations.

If I have these relations: - Races(**raceId**, description) - Results(***horseName, raceId***, finishTime, prizeMoney) - Horses(**horseName**, *motherName, fatherName*) - Owners(***personName, horseName***) - Persons(**personName**) and I want to find the name of all owners that own horses who have not participated in a race. This is my solution: select personName from Owners where horseName not in (select horseName from Results); however according to the answer sheet they do like this: select distinct personName from Owners natural join Horses left outer join Results on Horses.horseName = Results.horseName where prizeMoney is null;
sqlrelations
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

·
Squirrel avatar image
Squirrel answered
There are any ways to get the desire result. "NOT IN" is one of them. Beside that you can also use NOT EXISTS or LEFT OUTER JOIN. It really depends on what exactly it is asking for. For your query, you will get duplicate personName in the result as one person may own more than one horse that does not participate in a race. Adding DISTINCT to your query will fix that.
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.