52 lines
1.8 KiB
SQL
52 lines
1.8 KiB
SQL
-- 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 player’s 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 you’ve 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
|
||
CAST(s."salary" AS REAL) / 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
|
||
CAST(s."salary" AS REAL) / 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;
|