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
12 changes: 12 additions & 0 deletions 01-TheNumberOfSeniorsAndJuniorsToJoinTheCompany.sql
Original file line number Diff line number Diff line change
@@ -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)
58 changes: 58 additions & 0 deletions 02-LeagueStatistics.sql
Original file line number Diff line number Diff line change
@@ -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
21 changes: 21 additions & 0 deletions 03-SalesPerson.sql
Original file line number Diff line number Diff line change
@@ -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')
27 changes: 27 additions & 0 deletions 04-FriendRequest-II.sql
Original file line number Diff line number Diff line change
@@ -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