Find Products with Valid Serial Numbers - Problem

You're working as a data analyst for an e-commerce company that needs to identify products with valid serial numbers in their descriptions for inventory tracking purposes.

Given a products table containing product information, your task is to find all products whose descriptions contain a valid serial number pattern.

What makes a serial number valid?

  • Starts with exactly SN (case-sensitive - must be uppercase)
  • Followed by exactly 4 digits
  • Then a hyphen -
  • Followed by exactly 4 more digits
  • Example: SN1234-5678 ✅, sn1234-5678 ❌, SN123-5678

The serial number can appear anywhere within the description text - it doesn't need to be at the beginning.

Goal: Return all products with valid serial numbers, ordered by product_id in ascending order.

Input & Output

example_1.sql — Basic Pattern Matching
$ Input: products table: +------------+--------------+----------------------------------------+ | product_id | product_name | description | +------------+--------------+----------------------------------------+ | 1 | Laptop | Gaming laptop with SN1234-5678 | | 2 | Mouse | Wireless mouse, serial sn9999-0000 | | 3 | Keyboard | Mechanical keyboard SN4567-8901 new | +------------+--------------+----------------------------------------+
Output: +------------+--------------+----------------------------------------+ | product_id | product_name | description | +------------+--------------+----------------------------------------+ | 1 | Laptop | Gaming laptop with SN1234-5678 | | 3 | Keyboard | Mechanical keyboard SN4567-8901 new | +------------+--------------+----------------------------------------+
💡 Note: Product 1 and 3 have valid serial numbers (SN1234-5678 and SN4567-8901). Product 2 fails because 'sn' is lowercase, not uppercase 'SN'.
example_2.sql — Edge Cases
$ Input: products table: +------------+--------------+----------------------------------------+ | product_id | product_name | description | +------------+--------------+----------------------------------------+ | 4 | Monitor | Display SN123-4567 with short digits | | 5 | Speaker | Audio device SN12345-6789 extra digit | | 6 | Camera | Photo camera with SN0000-9999 | +------------+--------------+----------------------------------------+
Output: +------------+--------------+----------------------------------------+ | product_id | product_name | description | +------------+--------------+----------------------------------------+ | 6 | Camera | Photo camera with SN0000-9999 | +------------+--------------+----------------------------------------+
💡 Note: Only product 6 has a valid pattern. Product 4 has only 3 digits before hyphen, Product 5 has 5 digits before hyphen (both invalid).
example_3.sql — Multiple Serials
$ Input: products table: +------------+--------------+----------------------------------------+ | product_id | product_name | description | +------------+--------------+----------------------------------------+ | 7 | Tablet | No serial number here | | 8 | Phone | Device SN1111-2222 and SN3333-4444 | | 9 | Watch | Smart watch SN-1234-5678 extra hyphen | +------------+--------------+----------------------------------------+
Output: +------------+--------------+----------------------------------------+ | product_id | product_name | description | +------------+--------------+----------------------------------------+ | 8 | Phone | Device SN1111-2222 and SN3333-4444 | +------------+--------------+----------------------------------------+
💡 Note: Product 7 has no serial. Product 8 contains valid serials (having multiple valid patterns still counts as valid). Product 9 has extra hyphen making it invalid.

Constraints

  • 1 ≤ number of products ≤ 104
  • 1 ≤ product_id ≤ 106
  • 1 ≤ length of product_name ≤ 100
  • 1 ≤ length of description ≤ 1000
  • Serial number pattern is case-sensitive
  • Serial number can appear anywhere in the description

Visualization

Tap to expand
Serial Number Pattern Recognition SystemPattern ScannerSN[0-9]{4}-[0-9]{4}Product Descriptions Being Scanned:"Gaming laptop with SN1234-5678"✓ VALID"Wireless mouse serial sn9999-0000"✗ INVALID"Mechanical keyboard SN4567-8901 new"✓ VALID"Display SN123-4567 short digits"✗ INVALIDValid Products Found:Product ID: 1Gaming laptop with SN1234-5678Product ID: 3Mechanical keyboard SN4567-8901Results ordered by product_id ascending
Understanding the Visualization
1
Define Pattern
Set scanner to recognize SN####-#### format only
2
Scan Descriptions
Scanner checks each product description for the pattern
3
Pattern Match
When valid pattern found, product is flagged as valid
4
Collect Results
All flagged products are returned sorted by ID
Key Takeaway
🎯 Key Insight: Regular expressions provide an efficient, readable way to match complex string patterns in database queries, making them ideal for validation tasks like serial number format checking.
Asked in
Amazon 35 Google 28 Microsoft 22 Meta 18
23.4K Views
Medium Frequency
~12 min Avg. Time
842 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen