diff --git a/01-TheNumberOfSeniorsAndJuniorsToJoinTheCompany.sql b/01-TheNumberOfSeniorsAndJuniorsToJoinTheCompany.sql new file mode 100644 index 0000000..0c8673f --- /dev/null +++ b/01-TheNumberOfSeniorsAndJuniorsToJoinTheCompany.sql @@ -0,0 +1,12 @@ +-- Problem 1 : The Number of Seniors and Juniors to Join the Company (https://leetcode.com/problems/the-number-of-seniors-and-juniors-to-join-the-company/) + +with cte as( +select *, sum(salary) over (partition by experience order by salary, employee_id) as 'rsum' +from candidates) +select 'Senior' as experience, count(*) as 'accepted_candidates' +from cte +where experience='Senior' and rsum <= 70000 +union +select 'Junior' as experince, count(*) as 'accepted_candidates' +from cte +where experience = 'Junior' and rsum <= (select 70000 - ifnull(max(rsum),0) from cte where experience='Senior' and rsum <= 70000) \ No newline at end of file diff --git a/02-LeagueStatistics.sql b/02-LeagueStatistics.sql new file mode 100644 index 0000000..bb4c3f2 --- /dev/null +++ b/02-LeagueStatistics.sql @@ -0,0 +1,58 @@ +-- Problem 2 : League Statistics (https://leetcode.com/problems/league-statistics/ ) + +with cte as ( +select home_team_id as team_id, home_team_goals as goal_for, away_team_goals as goal_against +from matches +union all +select away_team_id as team_id, away_team_goals as goal_for, home_team_goals as goal_against +from matches) +select team_name, +count(cte.team_id) as matches_played, +sum( + case + when goal_for > goal_against then 3 + when goal_for = goal_against then 1 + else 0 + end +) + as points, +sum(goal_for) as goal_for, sum(goal_against) as goal_against, +sum(goal_for) - sum(goal_against) as goal_diff +from teams +join cte on teams.team_id=cte.team_id +group by cte.team_id +order by points desc, goal_diff desc, team_name + + +/* +just for my future reference! i could come up with the following solution at 1st which works but is too verbose and +unreadable. BUT I looked at the following solution and generated the above. so, it is a good reference for future +*/ +with matches_mod as( +select *, +(case +when home_team_goals > away_team_goals then 3 +when home_team_goals = away_team_goals then 1 +else 0 +end) as +home_team_points, +(case +when home_team_goals < away_team_goals then 3 +when home_team_goals = away_team_goals then 1 +else 0 +end) +away_team_points +from matches), +cte as ( +select home_team_id as team_id, home_team_points as points, home_team_goals as goal_for, away_team_goals as goal_against +from matches_mod +union all +select away_team_id as team_id, away_team_points as points, away_team_goals as goal_for, home_team_goals as goal_against +from matches_mod ) + +select team_name, count(cte.team_id) as matches_played, sum(points) as points, sum(goal_for) as goal_for, sum(goal_against) as goal_against, +sum(goal_for) - sum(goal_against) as goal_diff +from teams +join cte on teams.team_id=cte.team_id +group by cte.team_id +order by points desc, goal_diff desc, team_name diff --git a/03-SalesPerson.sql b/03-SalesPerson.sql new file mode 100644 index 0000000..f18c849 --- /dev/null +++ b/03-SalesPerson.sql @@ -0,0 +1,21 @@ +-- Problem 3 : Sales Person (https://leetcode.com/problems/sales-person/ ) + +-- using cte +with cte as ( +select order_id, o.com_id, name as c_name, sales_id +from company c +join orders o on c.com_id = o.com_id +) +select sp.name +from salesperson sp +left join cte c on sp.sales_id=c.sales_id +group by sp.sales_id +having sum(case when c.c_name='RED' then 1 else 0 end) = 0 + +-- more straight forward +select name from salesperson +where sales_id not in ( +select sales_id +from company c +join orders o on c.com_id = o.com_id +where c.name = 'RED') \ No newline at end of file diff --git a/04-FriendRequest-II.sql b/04-FriendRequest-II.sql new file mode 100644 index 0000000..ccfe7d2 --- /dev/null +++ b/04-FriendRequest-II.sql @@ -0,0 +1,27 @@ +-- Friend Requests II (https://leetcode.com/problems/friend-requests-ii-who-has-the-most-friends/ ) + +-- using cte +with cte as ( + select requester_id as id from RequestAccepted + union all + select accepter_id as id from RequestAccepted +), +friend_count as ( + select id, count(id) as num + from cte + group by id +) +select id, num +from friend_count +where num = (select max(num) from friend_count) -- it will also work if there are multiple people with max friends + +-- another appraoch +select id, count(id) as num +from ( + select requester_id as id from RequestAccepted + union all + select accepter_id as id from RequestAccepted +) as temp +group by id +order by num desc +limit 1 -- works because only one person has the most friends \ No newline at end of file