0% found this document useful (0 votes)
51 views3 pages

SQL Example 2

The document contains SQL statements to: 1) Create three tables (Horse, Race, Entry) with primary and foreign keys to record horse and race data. 2) Insert sample data into the tables. 3) Write queries to select, filter, aggregate and join data from the tables. 4) Add an index, create a view, and stored procedure to further interact with the data.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views3 pages

SQL Example 2

The document contains SQL statements to: 1) Create three tables (Horse, Race, Entry) with primary and foreign keys to record horse and race data. 2) Insert sample data into the tables. 3) Write queries to select, filter, aggregate and join data from the tables. 4) Add an index, create a view, and stored procedure to further interact with the data.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 3

/*Q.3.

1 Create tables*/

/*create Horse table*/

CREATE TABLE Horse(


HorseID INT NOT NULL,
Name VARCHAR(50),
PRIMARY KEY(HorseID)
);

/*create Race table*/

CREATE TABLE Race(


RaceID INT NOT NULL,
Name VARCHAR(50),
Dat DATE,
PRIMARY KEY(RaceID)
);

/*create Entry table*/

CREATE TABLE Entry(


EntryID INT NOT NULL,
HorseID INT,
RaceID INT,
PRIMARY KEY(EntryID),
FOREIGN KEY(Horse) REFERENCES Horse(Horse),
FOREIGN KEY(RaceID) REFERENCES Race(RaceID)
);

/*Q.3.2 Insert the following data into tables*/

/*insert data into Horse table*/

INSERT INTO Horse VALUES(1, 'Bold Ruler');


INSERT INTO Horse VALUES(1, 'Phantom');

/*insert data into Race table*/

INSERT INTO Race VALUES(1, 'Most Important Race 2019', '2019-07-05');


INSERT INTO Race VALUES(2, 'Some Other Race 2020', '2020-11-19');

/*insert data into Entry table*/

INSERT INTO Entry VALUES(1, 1, 1);


INSERT INTO Entry VALUES(2, 1, 2);
INSERT INTO Entry VALUES(3, 2, 3);

/*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*/

CREATE UNIQUE INDEX Index_Race_Name


ON Race(Name);
/*Q.3.8 Create a view called getthisyearsraces that queries the names and dates of the
races that have a date that is in the current year*/

CREATE VIEW getthisyearsraces


AS
SELECT Name,
Dat
FROM Race
WHERE YEAR(DAT) = YEAR(GETDATE());

/*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*/

CREATE PROCEDURE count_horse_races @Horse_Name VARCHAR(50)


AS
SELECT COUNT(Entry.EntryID)
FROM Entry
JOIN Horse
ON Entry.HorseID = Horse.HorseID
WHERE Horse.Name = @Horse_Name
GO;

EXEC count_horse_races @Horse_Name = 'Bold Ruler';

You might also like