Advertisement
AntoniiaG

Untitled

Oct 27th, 2024
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 5.74 KB | None | 0 0
  1. use moneyball;
  2.  
  3. select * from performances;
  4. select * from players;
  5. select * from salaries;
  6. select * from teams;
  7.  
  8. -- Task 1
  9. select round(avg(salary), 2) as "average salary", `year` from salaries group by `year` order by `year` desc;
  10.  
  11. -- Task 2
  12. select * from players where first_name = "Cal" and last_name = "Ripken"; -- player_id = 15726
  13. select round(avg(salary), 2) as "average salary", `year` from salaries where player_id = 15726 order by `year` desc;
  14.  
  15. select round(avg(salary), 2) as "average salary", `year`
  16. from salaries
  17. where player_id in (
  18.     select id from players where first_name = "Cal" and last_name = "Ripken"
  19. ) group by `year` order by `year` desc;
  20.  
  21. -- Task 3
  22. select * from players where first_name = "Ken" and last_name = "Griffey" and birth_year = 1969; -- player_id = 7266
  23. select HR, `year` from performances where player_id = 7266;
  24.  
  25. select HR, `year`
  26. from performances
  27. where player_id in (
  28.     select id from players where first_name = "Ken" and last_name = "Griffey" and birth_year = 1969
  29. );
  30.  
  31. -- Task 4
  32. select * from salaries where `year` = 2001 order by salary limit 50;
  33. select first_name, last_name from  players order by first_name and last_name or id;
  34.  
  35. SELECT
  36. (SELECT first_name FROM players WHERE id = player_id) AS first_name,
  37. (SELECT last_name FROM players WHERE id = player_id) AS last_name,
  38. salary
  39. FROM salaries
  40. WHERE `year` = 2001 ORDER BY salary ASC, first_name ASC, last_name ASC, player_id ASC LIMIT 50;
  41.  
  42. -- Task 5
  43. select * from players where first_name = "Satchel" and last_name = "Paige"; -- player_id = 14190
  44. select * from performances where player_id = 14190;
  45.  
  46. select `name`
  47. from teams
  48. where id in (
  49.     select team_id from performances where player_id in (
  50.         select id from players where first_name = "Satchel" and last_name = "Paige"
  51.     )
  52. );
  53.  
  54. -- Task 6
  55. select
  56. (select `name` from teams where id = team_id) as "Team Name",
  57. H as "Total hits"
  58. from performances where `year` = 2001 group by team_id order by H desc limit 5;
  59.  
  60. -- Task 7
  61. select *, max(salary) from salaries order by salary desc; -- 22000000 player_id = 15912
  62. select * from salaries order by salary desc;
  63. select * from players where id = 15912;
  64.  
  65. select
  66. (select first_name from players where id = player_id) as "First Name",
  67. (select last_name from players where id = player_id) as "Last Name",
  68. max(salary) as "Salary"
  69. from salaries;
  70.  
  71.  -- player_id = 15912
  72.  select first_name, last_name
  73.  from players
  74.  where id in (
  75.     select player_id from salaries where salary = (select max(salary) from salaries)
  76.  );
  77.  
  78.  -- Task 8
  79.  select * from performances where `year` = 2001 order by H desc; -- player_id = 18284
  80.   select max(H) from performances where `year` = 2001;
  81.  select * from salaries where player_id = 18284;
  82.  
  83.  select salary
  84.  from salaries
  85.  where player_id in (
  86.     select player_id from performances where H in (
  87.      select max(H) from performances where `year` = 2001
  88.     )
  89.  );
  90.  
  91.  -- Task 9
  92. select *, min(salary) from salaries where `year` = 2001;
  93. select *, round(avg(salary),2) as "Average Salary" from salaries  where `year` = 2001 group by team_id order by round(avg(salary),2) asc limit 5;
  94.  
  95. select
  96. (select `name` from teams where id = team_id) as "Team Name",
  97. round(avg(salary),2) as "Average Salary"
  98. from salaries  
  99. where `year` = 2001 group by team_id order by round(avg(salary),2) asc limit 5;
  100.  
  101. -- Task 10 име и фамилия на всеки играч, заплати, хоумрънове, годината на получената заплата и кога е постигнал хоумръновете
  102. select first_name, last_name from players;
  103. select H, `year` from performances;
  104. select salary, `year` from salaries;
  105.  
  106. select
  107. (select HR, `year` from performances where player_id = id group by `year`) as "home runs for each year",
  108. (select salary, `year` from salaries where player_id = id group by `year`) as "salary for each year",
  109. first_name, last_name
  110. from players order by id;
  111.  
  112. select
  113. (select first_name from players where id = player_id) as "first name",
  114. (select last_name from players where id = player_id) as "last name",
  115. salary,
  116. `year`,
  117. (select sum(HR) from performances as p where p.player_id = s.player_id AND p.`year` = s.`year`) as hr
  118. from salaries as
  119. order by player_id, `year` desc, hr desc, salary desc;
  120.  
  121. select * from performances where player_id = 896 and year = 2001;
  122.  
  123. select
  124. (select first_name from players where id = player_id) as FirstName,
  125. (select last_name from players where id = player_id) as LastName,
  126. (salary / (select sum(h) from performances as p where p.player_id = s.player_id AND p.`year` = s.`year`)) as DollarsPerHit,
  127. salary
  128. from salaries as s
  129. where `year` = 2001
  130. group by player_id
  131. having DollarsPerHit > 0
  132. order by DollarsPerHit asc, FirstName, LastName
  133. limit 10;
  134.  
  135. select
  136. (select first_name from players where id = player_id) as FirstName,
  137. (select last_name from players where id = player_id) as LastName,
  138. (salary / (select sum(h) from performances as p where p.player_id = s.player_id AND p.`year` = s.`year`)) as DollarsPerHit,
  139. (salary / (select sum(RBI) from performances as p where p.player_id = s.player_id AND p.`year` = s.`year`)) as DollarsPerRBI,
  140. salary,
  141. ((salary / (select sum(h) from performances as p where p.player_id = s.player_id AND p.`year` = s.`year`)) + (salary / (select sum(RBI) from performances as p where p.player_id = s.player_id AND p.`year` = s.`year`))) AS "sum"
  142. from salaries as s
  143. where `year` = 2001
  144. group by player_id
  145. having DollarsPerHit > 0 && DollarsPerRBI > 0
  146. order by ((salary / (select sum(h) from performances as p where p.player_id = s.player_id AND p.`year` = s.`year`)) + (salary / (select sum(RBI) from performances as p where p.player_id = s.player_id AND p.`year` = s.`year`))) , DollarsPerRBI, FirstName, LastName
  147. limit 10;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement