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

SQL Practice Sheet

Relational Schema Diagram Practice

Uploaded by

anik.additional
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

SQL Practice Sheet

Relational Schema Diagram Practice

Uploaded by

anik.additional
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

SQL Practice Sheet

*Some questions may have more than one correct solutions

Consider the following Relational Schema:

1. Write appropriate SQL queries for the following questions. For each question write a
single query.

a. Retrieve all information of all players where the team name has the term “City” in
it or if the team name starts with an “A”.
b. Retrieve all information of the players who have the highest skill level.
c. Retrieve all information of the players who have the highest skill level for each
Team.
d. Retrieve the average score of each host team. The average score should be printed
as “host_average”.
e. Retrieve the list of cities all teams belong to. There should not be any duplicate
cities in the output.
f. Retrieve the number of teams in each city sorted by city in alphabetical order.
g. Retrieve all game information where the guest team has a higher score than the
host team in the year 2023.
h. Retrieve the maximum score of each guest team if the team has played as guest at
least 3 times. The maximum score should be printed as “guest_max”.
i. Retrieve the Game information of the last 10 games.
j. Retrieve all game information for games played on the first recorded date. The
information should be sorted by host team name.
k. Retrieve the information of players who play in the “center forward” position and
whose skill level is higher than every single player who plays in the “center back”
position.

2. Write appropriate SQL queries for the following questions.

a. Retrieve all information for each team and the captain’s information of each team.
b. Retrieve the host team - name, city, score, guest team - name, city, score and game
date of all games held in 2022.
c. Retrieve the Name, Position and Skill_Level of players who are members of a
Host_Team and also have the highest skill level.
d. Retrieve the Team Name, Captain Name, Phone and Captain’s Injury Records of
all Teams. The information should be sorted by the captain’s phone in ascending
order.

SOLUTIONS:

Answer 1:

a. Select * from Players where Team_Name Like “%City%” or Team_Name like “A%”;
b. Select * from Players where Skill_Level = (Select max(Skill_Level) from Players);
c. Select * from Players where (Team_Name, Skill_Level) in (Select Team_Name,
max(Skill_Level) from Players group by Team_Name);
d. Select Host_Team, Avg(Score) As Host_Average from Games group by Host_Team;
e. Select Distinct City from Teams;
f. Select City, count(*) from Teams group by City Order by City;
g. Select * from Games where Guest_Score>Host_Score and Date Between “2023-01-01”
and “2023-12-31”
h. Select Guest_Team, max(Guest_Score) from Games group by Guest_Team having
count(*)>=3;
i. Select * from Games order by Date Desc Limit 10;
j. Select * from Games where Date = (Select min(Date) from Games) order by Host_Team;
k. Select * from Players where Position = “center forward” and Skill_level > all (Select
Skill_Level from Players where Position = “center back”);

Answer 2:
a. Select * from Teams T Inner Join Players P on T.Captain_Phone = P.Phone
b. Select Host_Team, T1.City, Host_score, Guest_Team, T2.City, Guest_Score, Date from
(Teams T1 Inner Join Games G on T1.Name = G.Host_Team) Inner Join Team T2 on
T2.Name = G.Guest_Team) where Date Between “2022-01-01” and “2022-12-31”
c. Select P.Name, Position, Skill_Level from Teams T, Players P, Games G where T.Name =
P.Team_Name and T.Name = G.Host_Team and Skill_Level = (Select max(Skill_Level)
from Players)
d. Select T.Name, P.Name, P.Phone, injuryRecords from (Teams T inner join Players P on
T.captain_phone = P.Phone) inner Join Players_injuryRecords PI on PI.Phone = P.Phone
order by P.Phone

PRACTICE CHECKLIST:
SELECT
From
Where - condition clause, row wise
Or, And, Not
Between
Distinct
Is, In
Like, %, _
Order By, Asc, Desc
Limit
Aggregate functions- min, max, sum, avg, count
Group By
Having - condition clause, group wise
Subqueries
Any/All
Joins

You might also like