Databases Querying Relational Databases Set Operations Set Operations

Why is my question being marked wrong when it is right?

The original question was: There are two tables Fruit and Vegetable table. The Fruit table has a FruitID and a Name column and the Vegetable table has a VegetableID and Name column.

Create a list of all fruits and vegetables starting with the letters A through K . In other words all fruit and vegetables that don't start with the letter L to Z.

My response was: Select Name from Fruit Where Name LIKE '[A-K]%' UNION Select Name From Vegetable where Name LIKE '[A-K]%'

I verified the results by bringing it into MS SQL Server

2 Answers

Steven Parker
Steven Parker
186,663 Points

The character class syntax ("[A-K]") you are using in SQL Server is not a feature of SQLite. So you won't be able to use it (or the LIKE operator) for the challenge.

But you can take advantage of the fact that inequality comparisons on strings test dictionary order.

Hey Steven,

I tried using inequality operators like you suggested but I am still getting the same error. I tested my query in MS SQL Server and verified that my syntax is correct and am returning similar results.

Select Name from Fruit Where Name <= 'K%' and Name >= 'A%' UNION
Select Name From Vegetable where Name <= 'K%' and Name >= 'A%'

Steven Parker
Steven Parker
186,663 Points

Now that one I would not expect to work in SQL Server either, since the wildcard ("%") is only useful in a LIKE.

But Name < 'L' would cover anything that begins with A through K.

Ahhh that worked! Thank you.