Compare commits
10 Commits
6beb669329
...
31b0f33909
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
31b0f33909 | ||
|
|
5f2b5a71cb | ||
|
|
1e03573296 | ||
|
|
4eca6621ac | ||
|
|
6bcd687802 | ||
|
|
c14dcd272d | ||
|
|
695d390571 | ||
|
|
8225d14c04 | ||
|
|
4e05ce78d2 | ||
|
|
5e7a11eada |
26
10.sql
26
10.sql
@@ -0,0 +1,26 @@
|
|||||||
|
-- To be precise, the table should include:
|
||||||
|
-- All player’s first names
|
||||||
|
-- All player’s last names
|
||||||
|
-- All player’s salaries
|
||||||
|
-- All player’s home runs
|
||||||
|
-- The year in which the player was paid that salary and hit those home runs
|
||||||
|
|
||||||
|
-- In 10.sql, write a query to return just such a table.
|
||||||
|
-- Your query should return a table with five columns, per the above.
|
||||||
|
-- Order the results, first and foremost, by player’s IDs (least to greatest).
|
||||||
|
-- Order rows about the same player by year, in descending order.
|
||||||
|
-- Consider a corner case: suppose a player has multiple salaries or performances for a given year.
|
||||||
|
-- Order them first by number of home runs, in descending order, followed by salary, in descending order.
|
||||||
|
-- Be careful to ensure that, for a single row, the salary’s year and the performance’s year match.
|
||||||
|
SELECT p."first_name", p."last_name", s."salary", perf."HR", s."year"
|
||||||
|
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"
|
||||||
|
ORDER BY
|
||||||
|
p."id" ASC,
|
||||||
|
s."year" DESC,
|
||||||
|
perf."HR" DESC,
|
||||||
|
s."salary" DESC;
|
||||||
|
|||||||
28
11.sql
28
11.sql
@@ -0,0 +1,28 @@
|
|||||||
|
-- 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 player’s 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 salary’s year and the performance’s 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",
|
||||||
|
(s."salary" / 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;
|
||||||
|
|||||||
51
12.sql
51
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
|
||||||
|
(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;
|
||||||
|
|||||||
14
9.sql
14
9.sql
@@ -0,0 +1,14 @@
|
|||||||
|
-- write a SQL query to find the 5 lowest paying teams (by average salary) in 2001.
|
||||||
|
-- Round the average salary column to two decimal places and call it “average salary”.
|
||||||
|
-- Sort the teams by average salary, least to greatest.
|
||||||
|
-- Your query should return a table with two columns, one for the teams’ names and one for their average salary.
|
||||||
|
SELECT
|
||||||
|
t."name",
|
||||||
|
ROUND(AVG(s."salary"), 2) AS "average salary"
|
||||||
|
FROM "teams" AS t
|
||||||
|
JOIN "salaries" AS s
|
||||||
|
ON s."team_id" = t."id"
|
||||||
|
WHERE s."year" = '2001'
|
||||||
|
GROUP BY t."id", t."name"
|
||||||
|
ORDER BY "average salary" ASC
|
||||||
|
LIMIT 5;
|
||||||
|
|||||||
Reference in New Issue
Block a user