0% found this document useful (0 votes)
2 views

SQL Practice Problem2 Sol

The document provides a comprehensive SQL practice problem related to a relational database for the Super Baseball League, detailing the schema, data insertion, and various SQL queries. It includes the creation of tables for teams, coaches, players, and their affiliations, along with specific SQL commands for data retrieval and manipulation. Additionally, it presents nested SQL queries for advanced data extraction based on specific criteria.

Uploaded by

tfaizanvr
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

SQL Practice Problem2 Sol

The document provides a comprehensive SQL practice problem related to a relational database for the Super Baseball League, detailing the schema, data insertion, and various SQL queries. It includes the creation of tables for teams, coaches, players, and their affiliations, along with specific SQL commands for data retrieval and manipulation. Additionally, it presents nested SQL queries for advanced data extraction based on specific criteria.

Uploaded by

tfaizanvr
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

National University of Computer and Emerging Sciences, Lahore Campus

Course: Database Systems


Program: BS(Computer Science)
Instructor: Muhammad Ishaq Raza

Practice Problem: SQL (2) - SOLUTION

Part (1)

Question 1:
Consider the following relational database for the Super Baseball League. It keeps track of teams in the
league, coaches and players on the teams, work experience of the coaches, bats belonging to each
team, and which players have played on which teams.

Note the following facts about this environment:

• The database keeps track of the history of all the teams that each player has played on and all
the players who have played on each team.
• The database only keeps track of the current team that a coach works for.
• Team number, team name, and player number are each unique attributes across the league.
• Coach name is unique only within a team (and we assume that a team cannot have two coaches
of the same name).
• Serial number (for bats) is unique only within a team.
• In the Affiliation table, the Years attribute indicates the number of years that a player played on
a team; the batting average is for the years that a player played on a team.

Team (TeamNum, TeamName, City, Manager)


Coach (TeamNum, CoachName, Address )
WorkExperience (TeamNum, CoachName, ExperienceType, YearsExperience)
Bats( TeamNum, SerialNum, Manufacturer)
Player (PlayerNum, PlayerName, Age)
Affiliation (PlayerNum, TeamNum, Years, BattingAvg)
a) Write appropriate SQL DDL statements for declaring the above relational schema. Specify the
appropriate keys and referential integrity constraints and actions. Also populate some data in tables.
Ans:
--Team (TeamNum, TeamName, City, Manager)
create table Team(
Teamnum integer primary key,
Teamname varchar(20) NOT Null ,
City varchar(20),
Manager varchar(20),
)
insert into Team values(1, 'Shaheen','London','Ali');
insert into Team values(23,'Janbaz','Paris', 'Mohsin');
insert into Team values(3,'Phurteely', 'London','Ghafoor');
insert into Team values(4,'Rangeely', 'London','mubashir');
insert into Team values(5,'Josheely','Berlin','Raza');

--Coach (TeamNum, CoachName, Address )


create table Coach(
Teamnum integer,
Coachname varchar(20),
Adress varchar(20),
primary key (Teamnum,Coachname),
foreign key (Teamnum) REFERENCES Team(Teamnum),
)

insert into Coach values(1, 'Ashraf','London');


insert into Coach values(2,'Usman','Paris');
insert into Coach values(3,'Naimat', 'London');

--WorkExperience (TeamNum, CoachName, ExperienceType, YearsExperience)


create table WorkExperience(
Teamnum integer,
Coachname varchar(20),
ExperienceType varchar(20),
YearsExperience integer,
primary key (Teamnum,Coachname,ExperienceType),
foreign key (Teamnum,Coachname) REFERENCES Coach(Teamnum,Coachname) on delete cascade on update
cascade,
)
insert into WorkExperience values(1, 'Ashraf','college coach',10);
insert into WorkExperience values(23,'Usman','School coach',5);
insert into WorkExperience values(3,'Naimat', 'uni coach',7);

--Bats( TeamNum, SerialNum, Manufacturer)


create table Bats(
Teamnum integer,
Serialnum integer,
Manufacturer varchar(20)
primary key (Teamnum,serialnum),
foreign key (Teamnum) REFERENCES Team(Teamnum) on delete cascade on update cascade,
)
insert into Bats values(1,101,'adidas');
insert into Bats values(2,2002,'ismaels');
insert into Bats values(3,30003, 'sports');

--Player (PlayerNum, PlayerName, Age)


create table Player(
Playernum integer primary key,
Playername varchar(20),
Age integer
)
insert into Player values(1,'Hafiz',19);
insert into Player values(2,'Mirani',25);
insert into Player values(3,'mohsin',24);

--Affiliation (PlayerNum, TeamNum, Years, BattingAvg)

create table Affliation(


Playernum integer,
Teamnum integer,
years integer,
batting_avg decimal(5,2),
primary key (Playernum,Teamnum),
foreign key (Teamnum) REFERENCES Team(Teamnum) on delete cascade on update cascade,
foreign key (Playernum) REFERENCES Player(Playernum) on delete cascade on update cascade,
)
insert into Affliation values(1, 1,10,5.2);
insert into Affliation values(2,2,5,5.3);
insert into Affliation values(3,3,6,7.3);

b) Write down SQL queries for each statement.


Ans:

1. Find the names and cities of all of the teams with team numbers greater than 15.

select city,Teamname
from Team
where Teamnum>15

2. List all of the coaches who have between 5 and 10 years of experience as college coaches (see
YEARSEXPERIENCE and EXPERIENCETYPE).

select *
from Coach join WorkExperience on Coach.Teamnum=WorkExperience.Teamnum
where WorkExperience.ExperienceType='college coach' and WorkExperience.YearsExperience between 5
and 10

3. Find the total number of years of experience of Coach Waqar on team number 23.

select sum(YearsExperience)
from WorkExperience
where Coachname='Waqar' and Teamnum=23

4. Find the number of different types of experience that Coach Waqar on team number 23 has.

select count(ExperienceType)
from WorkExperience
where Coachname='Waqar' and Teamnum=23

5. Find the total number of years of experience of each coach on team number 23.
select Coachname,count(ExperienceType)
from WorkExperience
where Teamnum=23
Group by Coachname

6. How many different manufacturers make bats for the league’s teams?

select count(Distinct Manufacturer)


from Bats

7. Assume that team names are unique. Find the names of the players who have played for the
Dodgers for at least five years (see YEARS in the AFFILIATION Table.)

select playername
from (Affliation join Team on Affliation.Teamnum=Team.Teamnum ) join Player on
Affliation.playernum=player.Playernum
where Teamname='Dodgers' and years>=5

8. Find the names of the league’s youngest players.

select Teamnum,Playername,min(Player.Age)
from player join Affliation on Player.Playernum=Affliation.Playernum
Group by Teamnum,Playername

9. Find the players who have been affiliated with more than two teams.

select playername,Count(Teamnum) as Teamcount


from player join Affliation on Player.Playernum=Affliation.Playernum
Group by Teamnum,playername
Having Count(Teamnum)>2

10. Find the players who are not affiliated with any team.

select playername
from player left join Affliation on Player.Playernum=Affliation.Playernum
where player.Playernum is NULL

11. For each player, find the number of teams they have been affiliated with in their career.

select playername,Count(Teamnum) as Teamcount


from player join Affliation on Player.Playernum=Affliation.Playernum
Group by Teamnum,playername

12. For each team, list the average batting average.


select Teamname,Batting_avg
from Team join Affliation on Team.Teamnum=Affliation.Teamnum
Group By Teamname,Batting_avg
Part (2)

Question 2:

Use schema of Q1 and write only nested SQL queries for the following questions.

1. Display the name of the teams such that their coach is Waqar.
2. Display the names of the players who are under 18.
3. Find the name of players such that their age is more than the average age. Sort the results in descending
order of age.
4. Assume that team names are unique. Find the total number of years of work experience of each
coach on the Dodgers, but include in the result only those coaches who have more than eight
years of experience.
5. Find the manufacturers who have made bats for all the teams in New York.

Ans:

---------------------------NESTED QUERIES---------------------
--1. Display the name of the teams such that their coach is Waqar.
select Teamname
from Team
where Teamnum in(select Teamnum from Coach Where Coachname='Waqar')

--2. Display the names of the players who are under 18.
select playername
from player
where playernum in (select playernum from player where player.Age<18)

--3. Find the name of players such that their age is more than
--the average age. Sort the results in descending order of age.
select playername
from player
where Age > (select Avg(Age) from player)
order by Age desc

--4. Assume that team names are unique. Find the total number of years
-- of work experience of each coach on the Dodgers, but include in the
-- result only those coaches who have more than eight years of experience

select Coach.Coachname,sum(WorkExperience.YearsExperience)
from (WorkExperience join Coach on WorkExperience.Teamnum=Coach.Teamnum ) join Team on
Coach.Teamnum=Team.Teamnum
where Teamname='Dodgers' and Team.Teamnum in(select WorkExperience.Teamnum from WorkExperience
where WorkExperience.YearsExperience>8)
Group by Coach.Coachname

--5. Find the manufacturers who have made bats for all the teams in New York.
select Manufacturer
from Bats
where Teamnum in
( select Teamnum
from Team
where Team.City='New York')
Question 3:
Product ( pid, name, price, category, maker-cid)
Purchase (buyer-ssn, seller-ssn, store, pid)
Company (cid, name, stock price, country)
Person (ssn, name, phone number, city)

Write only nested SQL queries for the following questions.

1. Display the name of products manufactured by ‘LG’


2. Find the name of products whose price is more than the average price range. Sort the results in descending
order of prices.
3. Display the name and company of all products made in Pakistan.
4. Display the name, phone of all buyers who buy products from ‘Ali’.
5. Create a query to display the name of products that have the same price as that of product id, pid=1.

Ans:

--Q3-1)

SELECT product.name
FROM Product
WHERE product.make_cid in (SELECT company.cid FROM company WHERE company.name='LG')

--Q3-2)

SELECT Product.name
FROM Product
WHERE price > (
SELECT AVG(price)
FROM Product
)
ORDER BY price DESC

--Q3-3)

SELECT product.name, company.name as Company


FROM Product
join company on company.cid = product.make_cid
WHERE product.make_cid in (SELECT c.cid FROM company c WHERE c.country='Pakistan')

--Q3- 4)

SELECT name, phonenumber


FROM Person JOIN Purchase ON ssn = buyerSsn
WHERE sellerSsn in (
SELECT ssn
FROM Person
WHERE name = 'Ali'
)

--Q3-5)
SELECT product.name as NameOfProducts
FROM Product
WHERE price in (SELECT product.price FROM Product WHERE product.pid = 1) AND product.pid = 1

You might also like