SQL Practice Problem2 Sol
SQL Practice Problem2 Sol
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.
• 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.
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?
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
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.
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.
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)
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)
--Q3- 4)
--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