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.

2 Answers

Steven Parker
Steven Parker
177,847 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 < "O".

Stephen Cole
PRO
Stephen Cole
Pro Student 9,892 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.)