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
21 changes: 21 additions & 0 deletions Sql4_Q1_SrJr_Union.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
with cte as (
select
employee_id
, experience
, salary
, row_number() over(partition by experience order by salary) as rownum
, sum(salary) over(partition by experience order by salary, employee_id) as cumsum
from Candidates
)
select
'Senior' as experience
, coalesce(count(employee_id),0) as accepted_candidates
from cte
where experience = 'Senior' and cumsum <= 70000
union
select
'Junior' as experience
, coalesce(count(employee_id),0) as accepted_candidates
from cte
where experience = 'Junior' and cumsum <= (select 70000 - coalesce(max(cumsum),0) from cte where experience = 'Senior'
and cumsum <= 70000)
29 changes: 29 additions & 0 deletions Sql4_Q2_LeagueStats.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
with home_away_master as
(
select
home_team_id as team_id
, home_team_goals as for_goals
, away_team_goals as against_goals
from
Matches
union all
select
away_team_id as team_id
, away_team_goals as for_goals
, home_team_goals as against_goals
from
Matches
)
select
t.team_name
, count(h.team_id) as matches_played
, sum(case when h.for_goals > h.against_goals then 3
when h.for_goals = h.against_goals then 1
else 0 end) as points
, sum(h.for_goals) as goal_for
, sum(h.against_goals) as goal_against
, sum(h.for_goals - h.against_goals) as goal_diff
from
Teams t join home_away_master h on t.team_id = h.team_id
group by h.team_id
order by 3 desc, 6 desc, 1
13 changes: 13 additions & 0 deletions Sql4_Q3_SalesPerson.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
with RED_sales_id as
(
select
o.sales_id
from
Orders o join Company c on c.com_id = o.com_id
where c.name = 'RED'
)
select
s.name as name
from
SalesPerson s
where s.sales_id not in (select distinct sales_id from RED_sales_id)
18 changes: 18 additions & 0 deletions Sql4_Q4_FriendReq_LIMIT.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
with acceptor_sender_master as
(
select
requester_id as id
from RequestAccepted
union all
select
accepter_id as id
from RequestAccepted
)
select
id
, count(id) as num
from
acceptor_sender_master
group by 1
order by 2 desc
limit 1
25 changes: 25 additions & 0 deletions Sql4_Q4_FriendReq_MAX.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
with acceptor_sender_master as
(
select
requester_id as id
from RequestAccepted
union all
select
accepter_id as id
from RequestAccepted
)
, count_table as
(
select
id
, count(id) as num
from
acceptor_sender_master
group by 1
)
select
id
, num
from
count_table
where num = (select max(num) from count_table)
17 changes: 17 additions & 0 deletions Sql4_Q4_FriendReq_Window.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
with acceptor_sender_master as
(
select
requester_id as id
from RequestAccepted
union all
select
accepter_id as id
from RequestAccepted
)
select
distinct id
, count(id) over(partition by id) as num
from
acceptor_sender_master
order by 2 desc
limit 1