Hey folks, so I’ve been struggling with this off-platform project for a few days, in part because nobody has the same code to find the answers. Which is cool. More than one way to skin the code. However, all the solutions to the “Heaviest Hitter” award that others have posted (that I’ve seen) include a “WITH” clause, but I think it doesn’t need it. Obviously, I could be wrong, so here it is:
SELECT ROUND(AVG(weight), 4) AS heaviest_hitters,
batting.yearid AS year,
teams.name AS team
FROM people
JOIN batting
ON people.playerid = batting.playerid
JOIN teams
ON batting.teamid = teams.teamid
WHERE batting.teamid = teams.teamid AND batting.yearid = teams.yearid
GROUP BY 2, 3
ORDER BY 1 DESC
LIMIT 1;
I saw a solution using MAX, but a LIMIT clause set to 1 does the same job.
Edit: Here’s the very similar solution to the “Shortest Slugger” task:
SELECT ROUND(AVG(height), 3) AS shortest_sluggers
batting.yearid AS year,
teams.name AS team
FROM people
JOIN batting
ON people.playerid = batting.playerid
JOIN teams
ON batting.teamid = teams.teamid
WHERE batting.teamid = teams.teamid AND batting.yearid = teams.yearid
GROUP BY 2, 3
ORDER BY 1 ASC
LIMIT 1;
Edit: Starting to get on a bit of a roll. It’s great way to learn by getting it wrong as many times as I have… Here’s the Biggest Spenders solution I came up with
SELECT teams.name AS biggest_spenders, -- (gets the team name instead of the team's 3-letter code)
salaries.yearid AS year,
SUM(salary) AS total_salary
FROM salaries
JOIN teams
ON salaries.teamid = teams.teamid
WHERE salaries.yearid = teams.yearid -- (without this it sums multiple years)
GROUP BY 1, 2
ORDER BY 3 DESC
LIMIT 1;
Edit: Here’s the Most Bang For Their Buck (Moneyball) award. Gotta say I had to double check the total team salary was correct as I couldn’t believe how little they spent for that number of wins!
SELECT teams.name AS most_bang_for_their_buck,
teams.w AS wins,
SUM(salary) AS team_salary,
ROUND((SUM(salary) / teams.w)) AS cost_per_win
FROM salaries
JOIN teams
ON salaries.teamid = teams.teamid
WHERE salaries.yearid = 2010 AND teams.yearid = 2010
GROUP BY 1, 2
ORDER BY 4 ASC
LIMIT 1;