From 267aee42cf65797b96cc46000998bd2522481188 Mon Sep 17 00:00:00 2001 From: Aniruddh Date: Thu, 12 Jun 2025 17:01:59 -0700 Subject: [PATCH] SQL 4 Done --- FriendRequests2.sql | 10 +++++++++ LeagueStatistics.sql | 36 +++++++++++++++++++++++++++++++ NumberofSeniors.sql | 50 ++++++++++++++++++++++++++++++++++++++++++++ SalesPerson.sql | 2 ++ 4 files changed, 98 insertions(+) create mode 100644 FriendRequests2.sql create mode 100644 LeagueStatistics.sql create mode 100644 NumberofSeniors.sql create mode 100644 SalesPerson.sql diff --git a/FriendRequests2.sql b/FriendRequests2.sql new file mode 100644 index 0000000..30d6682 --- /dev/null +++ b/FriendRequests2.sql @@ -0,0 +1,10 @@ +# Write your MySQL query statement below +WITH CTE AS( + SELECT requester_id as 'id' FROM RequestAccepted + UNION ALL + SELECT accepter_id as 'id' FROM RequestAccepted +) + + + +Select id , COUNT(id) AS 'num' FROM CTE GROUP BY id ORDER BY num DESC limit 1; \ No newline at end of file diff --git a/LeagueStatistics.sql b/LeagueStatistics.sql new file mode 100644 index 0000000..2e288db --- /dev/null +++ b/LeagueStatistics.sql @@ -0,0 +1,36 @@ +WITH + Scores AS ( + SELECT + home_team_id AS team_id, + CASE + WHEN home_team_goals > away_team_goals THEN 3 + WHEN home_team_goals < away_team_goals THEN 0 + ELSE 1 + END AS score, + home_team_goals AS goals, + away_team_goals AS away_goals + FROM Matches + UNION ALL + SELECT + away_team_id AS team_id, + CASE + WHEN home_team_goals > away_team_goals THEN 0 + WHEN home_team_goals < away_team_goals THEN 3 + ELSE 1 + END AS score, + away_team_goals AS goals, + home_team_goals AS away_goals + FROM Matches + ) +SELECT + team_name, + COUNT(1) AS matches_played, + SUM(score) AS points, + SUM(goals) AS goal_for, + SUM(away_goals) AS goal_against, + (SUM(goals) - SUM(away_goals)) AS goal_diff +FROM + Scores AS s + JOIN Teams AS t ON s.team_id = t.team_id +GROUP BY s.team_id +ORDER BY points DESC, goal_diff DESC, team_name; \ No newline at end of file diff --git a/NumberofSeniors.sql b/NumberofSeniors.sql new file mode 100644 index 0000000..8c4a1e6 --- /dev/null +++ b/NumberofSeniors.sql @@ -0,0 +1,50 @@ +with s as ( + select + employee_id, + sum(salary) over( + order by + salary + ) cur + from + Candidates + where + experience = 'Senior' +), +j as ( + select + employee_id, + ifnull( + ( + select + max(cur) + from + s + where + cur <= 70000 + ), + 0 + ) + sum(salary) over( + order by + salary + ) cur + from + Candidates + where + experience = 'Junior' +) +select + 'Senior' experience, + count(employee_id) accepted_candidates +from + s +where + cur <= 70000 +union +all +select + 'Junior' experience, + count(employee_id) accepted_candidates +from + j +where + cur <= 70000; \ No newline at end of file diff --git a/SalesPerson.sql b/SalesPerson.sql new file mode 100644 index 0000000..fea29fd --- /dev/null +++ b/SalesPerson.sql @@ -0,0 +1,2 @@ +SELECT name FROM salesperson WHERE sales_id NOT IN( +SELECT o.sales_id FROM orders o JOIN company C on o.com_id = c.com_id WHERE c.name='RED'); \ No newline at end of file