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

How to write a query that will return all items that start with any letter between A and K?

In SQL, we are being asked:

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 a Name Column. Create a list of fruits and vegetables that start with any letter between A and K.

I had no problem with the union aspect, however I went back through our courses to try to see how I would get results from A - K and could not find anything. I tried:

SELECT Name FROM Fruit UNION SELECT Name FROM Vegetable WHERE Name < L;

And it returned all the fruits and vegetables.

Would someone please let me know how to cut it off after K?

1 Answer

Jeff Wilton
Jeff Wilton
16,646 Points

I think you are on the right track, just try adding a where clause in your first select statement and make sure 'L' is surrounded by quotes:

SELECT Name FROM Fruit 
WHERE Name < 'L'
UNION 
SELECT Name FROM Vegetable 
WHERE Name < 'L';

Thanks! Why for "WHERE" does it have to go after each select clause whereas something like "ORDER BY" only has to go at the very end of the query?

Jeff Wilton
Jeff Wilton
16,646 Points

You are performing two select statements and therefore need two where clauses for this example.