Skip to content

Commit 0e72bbf

Browse files
authored
Add files via upload
1 parent 46fd28e commit 0e72bbf

File tree

5 files changed

+322
-0
lines changed

5 files changed

+322
-0
lines changed

Medium/Capital Gain.sql

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
-- Question 61
2+
-- Table: Stocks
3+
4+
-- +---------------+---------+
5+
-- | Column Name | Type |
6+
-- +---------------+---------+
7+
-- | stock_name | varchar |
8+
-- | operation | enum |
9+
-- | operation_day | int |
10+
-- | price | int |
11+
-- +---------------+---------+
12+
-- (stock_name, day) is the primary key for this table.
13+
-- The operation column is an ENUM of type ('Sell', 'Buy')
14+
-- Each row of this table indicates that the stock which has stock_name had an operation on the day operation_day with the price.
15+
-- It is guaranteed that each 'Sell' operation for a stock has a corresponding 'Buy' operation in a previous day.
16+
17+
18+
-- Write an SQL query to report the Capital gain/loss for each stock.
19+
20+
-- The capital gain/loss of a stock is total gain or loss after buying and selling the stock one or many times.
21+
22+
-- Return the result table in any order.
23+
24+
-- The query result format is in the following example:
25+
26+
-- Stocks table:
27+
-- +---------------+-----------+---------------+--------+
28+
-- | stock_name | operation | operation_day | price |
29+
-- +---------------+-----------+---------------+--------+
30+
-- | Leetcode | Buy | 1 | 1000 |
31+
-- | Corona Masks | Buy | 2 | 10 |
32+
-- | Leetcode | Sell | 5 | 9000 |
33+
-- | Handbags | Buy | 17 | 30000 |
34+
-- | Corona Masks | Sell | 3 | 1010 |
35+
-- | Corona Masks | Buy | 4 | 1000 |
36+
-- | Corona Masks | Sell | 5 | 500 |
37+
-- | Corona Masks | Buy | 6 | 1000 |
38+
-- | Handbags | Sell | 29 | 7000 |
39+
-- | Corona Masks | Sell | 10 | 10000 |
40+
-- +---------------+-----------+---------------+--------+
41+
42+
-- Result table:
43+
-- +---------------+-------------------+
44+
-- | stock_name | capital_gain_loss |
45+
-- +---------------+-------------------+
46+
-- | Corona Masks | 9500 |
47+
-- | Leetcode | 8000 |
48+
-- | Handbags | -23000 |
49+
-- +---------------+-------------------+
50+
-- Leetcode stock was bought at day 1 for 1000$ and was sold at day 5 for 9000$. Capital gain = 9000 - 1000 = 8000$.
51+
-- Handbags stock was bought at day 17 for 30000$ and was sold at day 29 for 7000$. Capital loss = 7000 - 30000 = -23000$.
52+
-- Corona Masks stock was bought at day 1 for 10$ and was sold at day 3 for 1010$. It was bought again at day 4 for 1000$ and was sold at day 5 for 500$. At last, it was bought at day 6 for 1000$ and was sold at day 10 for 10000$. Capital gain/loss is the sum of capital gains/losses for each ('Buy' --> 'Sell')
53+
-- operation = (1010 - 10) + (500 - 1000) + (10000 - 1000) = 1000 - 500 + 9000 = 9500$.
54+
55+
-- Solution
56+
select stock_name, (one-two) as capital_gain_loss
57+
from(
58+
(select stock_name, sum(price) as one
59+
from stocks
60+
where operation = 'Sell'
61+
group by stock_name) b
62+
left join
63+
(select stock_name as name, sum(price) as two
64+
from stocks
65+
where operation = 'Buy'
66+
group by stock_name) c
67+
on b.stock_name = c.name)
68+
order by capital_gain_loss desc

Medium/Friend Requests 2.sql

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
-- Question 60
2+
-- In social network like Facebook or Twitter, people send friend requests and accept others' requests as well.
3+
4+
-- Table request_accepted
5+
6+
-- +--------------+-------------+------------+
7+
-- | requester_id | accepter_id | accept_date|
8+
-- |--------------|-------------|------------|
9+
-- | 1 | 2 | 2016_06-03 |
10+
-- | 1 | 3 | 2016-06-08 |
11+
-- | 2 | 3 | 2016-06-08 |
12+
-- | 3 | 4 | 2016-06-09 |
13+
-- +--------------+-------------+------------+
14+
-- This table holds the data of friend acceptance, while requester_id and accepter_id both are the id of a person.
15+
16+
17+
-- Write a query to find the the people who has most friends and the most friends number under the following rules:
18+
19+
-- It is guaranteed there is only 1 people having the most friends.
20+
-- The friend request could only been accepted once, which mean there is no multiple records with the same requester_id and accepter_id value.
21+
-- For the sample data above, the result is:
22+
23+
-- Result table:
24+
-- +------+------+
25+
-- | id | num |
26+
-- |------|------|
27+
-- | 3 | 3 |
28+
-- +------+------+
29+
-- The person with id '3' is a friend of people '1', '2' and '4', so he has 3 friends in total, which is the most number than any others.
30+
31+
-- Solution
32+
select requester_id as id, b.total as num
33+
from(
34+
select requester_id, sum(one) as total
35+
from((
36+
select requester_id, count(distinct accepter_id) as one
37+
from request_accepted
38+
group by requester_id)
39+
union all
40+
(select accepter_id, count(distinct requester_id) as two
41+
from request_accepted
42+
group by accepter_id)) a
43+
group by requester_id
44+
order by total desc) b
45+
limit 1

Medium/Game Play Analysis 3.sql

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
-- Question 62
2+
-- Table: Activity
3+
4+
-- +--------------+---------+
5+
-- | Column Name | Type |
6+
-- +--------------+---------+
7+
-- | player_id | int |
8+
-- | device_id | int |
9+
-- | event_date | date |
10+
-- | games_played | int |
11+
-- +--------------+---------+
12+
-- (player_id, event_date) is the primary key of this table.
13+
-- This table shows the activity of players of some game.
14+
-- Each row is a record of a player who logged in and played a number of games (possibly 0) before logging out on some day using some device.
15+
16+
17+
-- Write an SQL query that reports for each player and date, how many games played so far by the player. That is, the total number of games played by the player until that date. Check the example for clarity.
18+
19+
-- The query result format is in the following example:
20+
21+
-- Activity table:
22+
-- +-----------+-----------+------------+--------------+
23+
-- | player_id | device_id | event_date | games_played |
24+
-- +-----------+-----------+------------+--------------+
25+
-- | 1 | 2 | 2016-03-01 | 5 |
26+
-- | 1 | 2 | 2016-05-02 | 6 |
27+
-- | 1 | 3 | 2017-06-25 | 1 |
28+
-- | 3 | 1 | 2016-03-02 | 0 |
29+
-- | 3 | 4 | 2018-07-03 | 5 |
30+
-- +-----------+-----------+------------+--------------+
31+
32+
-- Result table:
33+
-- +-----------+------------+---------------------+
34+
-- | player_id | event_date | games_played_so_far |
35+
-- +-----------+------------+---------------------+
36+
-- | 1 | 2016-03-01 | 5 |
37+
-- | 1 | 2016-05-02 | 11 |
38+
-- | 1 | 2017-06-25 | 12 |
39+
-- | 3 | 2016-03-02 | 0 |
40+
-- | 3 | 2018-07-03 | 5 |
41+
-- +-----------+------------+---------------------+
42+
-- For the player with id 1, 5 + 6 = 11 games played by 2016-05-02, and 5 + 6 + 1 = 12 games played by 2017-06-25.
43+
-- For the player with id 3, 0 + 5 = 5 games played by 2018-07-03.
44+
-- Note that for each player we only care about the days when the player logged in.
45+
46+
-- Solution
47+
select player_id, event_date,
48+
sum(games_played) over(partition by player_id order by event_date) as games_played_so_far
49+
from activity
50+
order by 1,2
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
-- Question 63
2+
-- Table: Enrollments
3+
4+
-- +---------------+---------+
5+
-- | Column Name | Type |
6+
-- +---------------+---------+
7+
-- | student_id | int |
8+
-- | course_id | int |
9+
-- | grade | int |
10+
-- +---------------+---------+
11+
-- (student_id, course_id) is the primary key of this table.
12+
13+
-- Write a SQL query to find the highest grade with its corresponding course for each student. In case of a tie, you should find the course with the smallest course_id. The output must be sorted by increasing student_id.
14+
15+
-- The query result format is in the following example:
16+
17+
-- Enrollments table:
18+
-- +------------+-------------------+
19+
-- | student_id | course_id | grade |
20+
-- +------------+-----------+-------+
21+
-- | 2 | 2 | 95 |
22+
-- | 2 | 3 | 95 |
23+
-- | 1 | 1 | 90 |
24+
-- | 1 | 2 | 99 |
25+
-- | 3 | 1 | 80 |
26+
-- | 3 | 2 | 75 |
27+
-- | 3 | 3 | 82 |
28+
-- +------------+-----------+-------+
29+
30+
-- Result table:
31+
-- +------------+-------------------+
32+
-- | student_id | course_id | grade |
33+
-- +------------+-----------+-------+
34+
-- | 1 | 2 | 99 |
35+
-- | 2 | 2 | 95 |
36+
-- | 3 | 3 | 82 |
37+
-- +------------+-----------+-------+
38+
39+
-- Solution
40+
select student_id, course_id, grade
41+
from(
42+
select student_id, course_id, grade,
43+
rank() over(partition by student_id order by grade desc, course_id) as rk
44+
from enrollments) a
45+
where a.rk = 1

Medium/Movie Rating.sql

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
-- Question 59
2+
-- Table: Movies
3+
4+
-- +---------------+---------+
5+
-- | Column Name | Type |
6+
-- +---------------+---------+
7+
-- | movie_id | int |
8+
-- | title | varchar |
9+
-- +---------------+---------+
10+
-- movie_id is the primary key for this table.
11+
-- title is the name of the movie.
12+
-- Table: Users
13+
14+
-- +---------------+---------+
15+
-- | Column Name | Type |
16+
-- +---------------+---------+
17+
-- | user_id | int |
18+
-- | name | varchar |
19+
-- +---------------+---------+
20+
-- user_id is the primary key for this table.
21+
-- Table: Movie_Rating
22+
23+
-- +---------------+---------+
24+
-- | Column Name | Type |
25+
-- +---------------+---------+
26+
-- | movie_id | int |
27+
-- | user_id | int |
28+
-- | rating | int |
29+
-- | created_at | date |
30+
-- +---------------+---------+
31+
-- (movie_id, user_id) is the primary key for this table.
32+
-- This table contains the rating of a movie by a user in their review.
33+
-- created_at is the user's review date.
34+
35+
36+
-- Write the following SQL query:
37+
38+
-- Find the name of the user who has rated the greatest number of the movies.
39+
-- In case of a tie, return lexicographically smaller user name.
40+
41+
-- Find the movie name with the highest average rating in February 2020.
42+
-- In case of a tie, return lexicographically smaller movie name.
43+
44+
-- Query is returned in 2 rows, the query result format is in the folowing example:
45+
46+
-- Movies table:
47+
-- +-------------+--------------+
48+
-- | movie_id | title |
49+
-- +-------------+--------------+
50+
-- | 1 | Avengers |
51+
-- | 2 | Frozen 2 |
52+
-- | 3 | Joker |
53+
-- +-------------+--------------+
54+
55+
-- Users table:
56+
-- +-------------+--------------+
57+
-- | user_id | name |
58+
-- +-------------+--------------+
59+
-- | 1 | Daniel |
60+
-- | 2 | Monica |
61+
-- | 3 | Maria |
62+
-- | 4 | James |
63+
-- +-------------+--------------+
64+
65+
-- Movie_Rating table:
66+
-- +-------------+--------------+--------------+-------------+
67+
-- | movie_id | user_id | rating | created_at |
68+
-- +-------------+--------------+--------------+-------------+
69+
-- | 1 | 1 | 3 | 2020-01-12 |
70+
-- | 1 | 2 | 4 | 2020-02-11 |
71+
-- | 1 | 3 | 2 | 2020-02-12 |
72+
-- | 1 | 4 | 1 | 2020-01-01 |
73+
-- | 2 | 1 | 5 | 2020-02-17 |
74+
-- | 2 | 2 | 2 | 2020-02-01 |
75+
-- | 2 | 3 | 2 | 2020-03-01 |
76+
-- | 3 | 1 | 3 | 2020-02-22 |
77+
-- | 3 | 2 | 4 | 2020-02-25 |
78+
-- +-------------+--------------+--------------+-------------+
79+
80+
-- Result table:
81+
-- +--------------+
82+
-- | results |
83+
-- +--------------+
84+
-- | Daniel |
85+
-- | Frozen 2 |
86+
-- +--------------+
87+
88+
-- Daniel and Maria have rated 3 movies ("Avengers", "Frozen 2" and "Joker") but Daniel is smaller lexicographically.
89+
-- Frozen 2 and Joker have a rating average of 3.5 in February but Frozen 2 is smaller lexicographically.
90+
91+
-- Solution
92+
select name as results
93+
from(
94+
(select a.name
95+
from(
96+
select name, count(*),
97+
rank() over(order by count(*) desc) as rk
98+
from movie_rating m
99+
join users u
100+
on m.user_id = u.user_id
101+
group by name, m.user_id
102+
order by rk, name) a
103+
limit 1)
104+
union
105+
(select title
106+
from(
107+
select title, round(avg(rating),1) as rnd
108+
from movie_rating m
109+
join movies u
110+
on m.movie_id = u.movie_id
111+
where month(created_at) = 2
112+
group by title
113+
order by rnd desc, title) b
114+
limit 1)) as d

0 commit comments

Comments
 (0)