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
32 changes: 32 additions & 0 deletions 1st problem.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Write your MySQL query statement below

with seniors as
(
select employee_id,
70000 - rolling_sum as budget_left
from
(select *,
sum(salary) over(order by salary asc) as rolling_sum
from Candidates
where experience = 'Senior') a
where rolling_sum <= 70000
),

juniors as
(
select a.employee_id
from
(select *,
sum(salary) over(order by salary asc) as rolling_sum
from Candidates
where experience = 'Junior') a
where rolling_sum <= (select coalesce(min(budget_left), 70000) from seniors)
)

select 'Senior' as experience,
coalesce(count(employee_id), 0) as accepted_candidates
from seniors
union all
select 'Junior' as experience,
coalesce(count(employee_id), 0) as accepted_candidates
from juniors
12 changes: 12 additions & 0 deletions 2nd problem.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
select
team_name, count(*) as matches_played, sum(case when home > away then 3 when home = away then 1 else 0 end) as points, sum(home) as goal_for
, sum(away) as goal_against, sum(home) - sum(away) as goal_diff

from
(select home_team_id, home_team_goals as home, away_team_goals as away from matches
union all
select away_team_id as home_team_id, away_team_goals as home, home_team_goals as away from matches
) g
join teams t on g.home_team_id = t.team_id
group by 1
order by 3 desc, 6 desc, 1
4 changes: 4 additions & 0 deletions 3rd problem.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
select salesperson.name
from orders o join company c on (o.com_id = c.com_id and c.name = 'RED')
right join salesperson on salesperson.sales_id = o.sales_id
where o.sales_id is null
17 changes: 17 additions & 0 deletions 4th problem.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
WITH all_ids AS (
SELECT ra.requester_id AS id, COUNT(*) AS cnt
FROM RequestAccepted ra
GROUP BY ra.requester_id

UNION ALL

SELECT ra.accepter_id AS id, COUNT(*) AS cnt
FROM RequestAccepted ra
GROUP BY ra.accepter_id
)

SELECT id, SUM(cnt) AS num
FROM all_ids
GROUP BY id
ORDER BY num DESC
LIMIT 1