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_id ascending

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
Product Name Pattern Scanner✓ Valid: ABC123XYZABC123XYZExactly 3 digits ✓✗ Invalid: Product56789Product567895 digits - too long! ✗Pattern: (^|[^0-9])[0-9]{3}([^0-9]|$)🎯 Breakdown:• (^|[^0-9]) → Start of string OR non-digit character• [0-9]{3} → Exactly 3 consecutive digits• ([^0-9]|$) → Non-digit character OR end of string✓ Ensures digits are not part of longer sequence
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.
Asked in
Amazon 45 Microsoft 38 Google 32 Meta 25
28.4K Views
Medium Frequency
~15 min Avg. Time
850 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