diff --git a/problem1-2004-seniors-and-juniors.sql b/problem1-2004-seniors-and-juniors.sql new file mode 100644 index 0000000..d835830 --- /dev/null +++ b/problem1-2004-seniors-and-juniors.sql @@ -0,0 +1,34 @@ +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 +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..1269316 --- /dev/null +++ b/problem2-1841-league-stattistics.sql @@ -0,0 +1,42 @@ +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 as ts +inner join + teams + on ts.team = teams.team_id +group by team +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 new file mode 100644 index 0000000..aafee91 --- /dev/null +++ b/problem3-607-sales-person.sql @@ -0,0 +1,8 @@ +SELECT s.name +FROM salesperson AS s +WHERE s.sales_id NOT IN ( + SELECT o.sales_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 new file mode 100644 index 0000000..f4fa176 --- /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