0% found this document useful (0 votes)
3 views4 pages

DBMS - Worksheet 8 & 9 - Answers

The document contains SQL commands for creating and managing a database with tables for Players, Activities, and Participation records. It includes commands for inserting data, updating records, and querying various statistics and relationships between the tables. Additionally, it demonstrates the use of foreign keys and cascading deletes in the database structure.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views4 pages

DBMS - Worksheet 8 & 9 - Answers

The document contains SQL commands for creating and managing a database with tables for Players, Activities, and Participation records. It includes commands for inserting data, updating records, and querying various statistics and relationships between the tables. Additionally, it demonstrates the use of foreign keys and cascading deletes in the database structure.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

----------Worksheet 8-----------

----------Question 1--------

create table Player(


ID int Primary key,
Pname varchar (20),
Age int,
School varchar (50));

create table Activity(


AID varchar (50) primary key,
Cost float );

create table Participate(


ID int,
AID varchar (50),
constraint fk_ID foreign key (ID) references Player (ID) on delete cascade,
constraint fk_AID foreign key (AID) references Activity (AID) on delete cascade);

----------Question 02 -------------

insert into Player values (100, 'Ran', 5 , 'Museaus college ');


insert into Player values (101, 'Ruwan', 5 , 'Royal college');
insert into Player values (102, 'Methmi', 12, 'Museaus college');
insert into Player values (107, 'Ran', 6, 'Royal college');
insert into Player values (108, 'Ruwan',18 , 'Thurstan college');
insert into Player values (110, 'Kevin', 17, 'Royal college');
insert into Player values (114, 'Ridi', 12, 'Museaus college');
insert into Player values (121, 'Dev',14 , 'Royal college');
insert into Player values (122,'Dev', 12, 'Thurstan college');

insert into Activity values ('Swimming', 3000);


insert into Activity values ('Golf',5000);
insert into Activity values ('Sailing', 6000);
insert into Activity values ('Tennis', 5000);
insert into Activity values ('Cricket', 3000);
insert into Activity values ('Badminton', 3500);

insert into Participate values (122,'Tennis');


insert into Participate values (100,'Swimming');
insert into Participate values (102,'Tennis');
insert into Participate values (110,'Swimming');
insert into Participate values (108,'Sailing');
insert into Participate values (122,'Swimming');
insert into Participate values (102,'Swimming');
insert into Participate values (121,'Swimming');
insert into Participate values (107,'Sailing');
insert into Participate values (121,'Golf');

------- Question 4---------


alter table Activity add Coach varchar (20);

update Activity
set Coach = case
when AID = 'Swimming' then 'Kevin'
when AID = 'Golf' then 'Jagath'
when AID = 'Sailing' then 'Dev'
when AID = 'Tennis' then 'Manju'
when AID = 'Cricket' then 'Peter'
when AID = 'Badminton' then 'Ravi'
end;

----------Qeustion 5----------
update Player set Age = 6 where ID = 101;
update Player set Pname = 'Deva' where ID = 121;

-----------Question 6 ----------
delete from Player where ID =107;

-----//Since on delete cascade is implemented on all child tables whatver gets


deleted from the parent table
---will be deleted from the child tables as well

-----------Question 7 ----------
insert into Player values (123, 'Jim', 12, 'Thurstan College');
insert into Participate values (123,'Sailing');
insert into Participate values (123,'Swimming');

-----------Question 8 ----------
a) select distinct Pname from Player;
b) select * from Activity where cost<5000;
c) select * from Player where Pname like '[G-R]%' order by Pname desc;
d) select * from Player where Pname like '_e%';
e) select * from Player where Age<20 and Age>10;
f) select max(Age) as MaxAge from Player;
g) select School, count(*) as NumbeOfStudents from Player group by School;
h)
select Player.Pname, Player.School, Activity.AID, Activity.Coach from Player
join Participate on Player.ID = Participate.ID
join Activity on Participate.AID = Activity.AID;

i) select Player.Pname, Player.Age from Player


join Participate on Player.ID = Participate.ID
where AID = 'Swimming';

j)select Player.School from Player


join Participate on Player.ID = Participate.ID
where Participate.AID = 'Tennis';

k) select avg(Player.Age) as AVGage from Player


join Participate on Player.ID = Participate.ID
where Participate.AID = 'Sailing';

l) select AID, count(*) as NumberOfplayer from Participate group by AID;

m) select AID, count(*) as NumberOfplayers from Participate


group by AID having count(*)>2 order by NumberOfplayers desc;

n) select Player.ID , sum(Activity.Cost) as SumOFAmount from Player


join Participate on Player.ID = Participate.ID
join Activity on Participate.AID = Activity.AID
group by Player.ID having sum(Activity.Cost)>5000;

o) select Activity.Coach, count(Participate.ID) as NoOfPlayers from Activity


left join Participate on Activity.AID = Participate.AID
group by Activity.Coach
order by NoOfPlayers desc;

p)
select A.Coach, sum(A.Cost *count(PT.ID)) as TotalEarnings
from Activity A
left join Participate PT on A.AID = PT.AID
group by A.Coach;

SELECT Activity.Coach, SUM(Activity.Cost * Participate.ParticipantCount) AS


TotalEarnings
FROM Activity
LEFT JOIN
(
SELECT AID, COUNT(ID) AS ParticipantCount
FROM Participate
GROUP BY AID
)
Participate ON Activity.AID = Participate.AID
GROUP BY Activity.Coach;

select * from Participate


select * from Player
select * from Activity

----------Worksheet 9-----------

---------------Question 01--------------
insert into Participate values (114, 'Tennis');
insert into Participate values (121,'Tennis');
insert into Participate values (102, 'Sailing');

---------------Question 02--------------
a) select Pname from Player where Age = (select min(Age) from Player);

b) select AID, count(ID) as NoOfPlayers from Participate


where AID in ( select AID from Activity where Cost>3000 and Cost <6000)
group by AID;

c) select * from Player where School = (select School from Player where Pname =
'Methmi');

d) select * from Player where Age > (select avg(Age) from Player);

e) select AID from Activity where Cost = (select max(Cost) from Activity);
f) select AID from Activity where Cost = (select min(Cost) from Activity);

g) select Coach from Activity where Cost = (select max(Cost) from Activity);

h) select * from Player where Age = (select Age from Player where ID = 100)
or Age = (select Age from Player where ID = 107);

i) select * from Activity where Cost != (select Cost from Activity where AID =
'Sailing')
and Cost != (select Cost from Activity where AID = 'Swimming');

j) select AID, count(*) as NumberOFPlayers from Participate group by AID order by


NumberOFPlayers asc;

k) select AID, count(*) as NumberOFPlayers from Participate group by AID order by


NumberOFPlayers desc;

-------Another method to do J and K


SELECT AID, COUNT(*) AS NumberOfPlayers
FROM Participate
GROUP BY AID
HAVING COUNT(*) = (
SELECT MIN(PlayerCount)
FROM (
SELECT COUNT(*) AS PlayerCount
FROM Participate
GROUP BY AID
) AS SubQuery
);

SELECT AID, COUNT(*) AS NumberOfPlayers


FROM Participate
GROUP BY AID
HAVING COUNT(*) = (
SELECT max(PlayerCount)
FROM (
SELECT COUNT(*) AS PlayerCount
FROM Participate
GROUP BY AID
) AS SubQuery
);

You might also like