Databases Querying Relational Databases Set Operations Set Operations

Michael Nanni
Michael Nanni
7,355 Points

How do I filter query results between a range of starting letters?

SELECT Fruit.Name FROM Fruit
SELECT Vegetable.Name FROM Vegetable
WHERE Name <= 'K%';

I know there's something I'm missing about this challenge, but why isn't the above acceptable?

I've tried with the WHERE clause after both SELECT statements but that is incorrect as well. I'm not sure which operator works best for a range of starting letters. BETWEEN? LIKE?

Thanks in advance for any help.

1 Answer

alastair cooper
alastair cooper
28,181 Points
select employee_name
from employees
where employee_name LIKE 'A%' OR employee_name LIKE 'B%'

That is a quick way if your range is 2 or 3 letters

if you have a large range (ie A-M) then there is probably a better way

something like this...

FROM Employee
WHERE Name LIKE '[A-E]%';

The exact syntax will depend on version of SQL (mySql, Sql Server, etc)

Hope this helps

alastair cooper
alastair cooper
28,181 Points

oops, sorry, I didn't realise it was for a challenge question! However, the principle should hold, just replace the variables