Skip to content

Added task 3421 #1895

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jan 17, 2025
Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
3421\. Find Students Who Improved

Medium

Table: `Scores`

+-------------+---------+
| Column Name | Type |
+-------------+---------+
| student_id | int |
| subject | varchar |
| score | int |
| exam_date | varchar |
+-------------+---------+
(student_id, subject, exam_date) is the primary key for this table.
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).

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:

* Have taken exams in the **same subject** on at least two different dates
* Their **latest score** in that subject is **higher** than their **first score**

Return _the result table_ _ordered by_ `student_id,` `subject` _in **ascending** order_.

The result format is in the following example.

**Example:**

**Input:**

Scores table:

+------------+----------+-------+------------+
| student_id | subject | score | exam_date |
+------------+----------+-------+------------+
| 101 | Math | 70 | 15-01-2023 |
| 101 | Math | 85 | 15-02-2023 |
| 101 | Physics | 65 | 15-01-2023 |
| 101 | Physics | 60 | 15-02-2023 |
| 102 | Math | 80 | 15-01-2023 |
| 102 | Math | 85 | 15-02-2023 |
| 103 | Math | 90 | 15-01-2023 |
| 104 | Physics | 75 | 15-01-2023 |
| 104 | Physics | 85 | 15-02-2023 |
+------------+----------+-------+------------+

**Output:**

+------------+----------+-------------+--------------+
| student_id | subject | first_score | latest_score |
+------------+----------+-------------+--------------+
| 101 | Math | 70 | 85 |
| 102 | Math | 80 | 85 |
| 104 | Physics | 75 | 85 |
+------------+----------+-------------+--------------+

**Explanation:**

* Student 101 in Math: Improved from 70 to 85
* Student 101 in Physics: No improvement (dropped from 65 to 60)
* Student 102 in Math: Improved from 80 to 85
* Student 103 in Math: Only one exam, not eligible
* Student 104 in Physics: Improved from 75 to 85

Result table is ordered by student\_id, subject.
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Write your MySQL query statement below
# #Medium #Database #2025_01_17_Time_466_ms_(74.56%)_Space_0B_(100.00%)

WITH Ranked AS (
SELECT
student_id,
subject,
FIRST_VALUE(score) OVER(PARTITION BY student_id,subject ORDER BY exam_date) AS first_score,
FIRST_VALUE(score) OVER(PARTITION BY student_id,subject ORDER BY exam_date DESC) AS latest_score
FROM Scores
)

SELECT * FROM Ranked
WHERE first_score<latest_score
GROUP BY student_id,subject
ORDER BY student_id,subject
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package g3401_3500.s3421_find_students_who_improved;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.stream.Collectors;
import javax.sql.DataSource;
import org.junit.jupiter.api.Test;
import org.zapodot.junit.db.annotations.EmbeddedDatabase;
import org.zapodot.junit.db.annotations.EmbeddedDatabaseTest;
import org.zapodot.junit.db.common.CompatibilityMode;

@EmbeddedDatabaseTest(
compatibilityMode = CompatibilityMode.MySQL,
initialSqls =
" CREATE TABLE Scores ("
+ " student_id INT,"
+ " subject VARCHAR(50),"
+ " score INT,"
+ " exam_date VARCHAR(10)"
+ ");"
+ "insert into Scores (student_id, subject, score, exam_date) values "
+ "('101', 'Math', '70', '15-01-2023');"
+ "insert into Scores (student_id, subject, score, exam_date) values "
+ "('101', 'Math', '85', '15-02-2023');"
+ "insert into Scores (student_id, subject, score, exam_date) values "
+ "('101', 'Physics', '65', '15-01-2023');"
+ "insert into Scores (student_id, subject, score, exam_date) values "
+ "('101', 'Physics', '60', '15-02-2023');"
+ "insert into Scores (student_id, subject, score, exam_date) values "
+ "('102', 'Math', '80', '15-01-2023');"
+ "insert into Scores (student_id, subject, score, exam_date) values "
+ "('102', 'Math', '85', '15-02-2023');"
+ "insert into Scores (student_id, subject, score, exam_date) values "
+ "('103', 'Math', '90', '15-01-2023');"
+ "insert into Scores (student_id, subject, score, exam_date) values "
+ "('104', 'Physics', '75', '15-01-2023');"
+ "insert into Scores (student_id, subject, score, exam_date) values "
+ "('104', 'Physics', '85', '15-02-2023');")
class MysqlTest {
@Test
void testScript(@EmbeddedDatabase DataSource dataSource)
throws SQLException, FileNotFoundException {
try (final Connection connection = dataSource.getConnection()) {
try (final Statement statement = connection.createStatement();
final ResultSet resultSet =
statement.executeQuery(
new BufferedReader(
new FileReader(
"src/main/java/g3401_3500/"
+ "s3421_find_students_who_improved/script.sql"))
.lines()
.collect(Collectors.joining("\n"))
.replaceAll("#.*?\\r?\\n", ""))) {
assertThat(resultSet.next(), equalTo(true));
assertThat(resultSet.getNString(1), equalTo("101"));
assertThat(resultSet.getNString(2), equalTo("Math"));
assertThat(resultSet.getNString(3), equalTo("70"));
assertThat(resultSet.getNString(4), equalTo("85"));
assertThat(resultSet.next(), equalTo(true));
assertThat(resultSet.getNString(1), equalTo("102"));
assertThat(resultSet.getNString(2), equalTo("Math"));
assertThat(resultSet.getNString(3), equalTo("80"));
assertThat(resultSet.getNString(4), equalTo("85"));
assertThat(resultSet.next(), equalTo(true));
assertThat(resultSet.getNString(1), equalTo("104"));
assertThat(resultSet.getNString(2), equalTo("Physics"));
assertThat(resultSet.getNString(3), equalTo("75"));
assertThat(resultSet.getNString(4), equalTo("85"));
assertThat(resultSet.next(), equalTo(false));
}
}
}
}
Loading