View the step-by-step solution to:

CREATE TABLE Horse (horse_id INTEGER PRIMARY KEY , horse_name CHAR ( 30 ), horse_colour CHAR ( 30 ) horse_sire INTEGER , horse_dam INTEGER ,...

CREATE TABLE Horse
(horse_id INTEGER PRIMARY KEY,
 horse_name CHAR(30),
 horse_colour CHAR(30)
 horse_sire INTEGER,
 horse_dam INTEGER,
 horse_born INTEGER.
 horse_died INTEGER,
 horse_gender CHAR(2)
);

CREATE TABLE Entry
(horse_id INTEGER,
 entry_place INTEGER
);
INSERT INTO Horse (horse_id,horse_name,horse_colour,horse_sire,horse_dam,horse_born,horse_gender)
VALUES (101,'Flash','white',201,301,1990,'S');
INSERT INTO Horse (horse_id,horse_name,horse_colour,horse_sire,horse_dam,horse_born,horse_gender)
VALUES (102,'Star','brown',201,302,1991,'M');
INSERT INTO Horse (horse_id,horse_name,horse_colour,horse_sire,horse_dam,horse_born,horse_gender)
VALUES (201,'Boxer','grey',401,501,1980,'S');
INSERT INTO Horse (horse_id,horse_name,horse_colour,horse_sire,horse_dam,horse_born,horse_gender)
VALUES (301,'Daisy','white',401,502,1981,'M');
INSERT INTO Horse (horse_id,horse_name,horse_colour,horse_sire,horse_dam,horse_born,horse_died,horse_gender)
VALUES (302,'Tinkle','brown',401,501,1981,1994,'M');
INSERT INTO Horse (horse_id,horse_name,horse_colour,horse_dam,horse_born,horse_died,horse_gender)
VALUES (401,'Snowy','white',301,1976,1984,'S');
INSERT INTO Horse (horse_id,horse_name,horse_colour,horse_dam,horse_born,horse_died,horse_gender)
VALUES (501,'Bluebell','grey',301,1975,1982,'M');
INSERT INTO Horse (horse_id,horse_name,horse_colour,horse_dam,horse_born,horse_died,horse_gender)
VALUES (502,'Sally','white',301,1974,1987,'M');

INSERT INTO Entry (horse_id,entry_place)
VALUES (101,1);
INSERT INTO Entry (horse_id,entry_place)
VALUES (102,2);
INSERT INTO Entry (horse_id,entry_place)
VALUES (201,3);
INSERT INTO Entry (horse_id,entry_place)
VALUES (301,4);
INSERT INTO Entry (horse_id,entry_place)
VALUES (201,2);
INSERT INTO Entry (horse_id,entry_place)
VALUES (102,3);
INSERT INTO Entry (horse_id,entry_place)
VALUES (101,1);
INSERT INTO Entry (horse_id,entry_place)
VALUES (301,2);
INSERT INTO Entry (horse_id,entry_place)
VALUES (102,7);
INSERT INTO Entry (horse_id,entry_place)
VALUES (102,1);
INSERT INTO Entry (horse_id,entry_place)
VALUES (301,3);
  5.7 List the name of each horse, together with the number of events in which it has been awarded first place. Horses who have never won a first place should also be listed with a count of zero. Sort the list into descending order on number of wins.
The output of this query should be as shown below:

Horse  Number of wins

Flash          2
Star           1
Bluebell       0
Boxer          0
Daisy          0
Sally          0
Snowy          0
Tinkle         0

This is what I have tried in my first attempt:

SELECT DISTINCT Horse.horse_name ,COUNT(Entry.entry_place) AS [Number of Wins]
FROM Horse, Entry 
WHERE Horse.horse_id = Entry.horse_id 
AND entry_place =1 OR (NOT entry_place = 1)
GROUP by Horse.horse_name
ORDER BY  Entry.entry_place DESC;


Top Answer

SELECT h_name AS Horse, IFNULL (wins, 0 ) AS 'Number of wins' FROM ( SELECT... View the full answer

capture12.PNG

Sign up to view the full answer

Why Join Course Hero?

Course Hero has all the homework and study help you need to succeed! We’ve got course-specific notes, study guides, and practice tests along with expert tutors.

-

Educational Resources
  • -

    Study Documents

    Find the best study resources around, tagged to your specific courses. Share your own to gain free Course Hero access.

    Browse Documents
  • -

    Question & Answers

    Get one-on-one homework help from our expert tutors—available online 24/7. Ask your own questions or browse existing Q&A threads. Satisfaction guaranteed!

    Ask a Question
Ask a homework question - tutors are online