Files
me50/11.sql
2026-04-28 16:45:22 +02:00

29 lines
1.3 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 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 players 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 salarys year and the performances 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",
(CAST(s."salary" AS REAL) / perf."H") AS "dollars 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;