5. There are 4 relations in the schema, which are described below along with the
ID: 3914415 • Letter: 5
Question
5. There are 4 relations in the schema, which are described below along with their integrity constraints. Columns in the primary key are underlined.
Player(playerID: integer, name : varchar(50), position : varchar(10), height : integer, weight : integer, team: varchar(30))
Each Player is assigned a unique playerID. The position of a player can either be Guard, Center or Forward. The height of a player is in inches while the weight is in pounds. Each player plays for only one team. The team field is a foreign key to Team.
Each Team has a unique name associated with it. There can be multiple teams from the same city.
Game (gameID: integer, homeTeam: varchar(30), awayTeam : varchar(30), homeScore : integer, awayScore : integer)
Each Game has a unique gameID. The fields homeTeam and awayTeam are foreign keys to Team. Two teams may play each other multiple times each season. There is an integrity check to ensure homeTeam and awayTeam are different.
GameStats records the performance statistics of a player within a game. A player may not play in every game, in which case it will not have its statistics recorded for that game. gameID is a foreign key to Game. playerID is a foreign key to Player. Assume that two assertions are in place. The first is to ensure that the player involved belongs to either the involving home or away teams, and the second is to ensure that the total score obtained by a team (in Game) is consistent with the total sum (in GameStats) of individual players in the team playing in the game[2].
Write out SQL statements for the following 10 queries.
Note: You are asked to order the result of each query by some attribute(s) using keyword ORDER BY - you need to find out its usage by yourself.
?
1) Find the names of the shortest player(s) playing the "Guard" position for each team. (ORDER BY Players.name)
select list: Player.name
ordering: Player.name ascending
2) List the names of all players and the average number of points and rebounds for all games that they played in, ignoring players who did not play in any games. (ORDER BY Player.name)
select list: Player.name, average_points, average_rebounds
ordering: Player.name descending
Explanation / Answer
1) This is a basic query we just need the Player table to get the result :-
select name from Player where position = 'Guard' and height = (select min(height) from Player) order by name;
2) In this we require Player and GameStats table as the select list includes these columns :-
We will use both tables by cartesian product as we need players who have played the game and then using it will find the resultset :-
select p.name as name,avg(g.points) as average_points,avg(g.rebounds) as average_rebounds from Player p ,GameStats g where p.playerID = g.playerID group by p.name order by p.name desc
Here we used the group by clause to get the average of each player and used the where clause to get the player names who have played in one or more games.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.