From a210cac00c63341e3dca5138be311eb58a4d8aa6 Mon Sep 17 00:00:00 2001 From: SukeshSrinivas <123338848+SukeshSrinivas@users.noreply.github.com> Date: Wed, 1 Jan 2025 10:27:55 -0600 Subject: [PATCH 1/4] Done --- Problem 1.sql | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 Problem 1.sql diff --git a/Problem 1.sql b/Problem 1.sql new file mode 100644 index 0000000..7f309e5 --- /dev/null +++ b/Problem 1.sql @@ -0,0 +1,9 @@ +# Write your MySQL query statement below +WITH AllCandidates AS ( + SELECT experience, employee_id, SUM(salary) OVER (PARTITION BY experience ORDER BY salary, experience ) AS CusumSalary, salary FROM Candidates +) +SElECT 'Senior' AS experience, COUNT(employee_id) AS accepted_candidates FROM AllCandidates WHERE CusumSalary <=70000 AND experience ='Senior' +UNION ALL +SElECT 'Junior' AS experience, COUNT(employee_id) AS accepted_candidates FROM AllCandidates WHERE experience ='Junior' AND +CusumSalary<= (SELECT 70000 -IFNULL(MAX(CusumSalary),0) FROM AllCandidates WHERE CusumSalary <=70000 AND experience ='Senior') + From 192b21441dd57b006cd7069e3e05947af7696e0b Mon Sep 17 00:00:00 2001 From: SukeshSrinivas <123338848+SukeshSrinivas@users.noreply.github.com> Date: Wed, 1 Jan 2025 11:12:09 -0600 Subject: [PATCH 2/4] Problem 2 Done --- Problem 2 LeagueStats.sql | 51 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 Problem 2 LeagueStats.sql diff --git a/Problem 2 LeagueStats.sql b/Problem 2 LeagueStats.sql new file mode 100644 index 0000000..defd17d --- /dev/null +++ b/Problem 2 LeagueStats.sql @@ -0,0 +1,51 @@ +# Write your MySQL query statement below +-- Step 1: Calculate points, goals scored, and goal difference for home teams +WITH HomeTeam AS ( + SELECT + home_team_id AS team_id, + CASE + WHEN home_team_goals > away_team_goals THEN 3 -- 3 points for a win + WHEN home_team_goals < away_team_goals THEN 0 -- 0 points for a loss + ELSE 1 -- 1 point for a draw + END AS points, + home_team_goals AS goal_for, + away_team_goals AS goal_against, + home_team_goals - away_team_goals AS goal_diff + FROM Matches +), + +-- Step 2: Calculate the same metrics for away teams +AwayTeam AS ( + SELECT + away_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 points, + away_team_goals AS goal_for, + home_team_goals AS goal_against, + away_team_goals - home_team_goals AS goal_diff + FROM Matches +), + +-- Step 3: Combine home and away team results into a single dataset +FinalUnion AS ( + SELECT * FROM HomeTeam + UNION ALL + SELECT * FROM AwayTeam +) + +-- Step 4: Aggregate team statistics and rank teams +SELECT + t.team_name, + COUNT(FinalUnion.team_id) AS matches_played, -- Total matches played by each team + SUM(points) AS points, -- Total points earned by each team + SUM(goal_for) AS goal_for, -- Total goals scored + SUM(goal_against) AS goal_against, -- Total goals conceded + SUM(goal_diff) AS goal_diff -- Total goal difference +FROM FinalUnion +LEFT JOIN teams t +ON FinalUnion.team_id = t.team_id +GROUP BY t.team_name +ORDER BY points DESC, goal_diff DESC, t.team_name; -- Rank by points, then goal difference From ef4c296d18b0bc07efa900fbe0c1f83e601db513 Mon Sep 17 00:00:00 2001 From: SukeshSrinivas <123338848+SukeshSrinivas@users.noreply.github.com> Date: Wed, 1 Jan 2025 12:44:51 -0600 Subject: [PATCH 3/4] Problem 3 Done --- Problem 3SalesPerson.sql | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 Problem 3SalesPerson.sql diff --git a/Problem 3SalesPerson.sql b/Problem 3SalesPerson.sql new file mode 100644 index 0000000..913084f --- /dev/null +++ b/Problem 3SalesPerson.sql @@ -0,0 +1,11 @@ +-- Getting sales persons details who is associated with company Red +WITH CTE AS ( + SELECT o.order_id, o. sales_id, o.com_id, c.name as Companyname FROM Orders o + LEFT JOIN Company c + ON o.com_id = c.com_id + WHERE c.name = 'RED' + +) + -- Get sales names who are not associated with Red +SELECT name FROM SalesPerson +WHERE sales_id NOT IN (SELECT sales_id FROM CTE) \ No newline at end of file From 3beeca577f9f06101d344458018638b0d182cdb6 Mon Sep 17 00:00:00 2001 From: SukeshSrinivas <123338848+SukeshSrinivas@users.noreply.github.com> Date: Wed, 1 Jan 2025 19:32:05 -0600 Subject: [PATCH 4/4] Problem 4 done --- Problem 4FriendRequests.sql | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 Problem 4FriendRequests.sql diff --git a/Problem 4FriendRequests.sql b/Problem 4FriendRequests.sql new file mode 100644 index 0000000..c71cbfc --- /dev/null +++ b/Problem 4FriendRequests.sql @@ -0,0 +1,12 @@ +WITH UnionOutput AS ( + SELECT requester_id AS person, accepter_id AS friend FROM RequestAccepted + UNION ALL + SELECT accepter_id AS person, requester_id AS friend FROM RequestAccepted +) +SELECT + person AS id, + COUNT(friend) AS num +FROM UnionOutput +GROUP BY person +ORDER BY num DESC +LIMIT 1; \ No newline at end of file