From 1e03573296bdc09380760347d15e02068e31f33d Mon Sep 17 00:00:00 2001 From: daviddoji Date: Thu, 30 Apr 2026 09:09:31 +0200 Subject: [PATCH] automated commit by check50 [check50=True] --- 12.sql | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/12.sql b/12.sql index e69de29..b3ede5b 100644 --- a/12.sql +++ b/12.sql @@ -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 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;