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

Alex Forseth
Alex Forseth
8,017 Points

Getting the Length of String in SQL. Truly STUMPED by challenge.

In Database's "Reporting with SQL course" I am on the "Getting the Length of a String Challenge".

The challenge is as follows -

In the library database there's a books table with the columns id, title, author, genre and first_published.

Find the book with the longest title. Show the title and then the length. Alias the result of the length calculation to be longest_length. Only retrieve the longest book.

MY SOLUTION: ``

SELECT title, LENGTH(title) AS longest_length FROM books ORDER BY title DESC LIMIT 1; `` When I submit this code from my on interpretation the table is displaying the solution to the problem. Please help.

1 Answer

Ashley Carpenter
Ashley Carpenter
13,393 Points

try

SELECT title, LENGTH(title) AS longest_length FROM books ORDER BY longest_length DESC LIMIT 1;
Ashley Carpenter
Ashley Carpenter
13,393 Points

Sorry, should have explained why. It's because you're ordering alphabetically on the title and not numerically on the length of the title.

Seth Johnson
Seth Johnson
15,200 Points

Thanks for this clarification ashley, it sort of makes sense to me, but I have two big questions: 1) Why wouldn't have "title" and "LENGTH(title)" been concatenated together using the "||" operator? and 2) How is it that your alias isn't in quotes, AND you're able to use it as part of the "ORDER BY" conditional in the same query? I would have thought it would've been very explicitly pointed out as a thing in the videos thus far; the implications are that I was supposed to have somehow figured that out on my own, which tbh, doesn't seem particularly well-tailored to a course labeled as being for "beginners." Buuut, perhaps that's just me.