diff --git a/1st problem.sql b/1st problem.sql new file mode 100644 index 0000000..5636742 --- /dev/null +++ b/1st problem.sql @@ -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 \ No newline at end of file diff --git a/2nd problem.sql b/2nd problem.sql new file mode 100644 index 0000000..b08aacc --- /dev/null +++ b/2nd problem.sql @@ -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 \ No newline at end of file diff --git a/3rd problem.sql b/3rd problem.sql new file mode 100644 index 0000000..957598c --- /dev/null +++ b/3rd problem.sql @@ -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 \ No newline at end of file diff --git a/4th problem.sql b/4th problem.sql new file mode 100644 index 0000000..2c132da --- /dev/null +++ b/4th problem.sql @@ -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