Professional Documents
Culture Documents
SQL Example 2
SQL Example 2
1 Create tables*/
/*Q.3.3 Query all the names of all the horses in the database, sorted from Z to A*/
SELECT Name
FROM Horse
ORDER BY Name DESC;
/*Q.3.4 Query the races where the date of the race is from 2019-01-01 to 2019-12-
31*/
SELECT *
FROM Race
WHERE Dat BETWEEN '2019-01-01' AND '2019-12-31';
/*Q.3.5 Determine how many races each horse entered. Include the Horse ID and the
number of races that the horse participated in*/
SELECT HorseID,
COUNT(EntryID)
FROM Entry
GROUP BY HorseID;
/*Q.3.6 Query the name of the horse and the name of the race for all the race entries
recorded in the database*/
SELECT Horse.Name.
Race.Name
FROM Horse
JOIN Entry
ON Horse.HorseID = Entry.HorseID
JOIN Race
ON Entry.RaceID = Race.RaceID;
/*Q.3.7 Add a unique index on the Name column in the Race table*/
/*Q.3.9 Create a stored procedure called count_horse_races. It shourld take the name
of the horse as input and datermine the number of races it has entered*/