From 8f261c4133971503578c540df8e42af520507340 Mon Sep 17 00:00:00 2001 From: Sakshi Asati Date: Wed, 4 Jun 2025 00:29:18 -0600 Subject: [PATCH] Done SQL4 --- FriendRequestsII.sql | 16 ++++++++++++ LeagueStatistics.sql | 25 +++++++++++++++++++ SalesPerson.sql | 10 ++++++++ ...eniors-and-juniors-to-join-the-company.sql | 25 +++++++++++++++++++ 4 files changed, 76 insertions(+) create mode 100644 FriendRequestsII.sql create mode 100644 LeagueStatistics.sql create mode 100644 SalesPerson.sql create mode 100644 number-of-seniors-and-juniors-to-join-the-company.sql diff --git a/FriendRequestsII.sql b/FriendRequestsII.sql new file mode 100644 index 0000000..23c2083 --- /dev/null +++ b/FriendRequestsII.sql @@ -0,0 +1,16 @@ +WITH CTE AS ( + SELECT requester_id AS id, + (SELECT COUNT(*) FROM RequestAccepted WHERE id = requester_id OR id = accepter_id) AS num + FROM RequestAccepted + + UNION + + SELECT accepter_id AS id, + (SELECT COUNT(*) FROM RequestAccepted WHERE id = requester_id OR id = accepter_id) AS num + FROM RequestAccepted +) + +SELECT * +FROM CTE +ORDER BY num DESC +LIMIT 1; diff --git a/LeagueStatistics.sql b/LeagueStatistics.sql new file mode 100644 index 0000000..e18c81e --- /dev/null +++ b/LeagueStatistics.sql @@ -0,0 +1,25 @@ +WITH CTE AS ( + SELECT home_team_id AS r1, away_team_id AS r2, home_team_goals AS g1, away_team_goals AS g2 + FROM Matches + UNION ALL + SELECT away_team_id AS r1, home_team_id AS r2, away_team_goals AS g1, home_team_goals AS g2 + FROM Matches +) + +SELECT + t.team_name, + COUNT(c.r1) AS matches_played, + SUM( + CASE + WHEN c.g1 > c.g2 THEN 3 + WHEN c.g1 = c.g2 THEN 1 + ELSE 0 + END + ) AS points, + SUM(c.g1) AS goal_for, + SUM(c.g2) AS goal_against, + SUM(c.g1 - c.g2) AS goal_diff +FROM Teams t +JOIN CTE c ON t.team_id = c.r1 +GROUP BY c.r1, t.team_name +ORDER BY points DESC, goal_diff DESC, t.team_name; diff --git a/SalesPerson.sql b/SalesPerson.sql new file mode 100644 index 0000000..1303617 --- /dev/null +++ b/SalesPerson.sql @@ -0,0 +1,10 @@ +WITH CTE AS ( + SELECT o.sales_id + FROM Orders o + JOIN Company c ON o.com_id = c.com_id + WHERE c.name = 'RED' +) + +SELECT name +FROM SalesPerson +WHERE sales_id NOT IN (SELECT sales_id FROM CTE); diff --git a/number-of-seniors-and-juniors-to-join-the-company.sql b/number-of-seniors-and-juniors-to-join-the-company.sql new file mode 100644 index 0000000..feba92e --- /dev/null +++ b/number-of-seniors-and-juniors-to-join-the-company.sql @@ -0,0 +1,25 @@ +# Write your MySQL query statement below +WITH CTE AS ( + SELECT + employee_id, experience, SUM(salary) OVER(PARTITION BY experience ORDER BY salary, employee_id) AS rsum + FROM candidates +) + +SELECT + 'Senior' AS experience, COUNT(employee_id) AS accepted_candidates +FROM CTE +WHERE experience = 'Senior' + AND rsum <= 70000 + +UNION + +SELECT + 'Junior' AS experience, COUNT(employee_id) AS accepted_candidates +FROM CTE +WHERE experience = 'Junior' + AND rsum <= ( + SELECT 70000 - IFNULL(MAX(rsum), 0) + FROM CTE + WHERE experience = 'Senior' + AND rsum <= 70000 + );