Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

General Discussion

Ryan Aves
Ryan Aves
2,203 Points

Not recognizing SQL wildcard characters

This question asks for a Union between the two tables where name begins with A-K.

I keep entering this statement which I believed to be correct, yet the answer still shows as incorrect.

SELECT Name from Fruit WHERE Name LIKE '[a-k]%' UNION SELECT Name from Vegetable WHERE Name LIKE '[a-k%]';

Is something wrong with my LIKE keyword?

2 Answers

Stuart Wright
Stuart Wright
41,120 Points

I’m not actually sure which, if either, of these is correct, but just wanted to point out that you’ve used two different variants on the syntax in your query, and I guess they can’t both be right:

[a-k]% [a-k%]

The first one looks more sensible to me, but I don’t know for sure that it’s correct.

Ryan Aves
Ryan Aves
2,203 Points

Thank you for the catch, Stuart.

I re-tried with [a-k]% in both 'Select' queries (and then [a-k%] for good measure), but both drew errors.

Stuart Wright
Stuart Wright
41,120 Points

I just created a small database with one table on my local machine, with two rows ('apple' and 'orange'). I attempted:

SELECT * FROM Fruit WHERE Name LIKE '[a-k]%';

And got no results back. So I don't think the above pattern works in SQLite, and the challenge is correct to not let your code pass. It does work in some other SQL variants (minor variations exist among the various different SQL variants).

There are other ways to complete the challenge. For example:

WHERE Name >= 'A' and Name < 'L'
Ryan Aves
Ryan Aves
2,203 Points

Stuart, that worked! Great catch, I imagine you're right that the pattern wasn't compatible with SQLite. Thanks again.