SQL Lab Exercise

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Information Management 2 SQL Exercise

Using the South African Tour database tables in Figure 1 illustrated below, write SQL
statements to answer questions 1 to 5.
TOUR
Tour-Id Tour-Name Tour-Duration Fee
U89 Ushaka Marine World 2 R250
B45 Botha Hill 3 R300
S23 Suncoast Casino 2 R50

GUIDE
Guide-Id Guide-Name Guide-Address Date-Hired
209 Sipho Luthuli Avondale Road, Morningside 02 February 2005
356 Sarah Benson 12a Fir Avenue, Pinetown 06 June 2010
560 Lungile Nzama 23 Currie Road, Berea 15 October 2004

SCHEDULE
Tour-Id Guide-Id Schedule-Time Schedule-Date
S23 209 09h00 15 February 2019
S23 560 12h00 21 March 2019
B45 356 10h00 03 April 2019

ATTRIBUTE / FIELD NAME DATA DECLARATION


Tour-Id CHAR(3)
Guide-Id CHAR(3)
Schedule-Time TIMEDATE
Schedule-Date DATE
Figure 1: Database Tables: South African Tours

1. Create a table name SCHEDULE. Enforce primary key and foreign key constraints
and all changes to the primary keys must be updated on foreign keys. Make sure all
other tables have been created.

2. Enter the data values for all tables.


3. Change the guide date-hired to 15 September 2003 for guide-id 560.
4. Write the SQL code to list all Guide_Name that start with the letter ‘S’.
5. Write a query to produce total fee owed if a tourist wanted to tour all places.
6. Write a query to produce most expensive tour.
7. Write a query to produce the least expensive tour.
8. List the Tour_Name, Tour_Duration, Fee, Scheduled_Time for Tour_Id ‘S23’.
9. List the Tour-id, Schedule-Time, and Fee in descending order of Fee.
10. Remove all occurrences of Tour_Id ‘U89’.

2
Information Management 2 SQL Exercise

Solution
Using the South African Tour database tables in Figure 1 illustrated below, write SQL
statements to answer questions 1 to 5.
TOUR
Tour-Id Tour-Name Tour-Duration Fee
U89 Ushaka Marine World 2 R250
B45 Botha Hill 3 R300
S23 Suncoast Casino 2 R50

GUIDE
Guide-Id Guide-Name Guide-Address Date-Hired
209 Sipho Luthuli Avondale Road, Morningside 02 February 2005
356 Sarah Benson 12a Fir Avenue, Pinetown 06 June 2010
560 Lungile Nzama 23 Currie Road, Berea 15 October 2004

SCHEDULE
Tour-Id Guide-Id Schedule-Time Schedule-Date
S23 209 09h00 15 February 2019
S23 560 12h00 21 March 2019
B45 356 10h00 03 April 2019

ATTRIBUTE / FIELD NAME DATA DECLARATION


Tour-Id CHAR(3)
Guide-Id CHAR(3)
Schedule-Time TIMEDATE
Schedule-Date DATE
Figure 1: Database Tables: South African Tours

1. Create a table name SCHEDULE. Enforce primary key and foreign key constraints
and all changes to the primary keys must be updated on foreign keys. Assume other
tables have been created already.

create table TOUR (


Tour_Id char(3) not null primary key,
Tour_Name char(25),
Tour_Duration numeric(5),
Tour_Fee numeric(9,2))

create table GUIDE (


Guide_Id char(3) not null primary key,
Guide_Name char(25),
Guide_Address char(35),
Date_Hired date)

CREATE TABLE SCHEDULE (

3
Information Management 2 SQL Exercise

Tour-Id CHAR(3) NOT NULL UNIQUE,


Guide-Id CHAR(3) NOT NULL,
Schedule-time TIME
Schedule-date DATE,
PRIMARY KEY (Tour-Id, Guide_Id),
FOREIGN KEY (Tour-Id) REFERENCES TOUR (Tour-Id) ON
UPDATE CASCADE,
FOREIGN KEY (Guide-Id) REFERENCES GUIDE (Guide-Id) ON
UPDATE CASCADE;

2 Enter the data values for all tables.


insert into TOUR values ('U89', 'Ushaka Marine World', 2,
250.00),('B45', 'botha hill', 3, 300.00),('S23', 'Suncoast Casino',
2, 50.00)

insert into GUIDE values ('B09', 'Sipho Luthuli', 'Avondale Road,


Morningside', '15-Feb-2005'),('356', 'Sarah Benson', '12a Fir
Avenue, Pinetown', '06-Jun-2010'),('560', 'Suncoast Casino', '23
Currie Road, Berea', '15-Oct-2004');

insert into SCHEDULE values ('S23', 'B09', '09:00', '15-Feb-


2019'),('U89', '560', '12:00', '21-Mar-2019'),('B45', '356',
'10:00', '03-Apr-2019');

insert into SCHEDULE values ('S23', '356', '10:00', '16-Feb-2019')


3. Display the data for all tables.
select * from TOUR
select * from GUIDE
select * from SCHEDULE

4. Change the guide date-hired to 15 September 2003 for guide-id 560.

UPDATE GUIDE
SET Date_Hired = '15-Sep-2003'
WHERE Guide_Id = '560';

5. Write the SQL code to list all guide names that start with the letter ‘S’.
select * from GUIDE where Guide_Name LIKE 'S%';

6. Write a query to produce total fee owed if a tourist wanted to tour all places.

select sum(Tour_Fee) as totalfee


from TOUR;

4
Information Management 2 SQL Exercise

7. Calculate the average tour fee.


select AVG(Tour_Fee) as tourfee
from TOUR;

8. Write a query to produce the most expensive tour fee.


select Max(Tour_Fee) as highest
from TOUR;

9. Write a query to produce the least expensive tour fee.


select MIN(Tour_Fee) as cheapest
from TOUR;

10. How many tours are there?


select count(Tour_ID) as NUMofTours
from TOUR;

11. List the Tour-id, Schedule-Time, and Fee in descending order of Fee.
SELECT Tour.Tour_Id, Schedule_Time, Tour_Fee
FROM SCHEDULE, TOUR
WHERE SCHEDULE.Tour_Id = TOUR.Tour_Id
ORDER BY Tour_Fee DESC;

12. Remove all occurrences of Tour_Id ‘U89’.


DELETE FROM Tour WHERE Tour-Id = ‘S23’;

You might also like