SQL - Questions
SQL - Questions
Consider a Horse-Show Database which contains the following tables and sample
data.
ENTRY HORSE
Event_id Horse_id place Horse name colour sire dam born died gender
0101 101 1 _id
0101 102 2 101 Flash white 201 301 1990 S
0101 201 3 102 Star brown 201 302 1991 M
0101 301 4 201 Boxer grey 401 501 1980 S
0102 201 2 301 Daisy white 401 502 1981 M
0103 102 3 302 Tinkl brown 401 501 1981 1994 M
0201 101 1 401 e
Snow white 1976 1984 S
0301 301 2 501 y
Blueb grey 1975 1982 M
0401 102 7 502 ell
Sally white 1974 1987 M
0501 102 1 EVENT
0501 301 3 PRIZE
Event_id show_id event_na judge_id
0101 01 me
Dressage 01
0102 01 Jumping 02
0103 01 Led in 01 event_co place prizemoney
0201 02 Led in 02 de
0101 1 $120.00
0301 03 Led in 01 0101 2 $60.00
0401 04 Dressage 04 0101 3 $30.00
0501 05 Dressage 01 0102 1 $10.00
0502 05 Flag and 02 0102 2 $5.00
SHOW Pole 0103 1 $100.00
0103 2 $60.00
show_i show_na show_hel show_address
0103 3 $40.00
d
01 me
Dubbo d
7/5/95 23 Wingewarra
0201 1 $10.00
St, Dubbo
02 Young 9/13/95 13 Cherry Lane, 0201 2 $5.00
Young 0401 1 $1,000.00
03 Castle Hill 5/4/96 Showground 0401 2 $500.00
Rd, Castle Hill 0401 3 $250.00
04 Royal 4/21/96 PO Box 13, 0501 1 $10.00
Easter GPO Sydney 0501 2 $5.00
05 Dubbo 7/1/96 17 Fitzroy St,
Dubbo
Problems
2.1 List the event_id and event_name for each event, together with the name of
the judge who is judging it. (hint - join the event and judge tables)
2.2 List the name of each horse who has been awarded a first place. Do not
display the name of a horse more than once.
2.3 For each event that a horse has entered. List the name of the horse together
with the name of the event, and its placing in that event (if any).
2.4 For each event, list the name of the event, the name and address of the show
that it is in, and the name and address of the judge who is judging it.
2.5 For all events at the show named “Dubbo”, list the date of the show, the
judge name and the event name.
2
2.6 For each event with a first prize, list the event name and the first prize
money.
2.7 List event_id, horse_id, place and money. Sort the results into order on
event_id then horse_id.
2.8 For each event with a first prize, list the show name, event name and first
prize money.
2.9 List the horse name, show name, event name and place for all horses in
events judged by “Green”.
2.10 List the event_id and event name for all events entered by Star or Flash and
judged by Smith from Melbourne.
Problems
3.1 List the name of each event type, and the number of times that event is
recorded as having been on.
3.2 List the name of each show, and the number of different times it has been
held. List the show which has been held most often first.
3.3 List the number of judges having each name.
3.4 For each show, list the show name, date held and the number of events at
that show.
3.5 For each horse that has won a prize, list the horse name, horse_id and total
prize money won.
3.6 For each horse which has won total prize money of at least $110, list the
horse name, horse_id and total prize money won.
3.7 Produce a table similar to that below, showing, for first and second places,
the number of times each prize was given, the total prize money awarded,
the greatest prize, the smallest prize, and the average prize. (The attribute
names in your solution may be different).
3.8 List the horse_id and horse name for each horse that has been placed in the
top 2 more than once.
3.9 List the name of each horse, and the number of times it has competed in an
event judged by Smith of Melbourne.
3
Section 4 - Dates
Problems
4.1 List the name of each show, together with the date that it was held. Format
the date into a readable format.
4.2 List the name of each event contested at a show held in 1996.
4.3 List the name of each horse, the name of the event and the show that the
event was in, and its age at the time of the event Subtracting the year that
the horse was born in, from the year that the show was held in will give a
reasonable approximation for its age at the time.
4.4 List the name of each event that has been held in July, together with the
number of times it was held in that month.
Problems
5.1 Using subqueries only (without products of any kind), list the name of every
judge who has judged an event at the show with an id of ‘01’.
5.2 Using subqueries only (without products of any kind), list the name of every
judge who has judged an event at the Dubbo show in 1995.
5.3 Using subqueries only (without products of any kind), list the event_id and
event name for all events entered by Star or Flash and judged by Smith from
Melbourne.
5.4 Using subqueries only (without products of any kind), list the name of each
judge who does not live in Melbourne, and who has judged an event entered
by any horse with a gender of ‘S’.
5.5 Repeat the previous query using only products.
5.6 List the name and id of each horse, together with one of the following text
values, as appropriate, “Alive” or “Dead”. Sort by horse_id.
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.
4
Section 6 - Other types of Join
INNER JOIN
LEFT OUTER JOIN
RIGHT OUTER JOIN
Problems
6.1 Re-do problem 2.1 using an INNER JOIN instead of a product.
6.2 Re-do problem 2.7 using an INNER JOIN instead of a product.
6.3 Re-do problem 2.10 using an INNER JOIN instead of a product.
6.4 Re-do problem 3.6 using an INNER JOIN instead of a product.
6.5 For each horse with a recorded sire (father), list the name of the horse, its year
of birth, and the name of its sire. Sort into order on year of birth.
6.6 List the name of the sire (father) of the horse called “Flash”.
6.7 For each white mare with a white dam (mother), list the name of the horse and
its dam.
6.8 For each horse that has a recorded paternal grandsire (sire of its sire), list the
names of the horse, of its sire and of its paternal grandsire.
6.9 For each event with at least three prizes, list the event_id and the prize money
available for first, second and third prize on one line. Order the table by
event_id. The results will appear as follows, although your attribute names
may be different:
Event_Id First Second Third
101 120 60 30
6.10 For each horse that has been entered in the same event as its sire (father), list
the names of the two horses and of the event.
6.11 For each judge, list the judge_id, Judge_name and the event_id of each event
they have judged. Include judges who have not judged any event (Hint - use
LEFT OUTER JOIN.)