From 1ef53850d46b8524043210954b170f6c6f5a0a7e Mon Sep 17 00:00:00 2001 From: tejas274 Date: Wed, 28 May 2025 12:29:32 -0400 Subject: [PATCH 1/3] feat: problem 1 and problem 2 added --- problem1-2004-seniors-and-juniors.sql | 25 ++++++++++++++++ problem2-1841-league-stattistics.sql | 43 +++++++++++++++++++++++++++ 2 files changed, 68 insertions(+) create mode 100644 problem1-2004-seniors-and-juniors.sql create mode 100644 problem2-1841-league-stattistics.sql diff --git a/problem1-2004-seniors-and-juniors.sql b/problem1-2004-seniors-and-juniors.sql new file mode 100644 index 0000000..4b94cde --- /dev/null +++ b/problem1-2004-seniors-and-juniors.sql @@ -0,0 +1,25 @@ +with running_sum_salary AS (select +employee_id, +experience, +salary, +sum(salary) over (partition by experience order by experience desc,salary,employee_id) AS running_sum +from +Candidates) + + +select +'Senior' AS experience, +count(employee_id) AS accepted_candidates +from running_sum_salary +where running_sum <= 70000 and experience = 'Senior' + + +union all + + +select +'Junior' AS experience, +count(employee_id) AS accepted_candidates +from running_sum_salary +where running_sum <= 70000 - (select coalesce(max(running_sum),0) from running_sum_salary where running_sum <= 70000 and experience = 'Senior') +and experience = 'Junior' \ No newline at end of file diff --git a/problem2-1841-league-stattistics.sql b/problem2-1841-league-stattistics.sql new file mode 100644 index 0000000..d4e77cd --- /dev/null +++ b/problem2-1841-league-stattistics.sql @@ -0,0 +1,43 @@ +with team_stats_pivot AS +( + select + home_team_id as team, + home_team_goals as goal_for, + away_team_goals as goal_against, + case + when home_team_goals > away_team_goals then 3 + when home_team_goals < away_team_goals then 0 + else + 1 + END AS 'points' + from Matches + + union all + + select + away_team_id as team, + away_team_goals as goal_for, + home_team_goals as goal_against, + case + when away_team_goals > home_team_goals then 3 + when away_team_goals < home_team_goals then 0 + else + 1 + END AS 'points' + from Matches + +) + +select +Teams.team_name, +count(ts.team) as matches_played, +sum(ts.points) as points, +sum(ts.goal_for) as goal_for, +sum(ts.goal_against) as goal_against, +sum(ts.goal_for) - sum(ts.goal_against) AS goal_diff +from team_stats_pivot ts +inner join +Teams +on ts.team = Teams.team_id +group by team +order by points desc, goal_diff desc,team_name \ No newline at end of file From a9a32c08f579f72ba2bf9e1075e67448ee621b0b Mon Sep 17 00:00:00 2001 From: tejas274 Date: Thu, 29 May 2025 16:51:16 -0400 Subject: [PATCH 2/3] feat: problem 3 and 4 added --- problem3-607-sales-person.sql | 8 ++++++++ problem4-602-friend-request.sql | 15 +++++++++++++++ 2 files changed, 23 insertions(+) create mode 100644 problem3-607-sales-person.sql create mode 100644 problem4-602-friend-request.sql diff --git a/problem3-607-sales-person.sql b/problem3-607-sales-person.sql new file mode 100644 index 0000000..a99bfda --- /dev/null +++ b/problem3-607-sales-person.sql @@ -0,0 +1,8 @@ +SELECT s.name +FROM SalesPerson s +WHERE s.sales_id NOT IN ( + SELECT o.sales_id + FROM Orders o + INNER JOIN Company c ON o.com_id = c.com_id + WHERE c.name = 'RED' +) \ No newline at end of file diff --git a/problem4-602-friend-request.sql b/problem4-602-friend-request.sql new file mode 100644 index 0000000..0874dfd --- /dev/null +++ b/problem4-602-friend-request.sql @@ -0,0 +1,15 @@ +with cte_friends as +( + select requester_id as id + from RequestAccepted + union all + select accepter_id as id + from RequestAccepted +) + +select distinct id, +count(id) as num +from cte_friends +group by id +order by num desc +limit 1 \ No newline at end of file From b8cd20417ddada1e36db418f2318b84add8f49d3 Mon Sep 17 00:00:00 2001 From: tejas274 Date: Thu, 29 May 2025 16:56:07 -0400 Subject: [PATCH 3/3] feat: correcting formatting of query --- problem1-2004-seniors-and-juniors.sql | 43 ++++++++++-------- problem2-1841-league-stattistics.sql | 63 +++++++++++++-------------- problem3-607-sales-person.sql | 6 +-- problem4-602-friend-request.sql | 14 +++--- 4 files changed, 67 insertions(+), 59 deletions(-) diff --git a/problem1-2004-seniors-and-juniors.sql b/problem1-2004-seniors-and-juniors.sql index 4b94cde..d835830 100644 --- a/problem1-2004-seniors-and-juniors.sql +++ b/problem1-2004-seniors-and-juniors.sql @@ -1,25 +1,34 @@ -with running_sum_salary AS (select -employee_id, -experience, -salary, -sum(salary) over (partition by experience order by experience desc,salary,employee_id) AS running_sum -from -Candidates) - +with running_sum_salary as ( + select + employee_id, + experience, + salary, + sum(salary) + over ( + partition by experience + order by experience desc, salary asc, employee_id asc + ) + as running_sum + from + candidates +) select -'Senior' AS experience, -count(employee_id) AS accepted_candidates + 'Senior' as experience, + count(employee_id) as accepted_candidates from running_sum_salary -where running_sum <= 70000 and experience = 'Senior' - +where running_sum <= 70000 and experience = 'Senior' union all - select -'Junior' AS experience, -count(employee_id) AS accepted_candidates + 'Junior' as experience, + count(employee_id) as accepted_candidates from running_sum_salary -where running_sum <= 70000 - (select coalesce(max(running_sum),0) from running_sum_salary where running_sum <= 70000 and experience = 'Senior') -and experience = 'Junior' \ No newline at end of file +where + running_sum + <= 70000 - ( + select coalesce(max(running_sum), 0) from running_sum_salary + where running_sum <= 70000 and experience = 'Senior' + ) + and experience = 'Junior' \ No newline at end of file diff --git a/problem2-1841-league-stattistics.sql b/problem2-1841-league-stattistics.sql index d4e77cd..1269316 100644 --- a/problem2-1841-league-stattistics.sql +++ b/problem2-1841-league-stattistics.sql @@ -1,43 +1,42 @@ -with team_stats_pivot AS -( +with team_stats_pivot as ( select - home_team_id as team, - home_team_goals as goal_for, - away_team_goals as goal_against, - case - when home_team_goals > away_team_goals then 3 - when home_team_goals < away_team_goals then 0 - else - 1 - END AS 'points' - from Matches + home_team_id as team, + home_team_goals as goal_for, + away_team_goals as goal_against, + case + when home_team_goals > away_team_goals then 3 + when home_team_goals < away_team_goals then 0 + else + 1 + end as 'points' + from matches union all select - away_team_id as team, - away_team_goals as goal_for, - home_team_goals as goal_against, - case - when away_team_goals > home_team_goals then 3 - when away_team_goals < home_team_goals then 0 - else - 1 - END AS 'points' - from Matches + away_team_id as team, + away_team_goals as goal_for, + home_team_goals as goal_against, + case + when away_team_goals > home_team_goals then 3 + when away_team_goals < home_team_goals then 0 + else + 1 + end as 'points' + from matches ) select -Teams.team_name, -count(ts.team) as matches_played, -sum(ts.points) as points, -sum(ts.goal_for) as goal_for, -sum(ts.goal_against) as goal_against, -sum(ts.goal_for) - sum(ts.goal_against) AS goal_diff -from team_stats_pivot ts + teams.team_name, + count(ts.team) as matches_played, + sum(ts.points) as points, + sum(ts.goal_for) as goal_for, + sum(ts.goal_against) as goal_against, + sum(ts.goal_for) - sum(ts.goal_against) as goal_diff +from team_stats_pivot as ts inner join -Teams -on ts.team = Teams.team_id + teams + on ts.team = teams.team_id group by team -order by points desc, goal_diff desc,team_name \ No newline at end of file +order by points desc, goal_diff desc, team_name asc \ No newline at end of file diff --git a/problem3-607-sales-person.sql b/problem3-607-sales-person.sql index a99bfda..aafee91 100644 --- a/problem3-607-sales-person.sql +++ b/problem3-607-sales-person.sql @@ -1,8 +1,8 @@ SELECT s.name -FROM SalesPerson s +FROM salesperson AS s WHERE s.sales_id NOT IN ( SELECT o.sales_id - FROM Orders o - INNER JOIN Company c ON o.com_id = c.com_id + FROM orders AS o + INNER JOIN company AS c ON o.com_id = c.com_id WHERE c.name = 'RED' ) \ No newline at end of file diff --git a/problem4-602-friend-request.sql b/problem4-602-friend-request.sql index 0874dfd..f4fa176 100644 --- a/problem4-602-friend-request.sql +++ b/problem4-602-friend-request.sql @@ -1,15 +1,15 @@ -with cte_friends as -( +with cte_friends as ( select requester_id as id - from RequestAccepted + from requestaccepted union all select accepter_id as id - from RequestAccepted + from requestaccepted ) -select distinct id, -count(id) as num +select distinct + id, + count(id) as num from cte_friends group by id order by num desc -limit 1 \ No newline at end of file +limit 1