Skip to content

Commit

Permalink
Added task 3421
Browse files Browse the repository at this point in the history
  • Loading branch information
javadev authored Jan 17, 2025
1 parent 15e30b8 commit 93ff703
Show file tree
Hide file tree
Showing 3 changed files with 165 additions and 0 deletions.
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,84 @@
package g3401_3500.s3421_find_students_who_improved

import org.hamcrest.CoreMatchers.equalTo
import org.hamcrest.MatcherAssert.assertThat
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
import java.io.BufferedReader
import java.io.FileNotFoundException
import java.io.FileReader
import java.sql.SQLException
import java.util.stream.Collectors
import javax.sql.DataSource

@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');"
),
],
)
internal class MysqlTest {
@Test
@Throws(SQLException::class, FileNotFoundException::class)
fun testScript(@EmbeddedDatabase dataSource: DataSource) {
dataSource.connection.use { connection ->
connection.createStatement().use { statement ->
statement.executeQuery(
BufferedReader(
FileReader(
"src/main/kotlin/g3401_3500/" +
"s3421_find_students_who_improved/script.sql",
),
)
.lines()
.collect(Collectors.joining("\n"))
.replace("#.*?\\r?\\n".toRegex(), ""),
).use { resultSet ->
assertThat<Boolean>(resultSet.next(), equalTo<Boolean>(true))
assertThat<String>(resultSet.getNString(1), equalTo<String>("101"))
assertThat<String>(resultSet.getNString(2), equalTo<String>("Math"))
assertThat<String>(resultSet.getNString(3), equalTo<String>("70"))
assertThat<String>(resultSet.getNString(4), equalTo<String>("85"))
assertThat<Boolean>(resultSet.next(), equalTo<Boolean>(true))
assertThat<String>(resultSet.getNString(1), equalTo<String>("102"))
assertThat<String>(resultSet.getNString(2), equalTo<String>("Math"))
assertThat<String>(resultSet.getNString(3), equalTo<String>("80"))
assertThat<String>(resultSet.getNString(4), equalTo<String>("85"))
assertThat<Boolean>(resultSet.next(), equalTo<Boolean>(true))
assertThat<String>(resultSet.getNString(1), equalTo<String>("104"))
assertThat<String>(resultSet.getNString(2), equalTo<String>("Physics"))
assertThat<String>(resultSet.getNString(3), equalTo<String>("75"))
assertThat<String>(resultSet.getNString(4), equalTo<String>("85"))
assertThat<Boolean>(resultSet.next(), equalTo<Boolean>(false))
}
}
}
}
}

0 comments on commit 93ff703

Please sign in to comment.