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 @@
WITH CTE AS (
SELECT requester_id AS id,
(SELECT COUNT(*) FROM RequestAccepted WHERE id = requester_id OR id = accepter_id) AS num
FROM RequestAccepted

UNION

SELECT accepter_id AS id,
(SELECT COUNT(*) FROM RequestAccepted WHERE id = requester_id OR id = accepter_id) AS num
FROM RequestAccepted
)

SELECT *
FROM CTE
ORDER BY num DESC
LIMIT 1;
25 changes: 25 additions & 0 deletions LeagueStatistics.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
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 - c.g2) AS goal_diff
FROM Teams t
JOIN CTE c ON t.team_id = c.r1
GROUP BY c.r1, t.team_name
ORDER BY points DESC, goal_diff DESC, t.team_name;
10 changes: 10 additions & 0 deletions SalesPerson.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
WITH CTE AS (
SELECT o.sales_id
FROM Orders o
JOIN Company c ON o.com_id = c.com_id
WHERE c.name = 'RED'
)

SELECT name
FROM SalesPerson
WHERE sales_id NOT IN (SELECT sales_id FROM CTE);
25 changes: 25 additions & 0 deletions number-of-seniors-and-juniors-to-join-the-company.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Write your MySQL query statement below
WITH CTE AS (
SELECT
employee_id, experience, 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
);