A Brightlane purchasing team is evaluating the Crest laptop line ahead of a volume purchasing decision. Every product in the line has a name that begins with Crest (e.g., Crest 13, Crest 15 Pro).
Write a query to return the name and price of every product whose name begins with Crest.
Assumptions:
- The
productstable contains every product in Brightlane's catalogue. - Product names are stored exactly as written, with
Crestcapitalised.
Output:
- One row per qualifying product, with columns
nameandprice.
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
name,
price
FROM
products
WHERE
name LIKE 'Crest%' The shape
LIKE 'Crest%' matches any name beginning with Crest, regardless of what follows. The % wildcard absorbs everything after the prefix — one character, twenty characters, or zero.
Clause by clause
SELECT name, pricereturns the two columns the purchasing review needs: the product name and its price for the volume comparison.FROM productsreads the catalogue.WHERE name LIKE 'Crest%'keeps only the rows whosenamestarts with the literal textCrestfollowed by zero or more characters.Crest Pro 14"matches becauseCrestsits at the start and% Pro 14"covers the rest.Crest Airmatches the same way. A product namedCrestalone would also match — the%accepts zero characters too.
Why this and not name = 'Crest'
Equality matches the literal string and nothing else. name = 'Crest' would only return a product whose name is exactly the four letters Crest, which isn't what the purchasing team is asking for. They want the whole line — every product whose name starts with Crest and then continues with the model designation.
The % wildcard is what turns LIKE from a fancier = into a prefix matcher. The pattern 'Crest%' says "the literal characters Crest, then anything." Drop the % and the same rule applies as with = — only an exact match qualifies.
You practiced matching a string prefix with LIKE and the % wildcard. name LIKE 'Crest%' matches any name that starts with Crest followed by zero or more characters — the everyday shape of a "by family" or "by brand" filter.