Brightlane's product catalog team is reviewing items whose name carries a numeric component — a model number, size, or generation identifier.
Write a query to return the ID and name of every product whose name contains at least one digit.
Assumptions:
- The
productstable has one row per product with anidand aname. - A qualifying product has a
namecontaining one or more characters in the range0through9.
Output:
- One row per qualifying product, with columns
idandname.
Schema · ecommerce 5 tables
Run previews · Check grades
Write a query, then run it to see results here.
Worked solution Try it yourself first
SELECT
id,
name
FROM
products
WHERE
name ~ '[0-9]' The shape
name ~ '[0-9]' uses a character class to mean "any digit," and the unanchored ~ operator looks for that class anywhere in the string. One pattern covers Zephyr 12, Sofa 3-Seater, and Picture Frame 8x10 without enumerating each digit separately.
Clause by clause
SELECT id, namereturns the product ID and name for the catalog review.FROM productsreads every product.WHERE name ~ '[0-9]'keeps rows whose name contains at least one character in the range0through9. The square brackets define a character class;[0-9]is shorthand for "any one of these ten characters." Because the regex is not anchored, a single matching digit anywhere in the name is enough.
Why this and not LIKE '%0%' OR LIKE '%1%' OR ... OR LIKE '%9%'
The LIKE form is correct but unmaintainable. It expands "contains a digit" into ten separate substring predicates joined by OR. A character class collapses the same intent into three characters of pattern. Whenever the question is "contains any character from this set," reach for a regex with a character class rather than writing out the set as alternatives.
The trap
[0-9] matches a single digit. [0-9]+ matches one or more consecutive digits. For "contains at least one digit," both produce the same boolean answer, because if any digit exists, both expressions match somewhere in the string. The + quantifier matters when you start extracting the digits or anchoring the pattern; for a pure existence check, the bare class is enough.
You practiced ~ '[0-9]' — character-class regex matching, the right shape for 'contains any digit' without enumerating each one.