You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
-- 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')
-- (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:
-- (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
0 commit comments