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
34 changes: 34 additions & 0 deletions problem1-2004-seniors-and-juniors.sql
Original file line number Diff line number Diff line change
@@ -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'
42 changes: 42 additions & 0 deletions problem2-1841-league-stattistics.sql
Original file line number Diff line number Diff line change
@@ -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
8 changes: 8 additions & 0 deletions problem3-607-sales-person.sql
Original file line number Diff line number Diff line change
@@ -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'
)
15 changes: 15 additions & 0 deletions problem4-602-friend-request.sql
Original file line number Diff line number Diff line change
@@ -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