Due: 11:59 pm on 12/1/2026
For all of these queries, follow strict SQL rules with respect to attributes in the GROUP BY clause.
Write a single query for each problem. Name is a single field with nameFirst and nameLast concatenated.
You will need the following as subqueries.
Subquery 1 - This query creates a directed acyclic graph such that the nodes are players who played for the Detroit Tigers in 2013 and hit at least 1 home run. There is an edge, represented as a row, from player A to player B if B hit the most home runs less than A. For example, Miguel Cabrera hit the most home runs and Prince Fielder hit the second most. Therefore, there is a row with cabremi01 and fieldpr01.
SELECT b0.playerid, b0.b_HR, b1.playerid, b1.b_HR
FROM batting b0, batting b1
WHERE b0.teamid = b1.teamid AND b0.yearid=b1.yearid AND b0.teamid = 'DET' AND b0.yearid = 2013 AND b1.b_HR =
(SELECT MAX(b_HR)
FROM batting b2
WHERE b2.teamid = b0.teamid AND b2.yearid = b0.yearid AND b2.b_HR > 0 AND b0.b_HR > b2.b_HR)
ORDER BY b1.playerid;
Subquery 2 - This query creates a directed acyclic graph such that the nodes are players who played for the Detroit Tigers in 2013 and hit at least 1 home run. There is an edge, represented as a row, from player A to player B if A hit more home runs than B. For example, Miguel Cabrera hit the most home runs, so there is a row with Miguel Cabrera and every other player who hit at least 1 home run.
SELECT b0.playerid, b0.b_HR, b1.playerid, b1.b_HR FROM batting b0, batting b1 WHERE b0.teamid = b1.teamid AND b0.yearid=b1.yearid AND b0.teamid = 'DET' AND b0.yearid = 2013 AND b1.b_HR > 0 AND b0.b_HR > b1.b_HR;
Subquery 3 effectively combines two instances of the graph in subquery 1 - one for 2013 and one for 2014, by merging the edges between the two graphs. This means I can now have cycles in my directed graph. For example, there is an edge from Jackson to Avila in 2013, from Avila to Davis in 2014 and from Davis to Jackson in 2014.
WITH RECURSIVE g AS (
SELECT b0.playerid as src, b1.playerid as dest
FROM batting b0, batting b1
WHERE b0.teamid = b1.teamid AND b0.yearid=b1.yearid AND b0.teamid = 'DET' AND (b0.yearid = 2013 OR b0.yearid = 2014) AND b1.b_HR =
(SELECT MAX(b_HR)
FROM batting b2
WHERE b2.teamid = b0.teamid AND b2.yearid = b0.yearid AND b2.b_HR > 0 AND b0.b_HR > b2.b_HR)
Your assignment should be a text file meeting the following criteria