Skip to content
Open

SQL4 #56

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions Problem 1.sql
Original file line number Diff line number Diff line change
@@ -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')

51 changes: 51 additions & 0 deletions Problem 2 LeagueStats.sql
Original file line number Diff line number Diff line change
@@ -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
11 changes: 11 additions & 0 deletions Problem 3SalesPerson.sql
Original file line number Diff line number Diff line change
@@ -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)
12 changes: 12 additions & 0 deletions Problem 4FriendRequests.sql
Original file line number Diff line number Diff line change
@@ -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;