From 040270cd3d9de5021552a3b00bae285e4bf9199a Mon Sep 17 00:00:00 2001 From: Feminto Date: Wed, 28 May 2025 21:05:55 -0700 Subject: [PATCH] Adding SQL files with solution --- friend_requests_II | 20 ++++++++++++++++++++ league_stats | 37 +++++++++++++++++++++++++++++++++++++ num_of_seniors_juniors | 30 ++++++++++++++++++++++++++++++ sales_person | 12 ++++++++++++ 4 files changed, 99 insertions(+) create mode 100644 friend_requests_II create mode 100644 league_stats create mode 100644 num_of_seniors_juniors create mode 100644 sales_person diff --git a/friend_requests_II b/friend_requests_II new file mode 100644 index 0000000..de8d1c3 --- /dev/null +++ b/friend_requests_II @@ -0,0 +1,20 @@ +WITH cte AS( + SELECT id, + SUM(num) AS num + FROM ( + SELECT requester_id AS id, + COUNT(*) AS num + FROM requestaccepted + GROUP BY 1 + UNION ALL + SELECT accepter_id AS id, + COUNT(*) AS num + FROM requestaccepted + GROUP BY 1 + )a + GROUP BY id + ) +SELECT id, + num +FROM cte +WHERE num = (SELECT MAX(num) FROM cte); \ No newline at end of file diff --git a/league_stats b/league_stats new file mode 100644 index 0000000..5b1f603 --- /dev/null +++ b/league_stats @@ -0,0 +1,37 @@ +SELECT t.team_name, + SUM(matches_played) AS matches_played, + SUM(a.points) AS points, + SUM(a.goal_for) AS goal_for, + SUM(a.goal_against) AS goal_against, + SUM(a.goal_for) - SUM(a.goal_against) AS goal_diff +FROM ( + -- calculate the home matches score + SELECT home_team_id AS team_id, + COUNT(*) AS matches_played, + SUM(CASE + WHEN home_team_goals > away_team_goals THEN 3 + WHEN home_team_goals < away_team_goals THEN 0 + WHEN home_team_goals = away_team_goals THEN 1 + END) AS points, + SUM(home_team_goals) AS goal_for, + SUM(away_team_goals) AS goal_against + FROM matches + GROUP BY 1 + UNION ALL + -- calculate the away matches score + SELECT away_team_id AS team_id, + COUNT(*) AS matches_played, + SUM(CASE + WHEN away_team_goals > home_team_goals THEN 3 + WHEN away_team_goals < home_team_goals THEN 0 + WHEN away_team_goals = home_team_goals THEN 1 + END) AS points, + SUM(away_team_goals) AS goal_for, + SUM(home_team_goals) AS goal_against + FROM matches + GROUP BY 1 +) a +INNER JOIN teams t +ON t.team_id = a.team_id +GROUP BY t.team_name +ORDER BY points DESC, goal_diff DESC, team_name; \ No newline at end of file diff --git a/num_of_seniors_juniors b/num_of_seniors_juniors new file mode 100644 index 0000000..aa2a21f --- /dev/null +++ b/num_of_seniors_juniors @@ -0,0 +1,30 @@ +WITH cte AS ( + SELECT employee_id, + experience, + SUM(salary) AS salary, + SUM(salary) OVER(PARTITION BY experience ORDER BY salary, employee_id) AS r_sum + FROM candidates + -- WHERE experience = 'Senior' + GROUP BY 1,2 +), +senior AS ( + SELECT 'Senior' AS experience, + r_sum + FROM cte + WHERE experience = 'Senior' + AND r_sum <= 70000 +), +junior AS( + SELECT 'Junior' AS experience, + r_sum + FROM cte + WHERE experience = 'Junior' + AND r_sum <= (SELECT 70000-IFNULL(MAX(r_sum),0) FROM senior) +) +SELECT experience, + COUNT(*) AS accepted_candidates +FROM senior +UNION ALL +SELECT experience, + COUNT(*) AS accepted_candidates +FROM junior; \ No newline at end of file diff --git a/sales_person b/sales_person new file mode 100644 index 0000000..673e4f5 --- /dev/null +++ b/sales_person @@ -0,0 +1,12 @@ +WITH cte AS ( + SELECT o.com_id, + o.sales_id, + c.name +FROM orders o +INNER JOIN company c +ON o.com_id = c.com_id +WHERE c.name = 'RED' +) +SELECT name +FROM salesperson +WHERE sales_id NOT IN (SELECT DISTINCT sales_id FROM cte); \ No newline at end of file