Find Products with Three Consecutive Digits - Problem
Given a products database, you need to identify products whose names contain exactly three consecutive digits in a sequence.
Your task is to write a SQL query that finds all products meeting this criteria. The challenge is to detect patterns like 123, 789, or 003 within product names, but reject patterns with more or fewer consecutive digits.
Key Points:
- Must be exactly 3 consecutive digits (not 2, not 4+)
- Digits must be in a row with no interruption
- Product names may contain multiple such sequences
- Return results ordered by
product_idascending
Example: ABC123XYZ ✅ (contains 123), Product56789 ❌ (contains 5 digits), A12B34C ❌ (digits separated by letters)
Input & Output
example_1.sql — Basic Pattern Match
$
Input:
Products table:
+-------------+--------------------+
| product_id | name |
+-------------+--------------------+
| 1 | ABC123XYZ |
| 4 | NoDigitsHere |
+-------------+--------------------+
›
Output:
+-------------+--------------------+
| product_id | name |
+-------------+--------------------+
| 1 | ABC123XYZ |
+-------------+--------------------+
💡 Note:
Product 1 contains exactly 3 consecutive digits '123' surrounded by letters. Product 4 has no digits at all.
example_2.sql — Edge Cases
$
Input:
Products table:
+-------------+--------------------+
| product_id | name |
+-------------+--------------------+
| 3 | Product56789 |
| 6 | Item003Description |
| 7 | Product12X34 |
+-------------+--------------------+
›
Output:
+-------------+--------------------+
| product_id | name |
+-------------+--------------------+
| 6 | Item003Description |
+-------------+--------------------+
💡 Note:
Product 3 has 5 consecutive digits (too many). Product 6 has exactly 3 digits '003'. Product 7 has digits separated by 'X'.
example_3.sql — Boundary Cases
$
Input:
Products table:
+-------------+--------------------+
| product_id | name |
+-------------+--------------------+
| 8 | 789Product |
| 9 | Product123 |
| 10 | 123 |
+-------------+--------------------+
›
Output:
+-------------+--------------------+
| product_id | name |
+-------------+--------------------+
| 8 | 789Product |
| 9 | Product123 |
| 10 | 123 |
+-------------+--------------------+
💡 Note:
All three products have exactly 3 consecutive digits at different positions: start, end, and entire string.
Constraints
- 1 ≤ product_id ≤ 104
- 1 ≤ name.length ≤ 100
- Product names contain only alphanumeric characters
- Each product_id is unique
Visualization
Tap to expand
Understanding the Visualization
1
Scan Product Name
Examine each character in the product name sequentially
2
Detect Digit Patterns
Identify sequences of consecutive digits
3
Validate Length
Ensure exactly 3 digits, not more or fewer
4
Check Boundaries
Verify digits aren't part of longer sequence
Key Takeaway
🎯 Key Insight: Regular expressions with boundary assertions provide the most efficient way to match exact patterns while avoiding false positives from longer sequences.
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code