Databases Reporting with SQL Working with Text Getting the Length of a String

Carol Garcia
Carol Garcia
1,875 Points

What am I doing wrong in this query?

Below is my code:

SELECT title, LENGTH(title) AS longest_length FROM books ORDER BY title, length DESC LIMIT 1;

2 Answers

Jennifer Nordell
STAFF
Jennifer Nordell
Treehouse Staff

Hi, Carol Garcia ! It looks like you're doing terrific thus far! The problem is in your ORDER BY. You just made an alias for the length of the title together with the title and named it longest_length. This is what you should be ordering by :smiley:

Try changing this part:

ORDER BY title, length

To this:

ORDER BY longest_length

Hope this helps! :sparkles:

Carol Garcia
Carol Garcia
1,875 Points

Wow thank you, Jennifer, that worked! I'm still not sure how though so allow me to work it out if you'll confirm. Below was the original example given in the video which I tried to follow but it did not work. SELECT username, LENGTH(username) AS length FROM customers ORDER BY length DESC LIMIT 1; "length" is the alias for username and length of username right?

In my answer "longest_length" is the alias for title and LENGTH(title), right (below in corrected format)? SELECT title, LENGTH(title) AS longest_length FROM books ORDER BY title, length DESC LIMIT 1;

Tommy Gebru
Tommy Gebru
28,819 Points

I believe "longest_length" is only an alias for one parameter, consider also that your alias will replace the column description, so you can count out how many columns are displayed as well :thumbsup: