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') + 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 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 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