diff --git a/Sql4_Q1_SrJr_Union.sql b/Sql4_Q1_SrJr_Union.sql new file mode 100644 index 0000000..c879f8b --- /dev/null +++ b/Sql4_Q1_SrJr_Union.sql @@ -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) \ No newline at end of file diff --git a/Sql4_Q2_LeagueStats.sql b/Sql4_Q2_LeagueStats.sql new file mode 100644 index 0000000..ee7893b --- /dev/null +++ b/Sql4_Q2_LeagueStats.sql @@ -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 diff --git a/Sql4_Q3_SalesPerson.sql b/Sql4_Q3_SalesPerson.sql new file mode 100644 index 0000000..90bc0d8 --- /dev/null +++ b/Sql4_Q3_SalesPerson.sql @@ -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) \ No newline at end of file diff --git a/Sql4_Q4_FriendReq_LIMIT.sql b/Sql4_Q4_FriendReq_LIMIT.sql new file mode 100644 index 0000000..5bd38b8 --- /dev/null +++ b/Sql4_Q4_FriendReq_LIMIT.sql @@ -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 \ No newline at end of file diff --git a/Sql4_Q4_FriendReq_MAX.sql b/Sql4_Q4_FriendReq_MAX.sql new file mode 100644 index 0000000..cc28e47 --- /dev/null +++ b/Sql4_Q4_FriendReq_MAX.sql @@ -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) \ No newline at end of file diff --git a/Sql4_Q4_FriendReq_Window.sql b/Sql4_Q4_FriendReq_Window.sql new file mode 100644 index 0000000..524083a --- /dev/null +++ b/Sql4_Q4_FriendReq_Window.sql @@ -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 \ No newline at end of file