Skip to content

Commit a3972ac

Browse files
authored
Medium set started- 4 Questions attempted
1 parent e3ece4b commit a3972ac

File tree

4 files changed

+224
-0
lines changed

4 files changed

+224
-0
lines changed

Medium/Consecutive Numbers.sql

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
-- Question 52
2+
-- Write a SQL query to find all numbers that appear at least three times consecutively.
3+
4+
-- +----+-----+
5+
-- | Id | Num |
6+
-- +----+-----+
7+
-- | 1 | 1 |
8+
-- | 2 | 1 |
9+
-- | 3 | 1 |
10+
-- | 4 | 2 |
11+
-- | 5 | 1 |
12+
-- | 6 | 2 |
13+
-- | 7 | 2 |
14+
-- +----+-----+
15+
-- For example, given the above Logs table, 1 is the only number that appears consecutively for at least three times.
16+
17+
-- +-----------------+
18+
-- | ConsecutiveNums |
19+
-- +-----------------+
20+
-- | 1 |
21+
-- +-----------------+
22+
23+
-- Solution
24+
select distinct a.num as ConsecutiveNums
25+
from(
26+
select *,
27+
lag(num) over() as prev,
28+
lead(num) over() as next
29+
from logs) a
30+
where a.num = a.prev and a.num=a.next

Medium/Nth Highest salary.sql

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
-- Question 50
2+
-- Write a SQL query to get the nth highest salary from the Employee table.
3+
4+
-- +----+--------+
5+
-- | Id | Salary |
6+
-- +----+--------+
7+
-- | 1 | 100 |
8+
-- | 2 | 200 |
9+
-- | 3 | 300 |
10+
-- +----+--------+
11+
-- For example, given the above Employee table, the nth highest salary where n = 2 is 200. If there is no nth highest salary, then the query should return null.
12+
13+
-- +------------------------+
14+
-- | getNthHighestSalary(2) |
15+
-- +------------------------+
16+
-- | 200 |
17+
-- +------------------------+
18+
19+
-- Solution
20+
CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT
21+
BEGIN
22+
RETURN (
23+
# Write your MySQL query statement below.
24+
select distinct a.salary
25+
from
26+
(select salary,
27+
dense_rank() over(order by salary desc) as rk
28+
from Employee) a
29+
where a.rk = N
30+
);
31+
END

Medium/Rank scores.sql

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
-- Question 51
2+
-- Write a SQL query to rank scores.
3+
-- If there is a tie between two scores, both should have the same ranking.
4+
-- Note that after a tie, the next ranking number should be the next consecutive integer value.
5+
-- In other words, there should be no "holes" between ranks.
6+
7+
-- +----+-------+
8+
-- | Id | Score |
9+
-- +----+-------+
10+
-- | 1 | 3.50 |
11+
-- | 2 | 3.65 |
12+
-- | 3 | 4.00 |
13+
-- | 4 | 3.85 |
14+
-- | 5 | 4.00 |
15+
-- | 6 | 3.65 |
16+
-- +----+-------+
17+
-- For example, given the above Scores table, your query should generate the following report (order by highest score):
18+
19+
-- +-------+---------+
20+
-- | score | Rank |
21+
-- +-------+---------+
22+
-- | 4.00 | 1 |
23+
-- | 4.00 | 1 |
24+
-- | 3.85 | 2 |
25+
-- | 3.65 | 3 |
26+
-- | 3.65 | 3 |
27+
-- | 3.50 | 4 |
28+
-- +-------+---------+
29+
-- Important Note: For MySQL solutions, to escape reserved words used as column names,
30+
-- you can use an apostrophe before and after the keyword. For example `Rank`.
31+
32+
-- Solution
33+
select Score,
34+
dense_rank() over(order by score desc) as "Rank"
35+
from scores
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
-- Question 53
2+
-- Table: Teams
3+
4+
-- +---------------+----------+
5+
-- | Column Name | Type |
6+
-- +---------------+----------+
7+
-- | team_id | int |
8+
-- | team_name | varchar |
9+
-- +---------------+----------+
10+
-- team_id is the primary key of this table.
11+
-- Each row of this table represents a single football team.
12+
-- Table: Matches
13+
14+
-- +---------------+---------+
15+
-- | Column Name | Type |
16+
-- +---------------+---------+
17+
-- | match_id | int |
18+
-- | host_team | int |
19+
-- | guest_team | int |
20+
-- | host_goals | int |
21+
-- | guest_goals | int |
22+
-- +---------------+---------+
23+
-- match_id is the primary key of this table.
24+
-- Each row is a record of a finished match between two different teams.
25+
-- Teams host_team and guest_team are represented by their IDs in the teams table (team_id) and they scored host_goals and guest_goals goals respectively.
26+
27+
28+
-- You would like to compute the scores of all teams after all matches. Points are awarded as follows:
29+
-- A team receives three points if they win a match (Score strictly more goals than the opponent team).
30+
-- A team receives one point if they draw a match (Same number of goals as the opponent team).
31+
-- A team receives no points if they lose a match (Score less goals than the opponent team).
32+
-- Write an SQL query that selects the team_id, team_name and num_points of each team in the tournament after all described matches. Result table should be ordered by num_points (decreasing order). In case of a tie, order the records by team_id (increasing order).
33+
34+
-- The query result format is in the following example:
35+
36+
-- Teams table:
37+
-- +-----------+--------------+
38+
-- | team_id | team_name |
39+
-- +-----------+--------------+
40+
-- | 10 | Leetcode FC |
41+
-- | 20 | NewYork FC |
42+
-- | 30 | Atlanta FC |
43+
-- | 40 | Chicago FC |
44+
-- | 50 | Toronto FC |
45+
-- +-----------+--------------+
46+
47+
-- Matches table:
48+
-- +------------+--------------+---------------+-------------+--------------+
49+
-- | match_id | host_team | guest_team | host_goals | guest_goals |
50+
-- +------------+--------------+---------------+-------------+--------------+
51+
-- | 1 | 10 | 20 | 3 | 0 |
52+
-- | 2 | 30 | 10 | 2 | 2 |
53+
-- | 3 | 10 | 50 | 5 | 1 |
54+
-- | 4 | 20 | 30 | 1 | 0 |
55+
-- | 5 | 50 | 30 | 1 | 0 |
56+
-- +------------+--------------+---------------+-------------+--------------+
57+
58+
-- Result table:
59+
-- +------------+--------------+---------------+
60+
-- | team_id | team_name | num_points |
61+
-- +------------+--------------+---------------+
62+
-- | 10 | Leetcode FC | 7 |
63+
-- | 20 | NewYork FC | 3 |
64+
-- | 50 | Toronto FC | 3 |
65+
-- | 30 | Atlanta FC | 1 |
66+
-- | 40 | Chicago FC | 0 |
67+
-- +------------+--------------+---------------+
68+
69+
-- Solution
70+
with t1 as(
71+
Select c.host_id, c.host_name, c.host_points
72+
from(
73+
select a.match_id, a.team_id as host_id, a.team_name as host_name, b.team_id as guest_id, b.team_name as guest_name, a.host_goals, a.guest_goals,
74+
case
75+
when a.host_goals > a.guest_goals then 3
76+
when a.host_goals = a.guest_goals then 1
77+
else 0
78+
end as host_points,
79+
case
80+
when a.host_goals < a.guest_goals then 3
81+
when a.host_goals = a.guest_goals then 1
82+
else 0
83+
end as guest_points
84+
from(
85+
select *
86+
from matches m
87+
join teams t
88+
on t.team_id = m.host_team) a
89+
join
90+
(select *
91+
from matches m
92+
join teams t
93+
on t.team_id = m.guest_team) b
94+
on a.match_id = b.match_id) c
95+
union all
96+
Select d.guest_id, d.guest_name, d.guest_points
97+
from(
98+
select a.match_id, a.team_id as host_id, a.team_name as host_name, b.team_id as guest_id, b.team_name as guest_name, a.host_goals, a.guest_goals,
99+
case
100+
when a.host_goals > a.guest_goals then 3
101+
when a.host_goals = a.guest_goals then 1
102+
else 0
103+
end as host_points,
104+
case
105+
when a.host_goals < a.guest_goals then 3
106+
when a.host_goals = a.guest_goals then 1
107+
else 0
108+
end as guest_points
109+
from(
110+
select *
111+
from matches m
112+
join teams t
113+
on t.team_id = m.host_team) a
114+
join
115+
(select *
116+
from matches m
117+
join teams t
118+
on t.team_id = m.guest_team) b
119+
on a.match_id = b.match_id) d)
120+
121+
Select team_id, team_name, coalesce(total,0) as num_points
122+
from teams t2
123+
left join(
124+
select host_id, host_name, sum(host_points) as total
125+
from t1
126+
group by host_id, host_name) e
127+
on t2.team_id = e.host_id
128+
order by num_points desc, team_id

0 commit comments

Comments
 (0)