6. Numbers II bugged?


#1


https://www.codecademy.com/courses/sql-table-transformation/lessons/date-number-string-functions/exercises/numbers-ii?action=lesson_resume


Hi all, this is the first time that I post for help on the forum, if there is anything inappropriate, please excuse me.

By

SELECT id, cook_time, cool_down_time
FROM baked_goods;

we'll get the query results as shown below:

But the results of

SELECT id, MAX(cook_time, cool_down_time)
FROM baked_goods;

shown as

which made me extremely confused, because I think the MAX and MIN functions are supposed to return the greatest and least value respectively.
In the row where id = 2, the 33 of cool_down_time is larger than the 5 of cook_time obviously, but why the result returns as 5? The same circumstance occur in the rows where id = 13 and 15.

Could anyone explain it for me, please?
Much appreciate!



#2

Oh I have figured it out by myself.

It is because of the data type of the columns, which is TEXT.
By MAX clause, the TEXT strings in the cook_time column and cool_down_time column will be compared by their first character. As a TEXT character, '5' is larger than '33', because '5' is larger than '3'. And by the way, E is larger than C due to its latter position in the alphabet.

But what if the data type is INT? Then the numbers will be compared mathematically, which means '33' will be returned in the row where id = 2.


#3

Thank you so much for pointing this out, as I had been confused by the results but had not been aware that the type was text. You would think they might want to point this out, as it is a very avluable lesson


#4

lol, you're welcome! I have never expected that someone would reply this post after such a long time.
Yes, indeed. I was astonished that no one had had mentioned it while I was searching for help on the forum. So I posted it and hoped it could help someone else.


#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.