From 18afd35347c0efff324a87d8f7850d4772ab6892 Mon Sep 17 00:00:00 2001 From: Paneri Patel Date: Wed, 25 Jun 2025 15:33:45 -0400 Subject: [PATCH] Done Sql4 --- FriendRequestsII.sql | 16 ++++++++++++++++ LeagueStatistics.sql | 17 +++++++++++++++++ SalesPerson.sql | 12 ++++++++++++ ...mberOfSeniorsAndJuniorsToJoinTheCompany.sql | 18 ++++++++++++++++++ 4 files changed, 63 insertions(+) create mode 100644 FriendRequestsII.sql create mode 100644 LeagueStatistics.sql create mode 100644 SalesPerson.sql create mode 100644 TheNumberOfSeniorsAndJuniorsToJoinTheCompany.sql diff --git a/FriendRequestsII.sql b/FriendRequestsII.sql new file mode 100644 index 0000000..d55ec42 --- /dev/null +++ b/FriendRequestsII.sql @@ -0,0 +1,16 @@ +''' +4 Problem 4 : Friend Requests II (https://leetcode.com/problems/friend-requests-ii-who-has-the-most-friends/ ) +''' + +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..504bf1d --- /dev/null +++ b/LeagueStatistics.sql @@ -0,0 +1,17 @@ +''' +2 Problem 2 : League Statistics (https://leetcode.com/problems/league-statistics/ ) +''' + +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) - SUM(c.g2) AS 'goal_diff' +FROM Teams t JOIN CTE c ON t.team_id = c.r1 GROUP BY c.r1 ORDER BY points DESC, goal_diff DESC, t.team_name \ No newline at end of file diff --git a/SalesPerson.sql b/SalesPerson.sql new file mode 100644 index 0000000..7e5a9c8 --- /dev/null +++ b/SalesPerson.sql @@ -0,0 +1,12 @@ +''' +3 Problem 3 : Sales Person (https://leetcode.com/problems/sales-person/ ) +''' + +SELECT name +FROM SalesPerson +WHERE sales_id NOT IN ( + SELECT sales_id + FROM Orders + JOIN Company ON Company.com_id = Orders.com_id + WHERE name = 'RED' +) \ No newline at end of file diff --git a/TheNumberOfSeniorsAndJuniorsToJoinTheCompany.sql b/TheNumberOfSeniorsAndJuniorsToJoinTheCompany.sql new file mode 100644 index 0000000..9296a87 --- /dev/null +++ b/TheNumberOfSeniorsAndJuniorsToJoinTheCompany.sql @@ -0,0 +1,18 @@ +''' +Sql4 + +1 Problem 1 : The Number of Seniors and Juniors to Join the Company (https://leetcode.com/problems/the-number-of-seniors-and-juniors-to-join-the-company/) +''' + +WITH CTE AS (SELECT employee_id, experience, salary, +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) +