Skip to content
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,62 @@
3465\. Find Products with Valid Serial Numbers

Easy

Table: `products`

+--------------+------------+
| Column Name | Type |
+--------------+------------+
| product_id | int |
| product_name | varchar |
| description | varchar |
+--------------+------------+
(product_id) is the unique key for this table.
Each row in the table represents a product with its unique ID, name, and description.

Write a solution to find all products whose description **contains a valid serial number** pattern. A valid serial number follows these rules:

* It starts with the letters **SN** (case-sensitive).
* Followed by exactly `4` digits.
* It must have a hyphen (-) **followed by exactly** `4` digits.
* The serial number must be within the description (it may not necessarily start at the beginning).

Return _the result table ordered by_ `product_id` _in **ascending** order_.

The result format is in the following example.

**Example:**

**Input:**

products table:

+------------+--------------+------------------------------------------------------+
| product_id | product_name | description |
+------------+--------------+------------------------------------------------------+
| 1 | Widget A | This is a sample product with SN1234-5678 |
| 2 | Widget B | A product with serial SN9876-1234 in the description |
| 3 | Widget C | Product SN1234-56789 is available now |
| 4 | Widget D | No serial number here |
| 5 | Widget E | Check out SN4321-8765 in this description |
+------------+--------------+------------------------------------------------------+

**Output:**

+------------+--------------+------------------------------------------------------+
| product_id | product_name | description |
+------------+--------------+------------------------------------------------------+
| 1 | Widget A | This is a sample product with SN1234-5678 |
| 2 | Widget B | A product with serial SN9876-1234 in the description |
| 5 | Widget E | Check out SN4321-8765 in this description |
+------------+--------------+------------------------------------------------------+

**Explanation:**

* **Product 1:** Valid serial number SN1234-5678
* **Product 2:** Valid serial number SN9876-1234
* **Product 3:** Invalid serial number SN1234-56789 (contains 5 digits after the hyphen)
* **Product 4:** No serial number in the description
* **Product 5:** Valid serial number SN4321-8765

The result table is ordered by product\_id in ascending order.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Write your MySQL query statement below
# #Easy #Database #2025_02_26_Time_292_ms_(90.91%)_Space_0.0_MB_(100.00%)
SELECT * FROM products WHERE description REGEXP 'SN[0-9]{4}-[0-9]{4}$'
OR description REGEXP 'SN[0-9]{4}-[0-9]{4}[^0-9]+' ORDER BY product_id
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package g3401_3500.s3465_find_products_with_valid_serial_numbers

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 products (" +
" product_id INT," +
" product_name VARCHAR(50)," +
" description VARCHAR(100)" +
");" +
"insert into products (product_id, product_name, description) values " +
"(1, 'Widget A', 'This is a sample product with SN1234-5678');" +
"insert into products (product_id, product_name, description) values " +
"(2, 'Widget B', 'A product with serial SN9876-1234 in the description');" +
"insert into products (product_id, product_name, description) values " +
"(3, 'Widget C', 'Product SN1234-56789 is available now');" +
"insert into products (product_id, product_name, description) values " +
"(4, 'Widget D', 'No serial number here');" +
"insert into products (product_id, product_name, description) values " +
"(5, 'Widget E', 'Check out SN4321-8765 in this description');"
),
],
)
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/" +
"s3465_find_products_with_valid_serial_numbers/" +
"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>("1"))
assertThat<String>(
resultSet.getNString(2),
equalTo<String>("Widget A"),
)
assertThat<String>(
resultSet.getNString(3),
equalTo<String>("This is a sample product with SN1234-5678"),
)
assertThat<Boolean>(resultSet.next(), equalTo<Boolean>(true))
assertThat<String>(resultSet.getNString(1), equalTo<String>("2"))
assertThat<String>(
resultSet.getNString(2),
equalTo<String>("Widget B"),
)
assertThat<String>(
resultSet.getNString(3),
equalTo<String>("A product with serial SN9876-1234 in the description"),
)
assertThat<Boolean>(resultSet.next(), equalTo<Boolean>(true))
assertThat<String>(resultSet.getNString(1), equalTo<String>("5"))
assertThat<String>(
resultSet.getNString(2),
equalTo<String>("Widget E"),
)
assertThat<String>(
resultSet.getNString(3),
equalTo<String>("Check out SN4321-8765 in this description"),
)
assertThat<Boolean>(resultSet.next(), equalTo<Boolean>(false))
}
}
}
}
}