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
20 changes: 20 additions & 0 deletions friend_requests_II
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
WITH cte AS(
SELECT id,
SUM(num) AS num
FROM (
SELECT requester_id AS id,
COUNT(*) AS num
FROM requestaccepted
GROUP BY 1
UNION ALL
SELECT accepter_id AS id,
COUNT(*) AS num
FROM requestaccepted
GROUP BY 1
)a
GROUP BY id
)
SELECT id,
num
FROM cte
WHERE num = (SELECT MAX(num) FROM cte);
37 changes: 37 additions & 0 deletions league_stats
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
SELECT t.team_name,
SUM(matches_played) AS matches_played,
SUM(a.points) AS points,
SUM(a.goal_for) AS goal_for,
SUM(a.goal_against) AS goal_against,
SUM(a.goal_for) - SUM(a.goal_against) AS goal_diff
FROM (
-- calculate the home matches score
SELECT home_team_id AS team_id,
COUNT(*) AS matches_played,
SUM(CASE
WHEN home_team_goals > away_team_goals THEN 3
WHEN home_team_goals < away_team_goals THEN 0
WHEN home_team_goals = away_team_goals THEN 1
END) AS points,
SUM(home_team_goals) AS goal_for,
SUM(away_team_goals) AS goal_against
FROM matches
GROUP BY 1
UNION ALL
-- calculate the away matches score
SELECT away_team_id AS team_id,
COUNT(*) AS matches_played,
SUM(CASE
WHEN away_team_goals > home_team_goals THEN 3
WHEN away_team_goals < home_team_goals THEN 0
WHEN away_team_goals = home_team_goals THEN 1
END) AS points,
SUM(away_team_goals) AS goal_for,
SUM(home_team_goals) AS goal_against
FROM matches
GROUP BY 1
) a
INNER JOIN teams t
ON t.team_id = a.team_id
GROUP BY t.team_name
ORDER BY points DESC, goal_diff DESC, team_name;
30 changes: 30 additions & 0 deletions num_of_seniors_juniors
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
WITH cte AS (
SELECT employee_id,
experience,
SUM(salary) AS salary,
SUM(salary) OVER(PARTITION BY experience ORDER BY salary, employee_id) AS r_sum
FROM candidates
-- WHERE experience = 'Senior'
GROUP BY 1,2
),
senior AS (
SELECT 'Senior' AS experience,
r_sum
FROM cte
WHERE experience = 'Senior'
AND r_sum <= 70000
),
junior AS(
SELECT 'Junior' AS experience,
r_sum
FROM cte
WHERE experience = 'Junior'
AND r_sum <= (SELECT 70000-IFNULL(MAX(r_sum),0) FROM senior)
)
SELECT experience,
COUNT(*) AS accepted_candidates
FROM senior
UNION ALL
SELECT experience,
COUNT(*) AS accepted_candidates
FROM junior;
12 changes: 12 additions & 0 deletions sales_person
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
WITH cte AS (
SELECT o.com_id,
o.sales_id,
c.name
FROM orders o
INNER JOIN company c
ON o.com_id = c.com_id
WHERE c.name = 'RED'
)
SELECT name
FROM salesperson
WHERE sales_id NOT IN (SELECT DISTINCT sales_id FROM cte);