From c4a019ea9c430f162ce01cc7b6384847d6e04d06 Mon Sep 17 00:00:00 2001 From: Pranathi Date: Mon, 9 Jun 2025 16:06:37 -0700 Subject: [PATCH] SQL4 submitted --- Problem1.sql | 6 ++++++ Problem2.sql | 18 ++++++++++++++++++ Problem3.sql | 4 ++++ Problem4.sql | 8 ++++++++ 4 files changed, 36 insertions(+) create mode 100644 Problem1.sql create mode 100644 Problem2.sql create mode 100644 Problem3.sql create mode 100644 Problem4.sql diff --git a/Problem1.sql b/Problem1.sql new file mode 100644 index 0000000..64064ea --- /dev/null +++ b/Problem1.sql @@ -0,0 +1,6 @@ +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) \ No newline at end of file diff --git a/Problem2.sql b/Problem2.sql new file mode 100644 index 0000000..d59cf76 --- /dev/null +++ b/Problem2.sql @@ -0,0 +1,18 @@ +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 home_team_id as r2, away_team_id as r1, home_team_goals as g2, away_team_goals as g1 + 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 INNER 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/Problem3.sql b/Problem3.sql new file mode 100644 index 0000000..54ee16c --- /dev/null +++ b/Problem3.sql @@ -0,0 +1,4 @@ +SELECT name from SalesPerson where sales_id NOT IN ( Select DISTINCT 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 diff --git a/Problem4.sql b/Problem4.sql new file mode 100644 index 0000000..d5807e7 --- /dev/null +++ b/Problem4.sql @@ -0,0 +1,8 @@ +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