Databases Querying Relational Databases Set Operations Set Operations

Hans Eisenman
Hans Eisenman
1,315 Points

How do you build a Where clause that selects out all words which start with letters A-K?

I'm not sure how to do this and get a syntax error right from the get-go. I've tried a few approaches and this one seems the most logical (code should be attached) but at the same time there must be a better way. I don't recall getting this in earlier SQL lessons, but that doesn't mean it wasn't covered. Calling out for a life line. :)

Hans Eisenman
Hans Eisenman
1,315 Points

I thought Treehouse was going to attach my code but now I don't see it. Here's a snippet of the approach I was taking. Again, this seems like the long way to my destination. Must be a better way!

select name from vegetable 
union 
select name from fruit 
where not like 'L%' 
and not like 'M%' 

[etc. ...]

4 Answers

KRIS NIKOLAISEN
PRO
KRIS NIKOLAISEN
Pro Student 51,854 Points

You can use less than and greater than signs to filter letters alphabetically just like numbers. So

WHERE name < "L"

would be letters A-K

Hans Eisenman
Hans Eisenman
1,315 Points

Thanks Kris! I knew there had to be something more graceful than what I was attempting! :)

Hans Eisenman
Hans Eisenman
1,315 Points

Weird. I can make this WHERE clause work in SQL Serve Mgmt Studio, but I can't get it to work in my Treehouse lesson.

SELECT name FROM fruit
UNION
SELECT name FROM vegetable
WHERE name < 'L'

Error I get is:

Bummer: Your UNION queries didn't bring back a union of fruits and veggies beginning with the letters A through K!

As a side note: I wish it were possible to check one's work in pieces like you can in a regular SQL IDE (select parts of it and Execute it). That way you could use use the process of elimination to narrow down the exact issue.

KRIS NIKOLAISEN
PRO
KRIS NIKOLAISEN
Pro Student 51,854 Points

Your first select statement needs a WHERE clause

Hans Eisenman
Hans Eisenman
1,315 Points

Ah. Of course. Should have thought of that. Many thanks again! I finally got past this one.