commit 144001e997909947cee0fdd9acc893d1a3a013a9 Author: bot50 Date: Tue Apr 28 14:42:24 2026 +0000 daviddoji-cs50/problems/2024/sql/moneyball@20260428T144224.408356905Z diff --git a/1.sql b/1.sql new file mode 100644 index 0000000..d9cbba5 --- /dev/null +++ b/1.sql @@ -0,0 +1,8 @@ +-- write a SQL query to find the average player salary by year. +-- Sort by year in descending order. +-- Round the salary to two decimal places and call the column “average salary”. +-- Your query should return a table with two columns, one for year and one for average salary. +SELECT "year", ROUND(AVG("salary"), 2) AS "average salary" +FROM "salaries" +GROUP BY "year" +ORDER BY "year" DESC; diff --git a/10.sql b/10.sql new file mode 100644 index 0000000..ae13436 --- /dev/null +++ b/10.sql @@ -0,0 +1,26 @@ +-- To be precise, the table should include: +-- All player’s first names +-- All player’s last names +-- All player’s salaries +-- All player’s home runs +-- The year in which the player was paid that salary and hit those home runs + +-- In 10.sql, write a query to return just such a table. +-- Your query should return a table with five columns, per the above. +-- Order the results, first and foremost, by player’s IDs (least to greatest). +-- Order rows about the same player by year, in descending order. +-- Consider a corner case: suppose a player has multiple salaries or performances for a given year. +-- Order them first by number of home runs, in descending order, followed by salary, in descending order. +-- Be careful to ensure that, for a single row, the salary’s year and the performance’s year match. +SELECT p."first_name", p."last_name", s."salary", perf."HR", s."year" +FROM "players" AS p +JOIN "salaries" AS s + ON s."player_id" = p."id" +JOIN "performances" AS perf + ON perf."player_id" = p."id" + AND perf."year" = s."year" +ORDER BY + p."id" ASC, + s."year" DESC, + perf."HR" DESC, + s."salary" DESC; diff --git a/11.sql b/11.sql new file mode 100644 index 0000000..5a66321 --- /dev/null +++ b/11.sql @@ -0,0 +1,28 @@ +-- write a SQL query to find the 10 least expensive players per hit in 2001. +-- Your query should return a table with three columns, one for the players’ first names, one of their last names, +-- and one called “dollars per hit”. +-- You can calculate the “dollars per hit” column by dividing a player’s 2001 salary by the number of hits they made +-- in 2001. Recall you can use AS to rename a column. +-- Dividing a salary by 0 hits will result in a NULL value. Avoid the issue by filtering out players with 0 hits. +-- Sort the table by the “dollars per hit” column, least to most expensive. If two players have the same “dollars per +-- hit”, order by first name, followed by last name, in alphabetical order. +-- As in 10.sql, ensure that the salary’s year and the performance’s year match. +-- You may assume, for simplicity, that a player will only have one salary and one performance in 2001. +SELECT + p."first_name", + p."last_name", + (s."salary" / perf."H") AS "dollar per hit" +FROM "players" AS p +JOIN "salaries" AS s + ON s."player_id" = p."id" +JOIN "performances" as perf + ON perf."player_id" = p."id" + AND perf."year" = s."year" +WHERE + s."year" = '2001' + AND perf."H" > 0 +ORDER BY + "dollars per hit" ASC, + p."first_name" ASC, + p."last_name" ASC +LIMIT 10; diff --git a/12.sql b/12.sql new file mode 100644 index 0000000..e69de29 diff --git a/2.sql b/2.sql new file mode 100644 index 0000000..f672ed0 --- /dev/null +++ b/2.sql @@ -0,0 +1,9 @@ +-- write a SQL query to find Cal Ripken Jr.’s salary history. +-- Sort by year in descending order. +-- Your query should return a table with two columns, one for year and one for salary. +SELECT s."year", s."salary" +FROM "salaries" AS s +JOIN "players" AS p + ON s."player_id" = p."id" +WHERE p."first_name" = "Cal" AND p."last_name" LIKE "Ripken%" +ORDER BY "year" DESC; diff --git a/3.sql b/3.sql new file mode 100644 index 0000000..52d4460 --- /dev/null +++ b/3.sql @@ -0,0 +1,15 @@ +-- write a SQL query to find Ken Griffey Jr.’s home run history. +-- Sort by year in descending order. +-- Note that there may be two players with the name “Ken Griffey.” This Ken Griffey was born in 1969. +-- Your query should return a table with two columns, one for year and one for home runs. +SELECT perf."year", perf."hr" +FROM "performances" AS perf +JOIN "players" AS p + ON perf."player_id" = p."id" +WHERE + p."first_name" = "Ken" + AND + p."last_name" LIKE "Griffey%" + AND + p."birth_year" = "1969" +ORDER BY "year" DESC; diff --git a/4.sql b/4.sql new file mode 100644 index 0000000..9ea8b9c --- /dev/null +++ b/4.sql @@ -0,0 +1,13 @@ +-- write a SQL query to find the 50 players paid the least in 2001. +-- Sort players by salary, lowest to highest. +-- If two players have the same salary, sort alphabetically by first name and then by last name. +-- If two players have the same first and last name, sort by player ID. +-- Your query should return three columns, one for players’ first names, one for their last names, and one for their salaries. +SELECT + p."first_name", p."last_name", s."salary" +FROM "players" AS p +JOIN "salaries" AS s + ON s."player_id" = p."id" +WHERE s."year" = "2001" +ORDER BY s."salary" ASC, p."first_name" ASC, p."last_name" ASC, p."id" ASC +LIMIT 50; diff --git a/5.sql b/5.sql new file mode 100644 index 0000000..6ad97a4 --- /dev/null +++ b/5.sql @@ -0,0 +1,9 @@ +-- write a SQL query to find all teams that Satchel Paige played for. +-- Your query should return a table with a single column, one for the name of the teams. +SELECT DISTINCT t."name" +FROM "teams" AS t +JOIN "performances" AS perf + ON perf."team_id" = t."id" +JOIN "players" AS p + ON p."id" = perf."player_id" +WHERE p."first_name" = "Satchel" AND p."last_name" = "Paige"; diff --git a/6.sql b/6.sql new file mode 100644 index 0000000..8b87f93 --- /dev/null +++ b/6.sql @@ -0,0 +1,14 @@ +-- write a SQL query to return the top 5 teams, sorted by the total number of hits by players in 2001. +-- Call the column representing total hits by players in 2001 “total hits”. +-- Sort by total hits, highest to lowest. +-- Your query should return two columns, one for the teams’ names and one for their total hits in 2001. +SELECT + t."name", + SUM(perf."H") AS "total hits" +FROM "teams" AS t +JOIN "performances" AS perf + ON perf."team_id" = t."id" +WHERE perf."year" = '2001' +GROUP BY t."id", t."name" +ORDER BY "total hits" DESC +LIMIT 5; diff --git a/7.sql b/7.sql new file mode 100644 index 0000000..ab1b9e7 --- /dev/null +++ b/7.sql @@ -0,0 +1,8 @@ +-- write a SQL query to find the name of the player who’s been paid the highest salary, of all time, in Major League Baseball. +-- Your query should return a table with two columns, one for the player’s first name and one for their last name. +SELECT p."first_name", p."last_name" +FROM "players" AS p +JOIN "salaries" AS s + ON s."player_id" = p."id" +ORDER BY s."salary" DESC +LIMIT 1; diff --git a/8.sql b/8.sql new file mode 100644 index 0000000..019acc7 --- /dev/null +++ b/8.sql @@ -0,0 +1,9 @@ +-- write a SQL query to find the 2001 salary of the player who hit the most home runs in 2001. +-- Your query should return a table with one column, the salary of the player. +SELECT s."salary" +FROM "salaries" AS s +JOIN "performances" AS perf + ON perf."player_id" = s."player_id" +WHERE s."year" = '2001' AND perf."year" = '2001' +ORDER BY perf."HR" DESC +LIMIT 1; diff --git a/9.sql b/9.sql new file mode 100644 index 0000000..a34ff07 --- /dev/null +++ b/9.sql @@ -0,0 +1,14 @@ +-- write a SQL query to find the 5 lowest paying teams (by average salary) in 2001. +-- Round the average salary column to two decimal places and call it “average salary”. +-- Sort the teams by average salary, least to greatest. +-- Your query should return a table with two columns, one for the teams’ names and one for their average salary. +SELECT + t."name", + ROUND(AVG(s."salary"), 2) AS "average salary" +FROM "teams" AS t +JOIN "salaries" AS s + ON s."team_id" = t."id" +WHERE s."year" = '2001' +GROUP BY t."id", t."name" +ORDER BY "average salary" ASC +LIMIT 5;