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

Databases SQL Reporting by Example Day 2: Advanced Selecting Students A to M

Why doesn't ' WHERE LAST_NAME <= 'N' ' work?

I was trying out different methods answer the question asked in the video and tried the code below but the last result only goes to 'Murray'.

SELECT * FROM STUDENTS WHERE LAST_NAME <= 'N' ORDER BY LAST_NAME ASC

I was wondering why it doesn't include last names starting with N, even though it's an equal to or less than operator.

4 Answers

Steven Parker
Steven Parker
231,184 Points

Anything with other letters following "N" would be considered to come later in alphabetic sequence, so only the single letter "N" would meet this condition.

You'd be likely to get the behavior you want with something like <= "Nzzzzz", but an even safer test would be:

WHERE LAST_NAME < 'O'   -- include everything starting with 'A' through 'N'
Stephen Cole
PLUS
Stephen Cole
Courses Plus Student 15,809 Points

Did you mean:

SELECT LAST_NAME, FIRST_NAME FROM STUDENTS
WHERE LAST_NAME <= 'M'
ORDER BY LAST_NAME ASC;

This does solve the problem of printing a list of names A-M:

WHERE LAST_NAME <= 'N'

...as words (with multiple letters) that start with the letter M are before the single letter N.

However, if you use this...

WHERE LAST_NAME <= 'M'

...there is no way to account for words that start with the letter M. It's a single value. (There's no way to use a wildcard character.)

THIS IS HOW I DID. COMMENTS APPRECIATED.

-- Generate a list of students with last names from A to M
SELECT * FROM Students WHERE last_name BETWEEN 'A%' AND 'N%'
ORDER BY last_name
Beau Coplin
Beau Coplin
7,903 Points

You can also write it like this:

SELECT * FROM Students
WHERE last_name < "N"
ORDER BY last_name asc

You get the same result as above because you are filtering out everything "less than" N which gives you A-M