SQL Beginner Challenge 16: Single-Character Matching with _ (Underscore Wildcard)

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_nametypedescription
product_idINTEGERUnique product ID
nameTEXTProduct name
skuTEXTProduct SKU
priceDECIMALProduct price

Sample Data

product_idnameskuprice
201USB-C HubUSB1-A34.50
202USB-C Hub ProUSB2-A49.99
203USB Flash DriveUSB10-A19.99
204Wireless MouseMOU1-B24.99
205Mechanical KeyboardKEY1-B89.00

Your Task

Write a SQL query that returns:

  • name
  • sku

…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

namesku
USB-C HubUSB1-A
USB-C Hub ProUSB2-A

Constraints

  • Use the LIKE operator
  • 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 ✅ matches USB1-A
  • USB_-A ❌ does not match USB10-A

SELECT
name,
sku
FROM products
WHERE sku LIKE 'USB_-A';

Explanation

  • LIKE 'USB_-A' matches SKUs where:
    • USB is fixed
    • _ represents exactly one character
    • -A is fixed
  • This gives you precise control over pattern length.

Common Mistakes

  1. Using % instead of _
    • % would match USB10-A, which we don’t want.
  2. Expecting _ to match multiple characters
  3. Forgetting that _ matches exactly one character

Optional Extension (Mini Bonus)

Try:

  1. Match MOU_-B SKUs
  2. Combine _ and % in one pattern
  3. 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.