From ced041312384a5f1b7ae302ba2f51a58c884a35c Mon Sep 17 00:00:00 2001 From: sudheer11111 Date: Sat, 28 Dec 2024 07:38:52 +0530 Subject: [PATCH 1/2] 'completed' --- 1st problem.sql | 32 ++++++++++++++++++++++++++++++++ 2nd problem.sql | 12 ++++++++++++ 3rd problem.sql | 4 ++++ 4th problem.sql | 17 +++++++++++++++++ 4 files changed, 65 insertions(+) create mode 100644 1st problem.sql create mode 100644 2nd problem.sql create mode 100644 3rd problem.sql create mode 100644 4th problem.sql 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..871bc15 --- /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 -- don't remove duplicates + + 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 \ No newline at end of file From b40acca14fd5c362bd9c85d5231e82338a5c8a18 Mon Sep 17 00:00:00 2001 From: sudheer11111 <147435589+sudheer11111@users.noreply.github.com> Date: Sat, 28 Dec 2024 07:40:20 +0530 Subject: [PATCH 2/2] Update 4th problem.sql --- 4th problem.sql | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/4th problem.sql b/4th problem.sql index 871bc15..2c132da 100644 --- a/4th problem.sql +++ b/4th problem.sql @@ -3,7 +3,7 @@ WITH all_ids AS ( FROM RequestAccepted ra GROUP BY ra.requester_id - UNION ALL -- don't remove duplicates + UNION ALL SELECT ra.accepter_id AS id, COUNT(*) AS cnt FROM RequestAccepted ra @@ -14,4 +14,4 @@ SELECT id, SUM(cnt) AS num FROM all_ids GROUP BY id ORDER BY num DESC -LIMIT 1 \ No newline at end of file +LIMIT 1