Difficulty: Beginner
Estimated time: 10–15 minutes
SQL concepts: LIKE, _ (single-character wildcard), pattern precision
Goal: Match text patterns where exact character positions matter.
The Scenario
A product manager asks:
“Can we find product codes that follow a specific pattern—where one character varies, but the rest is fixed?”
You already know % matches any number of characters.
Now you’ll use _ to match exactly one character.
Database Table
products
| column_name | type | description |
|---|---|---|
| product_id | INTEGER | Unique product ID |
| name | TEXT | Product name |
| sku | TEXT | Product SKU |
| price | DECIMAL | Product price |
Sample Data
| product_id | name | sku | price |
|---|---|---|---|
| 201 | USB-C Hub | USB1-A | 34.50 |
| 202 | USB-C Hub Pro | USB2-A | 49.99 |
| 203 | USB Flash Drive | USB10-A | 19.99 |
| 204 | Wireless Mouse | MOU1-B | 24.99 |
| 205 | Mechanical Keyboard | KEY1-B | 89.00 |
Your Task
Write a SQL query that returns:
namesku
…but only for products whose sku matches this pattern:
USB_-A
👉 This means:
- Starts with
USB - Followed by exactly one character
- Then
-A
Expected Output
| name | sku |
|---|---|
| USB-C Hub | USB1-A |
| USB-C Hub Pro | USB2-A |
Constraints
- Use the
LIKEoperator - Use the
_wildcard - Do not use
%for this challenge - Do not use
SELECT *
Hint (Optional)
_matches exactly one character%matches any number of characters
Examples:
USB_-A✅ matchesUSB1-AUSB_-A❌ does not matchUSB10-A
SELECT
name,
sku
FROM products
WHERE sku LIKE 'USB_-A';
Explanation
LIKE 'USB_-A'matches SKUs where:USBis fixed_represents exactly one character-Ais fixed
- This gives you precise control over pattern length.
Common Mistakes
- Using
%instead of_%would matchUSB10-A, which we don’t want.
- Expecting
_to match multiple characters - Forgetting that
_matches exactly one character
Optional Extension (Mini Bonus)
Try:
- Match
MOU_-BSKUs - Combine
_and%in one pattern - Find SKUs that start with
USB, have one digit, and end with-A
Next Challenge
Beginner Challenge #17: Combining Conditions with AND and OR
🔗 View reference solution on GitHub
(After you’ve tried the challenge)
Want more practical SQL challenges?
Subscribe to the Solve With SQL newsletter and get new problems delivered to your inbox.