Files
me50/12.sql
2026-04-30 09:09:31 +02:00

52 lines
1.8 KiB
SQL
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
-- 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
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;