Homework 2 - Rutgers CS

CS 336 Homework 2
1. Problem 3.8 on page 96 in your textbook.
2. Problem 3.14 on page 97 in your textbook.
3. Problem 3.19 on page 97 in your textbook.
4. The schema for the movie database is as follows:
Customers
CustID
LastName
Inventory
TapeID
MovieID
Movies
MovieID
MovieName
MovieSupplier
SupplierID
MovieID
FirstName
Price
Orders
OrderID
SupplierID
MovieID
Copies
Rentals
CustomerID
TapeID
CkoutDate
Duration
Suppliers
SupplierID
SupplierName
Write out SQL statements for the following 10 queries about the movie database.
1) Which movies are supplied by "Joe's House of Video" or "Video Warehouse"?
2) Which movie was rented for the longest duration (by any customer)?
3) Which suppliers supply all the movies in the inventory? (Hint: first get a list of the
movie suppliers and all the movies in the inventory using the cross product. Then
find out which of these tuples are invalid.)
4) How many movies in the inventory does each movie supplier supply? That is, for
each movie supplier, calculate the number of movies it supplies that also happen to be
movies in the inventory.
5) For which movies have more than 4 copies been ordered?
6) Which customers rented "Fatal Attraction 1987" or rented a movie supplied by "VWS
Video"?
7) For which movies are there more than 1 copy in our inventory? (Note that the
TapeID in inventory is different for different copies of the same MovieID)
8) Which customers rented movies for 5 days or more?
9) Which supplier has the cheapest price for the movie "Almost Angels 1962"?
10) Which movies aren't in the inventory?
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.
Team (name: varchar(30), city : varchar(20))
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 (playerID : integer, gameID: integer, points : integer, assists : integer,
rebounds : integer)
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 ascending
3. List all distinct (player name, team name) pairs of players and their teams, where the
player name contains "Mark" and the team is located in a city whose name starts with "B"
or "C" or "L". (ORDER BY Player.name, Team.name) Hint: You'll need to use the 'LIKE'
predicate operator.
select list: Player.name, Team.name
ordering: Player.name ascending, Team.name ascending
4. For each city that has at least one NBA team, list the city name and the height of the
tallest player playing the "Center" position in that city. (ORDER BY Team.city).
select list: Team.city, max_center_height
ordering: Team.city ascending
5. List the names, heights, and weights for all players that have not scored more than 19
points or collected more than 9 rebounds in any of their teams' home games. (ORDER
BY Player.name. Player.height)
select list: Player.name, Player.height, Player.weight
ordering: Player.name ascending, Player.height ascending
6. List the names and teams of all players that have averaged at least 10 points for their
teams' home games thus far, but have not scored 10 points or more in any of their teams'
away games. The result should be ordered by team names, and within each team, player
names should be presented in descending order.
select list: Player.team, Player.name
ordering: Player.team ascending, Player.name descending
7. List the IDs, names, and teams for all players who have scored at least 15 points in
every home game for their team. Do not output players whose teams have not played any
home games. (ORDER BY Player.playerID)
select list: Player.playerID, Player.name, Player.team
ordering: Player.playerID
8. List the names of all teams with a winning percentage of 60% or better at home
games. Output the team name (calling it 'team'), and the number of home wins and losses.
(ORDER BY team)
select list: team, num_of_home_wins, num_of_home_losses
ordering: team
9. For each win of the "ChicagoBulls", list the game ID and the name(s) of player(s) who
scored the maximum number of points for the Bulls in that win, along with that
maximum number of points. (ORDER BY Game.gameID, Player.name)
select list: gameID, Player.name, max_num_of_points
ordering: gameID, Player.name
10. List the names of all teams that have a winning record thus far in the season (total
number of wins > total number of losses). (ORDER BY team_name)
select list: team_name
ordering: team_name ascending