Skip to content

Commit e9fc5eb

Browse files
authored
Added task 3421
1 parent 8eee0d6 commit e9fc5eb

File tree

3 files changed

+162
-0
lines changed

3 files changed

+162
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
3421\. Find Students Who Improved
2+
3+
Medium
4+
5+
Table: `Scores`
6+
7+
+-------------+---------+
8+
| Column Name | Type |
9+
+-------------+---------+
10+
| student_id | int |
11+
| subject | varchar |
12+
| score | int |
13+
| exam_date | varchar |
14+
+-------------+---------+
15+
(student_id, subject, exam_date) is the primary key for this table.
16+
Each row contains information about a student's score in a specific subject on a particular exam date. score is between 0 and 100 (inclusive).
17+
18+
Write a solution to find the **students who have shown improvement**. A student is considered to have shown improvement if they meet **both** of these conditions:
19+
20+
* Have taken exams in the **same subject** on at least two different dates
21+
* Their **latest score** in that subject is **higher** than their **first score**
22+
23+
Return _the result table_ _ordered by_ `student_id,` `subject` _in **ascending** order_.
24+
25+
The result format is in the following example.
26+
27+
**Example:**
28+
29+
**Input:**
30+
31+
Scores table:
32+
33+
+------------+----------+-------+------------+
34+
| student_id | subject | score | exam_date |
35+
+------------+----------+-------+------------+
36+
| 101 | Math | 70 | 15-01-2023 |
37+
| 101 | Math | 85 | 15-02-2023 |
38+
| 101 | Physics | 65 | 15-01-2023 |
39+
| 101 | Physics | 60 | 15-02-2023 |
40+
| 102 | Math | 80 | 15-01-2023 |
41+
| 102 | Math | 85 | 15-02-2023 |
42+
| 103 | Math | 90 | 15-01-2023 |
43+
| 104 | Physics | 75 | 15-01-2023 |
44+
| 104 | Physics | 85 | 15-02-2023 |
45+
+------------+----------+-------+------------+
46+
47+
**Output:**
48+
49+
+------------+----------+-------------+--------------+
50+
| student_id | subject | first_score | latest_score |
51+
+------------+----------+-------------+--------------+
52+
| 101 | Math | 70 | 85 |
53+
| 102 | Math | 80 | 85 |
54+
| 104 | Physics | 75 | 85 |
55+
+------------+----------+-------------+--------------+
56+
57+
**Explanation:**
58+
59+
* Student 101 in Math: Improved from 70 to 85
60+
* Student 101 in Physics: No improvement (dropped from 65 to 60)
61+
* Student 102 in Math: Improved from 80 to 85
62+
* Student 103 in Math: Only one exam, not eligible
63+
* Student 104 in Physics: Improved from 75 to 85
64+
65+
Result table is ordered by student\_id, subject.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Write your MySQL query statement below
2+
# #Medium #Database #2025_01_17_Time_466_ms_(74.56%)_Space_0B_(100.00%)
3+
4+
WITH Ranked AS (
5+
SELECT
6+
student_id,
7+
subject,
8+
FIRST_VALUE(score) OVER(PARTITION BY student_id,subject ORDER BY exam_date) AS first_score,
9+
FIRST_VALUE(score) OVER(PARTITION BY student_id,subject ORDER BY exam_date DESC) AS latest_score
10+
FROM Scores
11+
)
12+
13+
SELECT * FROM Ranked
14+
WHERE first_score<latest_score
15+
GROUP BY student_id,subject
16+
ORDER BY student_id,subject
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package g3401_3500.s3421_find_students_who_improved;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import java.io.BufferedReader;
7+
import java.io.FileNotFoundException;
8+
import java.io.FileReader;
9+
import java.sql.Connection;
10+
import java.sql.ResultSet;
11+
import java.sql.SQLException;
12+
import java.sql.Statement;
13+
import java.util.stream.Collectors;
14+
import javax.sql.DataSource;
15+
import org.junit.jupiter.api.Test;
16+
import org.zapodot.junit.db.annotations.EmbeddedDatabase;
17+
import org.zapodot.junit.db.annotations.EmbeddedDatabaseTest;
18+
import org.zapodot.junit.db.common.CompatibilityMode;
19+
20+
@EmbeddedDatabaseTest(
21+
compatibilityMode = CompatibilityMode.MySQL,
22+
initialSqls =
23+
" CREATE TABLE Scores ("
24+
+ " student_id INT,"
25+
+ " subject VARCHAR(50),"
26+
+ " score INT,"
27+
+ " exam_date VARCHAR(10)"
28+
+ ");"
29+
+ "insert into Scores (student_id, subject, score, exam_date) values "
30+
+ "('101', 'Math', '70', '15-01-2023');"
31+
+ "insert into Scores (student_id, subject, score, exam_date) values "
32+
+ "('101', 'Math', '85', '15-02-2023');"
33+
+ "insert into Scores (student_id, subject, score, exam_date) values "
34+
+ "('101', 'Physics', '65', '15-01-2023');"
35+
+ "insert into Scores (student_id, subject, score, exam_date) values "
36+
+ "('101', 'Physics', '60', '15-02-2023');"
37+
+ "insert into Scores (student_id, subject, score, exam_date) values "
38+
+ "('102', 'Math', '80', '15-01-2023');"
39+
+ "insert into Scores (student_id, subject, score, exam_date) values "
40+
+ "('102', 'Math', '85', '15-02-2023');"
41+
+ "insert into Scores (student_id, subject, score, exam_date) values "
42+
+ "('103', 'Math', '90', '15-01-2023');"
43+
+ "insert into Scores (student_id, subject, score, exam_date) values "
44+
+ "('104', 'Physics', '75', '15-01-2023');"
45+
+ "insert into Scores (student_id, subject, score, exam_date) values "
46+
+ "('104', 'Physics', '85', '15-02-2023');")
47+
class MysqlTest {
48+
@Test
49+
void testScript(@EmbeddedDatabase DataSource dataSource)
50+
throws SQLException, FileNotFoundException {
51+
try (final Connection connection = dataSource.getConnection()) {
52+
try (final Statement statement = connection.createStatement();
53+
final ResultSet resultSet =
54+
statement.executeQuery(
55+
new BufferedReader(
56+
new FileReader(
57+
"src/main/java/g3401_3500/"
58+
+ "s3421_find_students_who_improved/script.sql"))
59+
.lines()
60+
.collect(Collectors.joining("\n"))
61+
.replaceAll("#.*?\\r?\\n", ""))) {
62+
assertThat(resultSet.next(), equalTo(true));
63+
assertThat(resultSet.getNString(1), equalTo("101"));
64+
assertThat(resultSet.getNString(2), equalTo("Math"));
65+
assertThat(resultSet.getNString(3), equalTo("70"));
66+
assertThat(resultSet.getNString(4), equalTo("85"));
67+
assertThat(resultSet.next(), equalTo(true));
68+
assertThat(resultSet.getNString(1), equalTo("102"));
69+
assertThat(resultSet.getNString(2), equalTo("Math"));
70+
assertThat(resultSet.getNString(3), equalTo("80"));
71+
assertThat(resultSet.getNString(4), equalTo("85"));
72+
assertThat(resultSet.next(), equalTo(true));
73+
assertThat(resultSet.getNString(1), equalTo("104"));
74+
assertThat(resultSet.getNString(2), equalTo("Physics"));
75+
assertThat(resultSet.getNString(3), equalTo("75"));
76+
assertThat(resultSet.getNString(4), equalTo("85"));
77+
assertThat(resultSet.next(), equalTo(false));
78+
}
79+
}
80+
}
81+
}

0 commit comments

Comments
 (0)