Skip to content

Added task 3465 #1922

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 2 commits into from
Feb 25, 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,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_25_Time_716_ms_(100.00%)_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,76 @@
package g3401_3500.s3465_find_products_with_valid_serial_numbers;

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 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');")
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/"
+ "s3465_find_products_with_valid_serial_numbers/"
+ "script.sql"))
.lines()
.collect(Collectors.joining("\n"))
.replaceAll("#.*?\\r?\\n", ""))) {
assertThat(resultSet.next(), equalTo(true));
assertThat(resultSet.getNString(1), equalTo("1"));
assertThat(resultSet.getNString(2), equalTo("Widget A"));
assertThat(
resultSet.getNString(3),
equalTo("This is a sample product with SN1234-5678"));
assertThat(resultSet.next(), equalTo(true));
assertThat(resultSet.getNString(1), equalTo("2"));
assertThat(resultSet.getNString(2), equalTo("Widget B"));
assertThat(
resultSet.getNString(3),
equalTo("A product with serial SN9876-1234 in the description"));
assertThat(resultSet.next(), equalTo(true));
assertThat(resultSet.getNString(1), equalTo("5"));
assertThat(resultSet.getNString(2), equalTo("Widget E"));
assertThat(
resultSet.getNString(3),
equalTo("Check out SN4321-8765 in this description"));
assertThat(resultSet.next(), equalTo(false));
}
}
}
}