bot50
2026-04-30 07:13:37 +00:00
commit df51b1f360
12 changed files with 204 additions and 0 deletions

51
12.sql Normal file
View File

@@ -0,0 +1,51 @@
-- write a SQL query to find the players among the 10 least expensive players per hit and among the 10 least expensive players per RBI in 2001.
-- Your query should return a table with two columns, one for the players first names and one of their last names.
-- You can calculate a players salary per RBI by dividing their 2001 salary by their number of RBIs in 2001.
-- You may assume, for simplicity, that a player will only have one salary and one performance in 2001.
-- Order your results by player ID, least to greatest (or alphabetically by last name, as both are the same in this case!).
-- Keep in mind the lessons youve learned in 10.sql and 11.sql!
WITH least_expensive_per_hit AS (
SELECT
p."id",
p."first_name",
p."last_name"
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
(s."salary" / perf."H") ASC,
p."first_name" ASC,
p."last_name" ASC
LIMIT 10
),
least_expensive_per_rbi AS (
SELECT
p."id",
p."first_name",
p."last_name"
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."RBI" > 0
ORDER BY
(s."salary" / perf."RBI") ASC,
p."first_name" ASC,
p."last_name" ASC
LIMIT 10
)
SELECT
h."first_name",
h."last_name"
FROM least_expensive_per_hit AS h
JOIN least_expensive_per_rbi AS r
ON h."id" = r."id"
ORDER BY h."id" ASC;