Skip to content
Open
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
16 changes: 16 additions & 0 deletions FriendRequestsII.sql
Original file line number Diff line number Diff line change
@@ -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
17 changes: 17 additions & 0 deletions LeagueStatistics.sql
Original file line number Diff line number Diff line change
@@ -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
12 changes: 12 additions & 0 deletions SalesPerson.sql
Original file line number Diff line number Diff line change
@@ -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'
)
18 changes: 18 additions & 0 deletions TheNumberOfSeniorsAndJuniorsToJoinTheCompany.sql
Original file line number Diff line number Diff line change
@@ -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)