" a valid comparison with VARCHAR data in SQL? So, I'm completely new to SQL (and scripting / coding in general), and this is from one of ...">

Why is ">" a valid comparison with VARCHAR data in SQL?

So, I'm completely new to SQL (and scripting / coding in general), and this is from one of the examples in the book, but they unfortunately decided that there would be no questions about this query and neglected the extension to '>' at the end request

Here is the question:

SELECT * FROM easy_drinks WHERE main > 'soda'; 

Here is a pastebine from several queries, hoping to give a perspective: http://pastebin.com/xfJQsBvU

DESC insert easy_drinks: http://pastebin.com/LZZPhk6Z

I'm just confused about how the ">" at the end of the query works, since main is stored as VARCHAR, and "soda" is definitely not an integer than can be compared to another integer. However, as you can see in the first pastebin, the request succeeds. Why doesn't MySQL return an error, and what is the pattern behind the different queries using ">" and "<"?

+4
source share
2 answers

Perhaps this is a lexicographical ordering.

This is similar to how you or I will order words in a dictionary. However, note that it may not handle numbers as you expected .

+7
source

You will often find "bindings" to strings, mostly used in alphabetical order, but of course they are implementation dependent and can be different, especially for different languages ​​/ encodings or for the question of upper and lower case.

0
source

All Articles