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 Reporting with SQL Working with Text Getting the Length of a String

David Corrales
David Corrales
4,698 Points

Some small minor error perhaps reporting with SQL, can someone help?

Challenge Task 1 of 1 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.

Type in your command below, then press Ctrl-Enter.

Bummer: Your query didn't retrieve the longest book title with it's length.

SELECT title, LENGTH(title) AS longest_length FROM books LIMIT 1;

3 Answers

Steven Parker
Steven Parker
231,236 Points

You're close, and the "LIMIT" does the job of returning just the first result; but it's not necessarily the longest one.

Hint: You might also want to use "ORDER BY".

David Corrales
David Corrales
4,698 Points

Oh my gosh you're the best! I did it! yayy! Thanks to your help!

In this same problem: 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.

I have written this piece of code: SELECT title, LENGTH(title) AS "longest_length" FROM books ORDER BY DESC LIMIT 1; But it shows the error: Bummer: There's something wrong with your SQL statement. Please review your code and try again. I am not sure, what I am doing wrong?

Steven Parker
Steven Parker
231,236 Points

Asking a question as an "answer" to another question! :see_no_evil:

Starting a fresh question will give you the best chance of a prompt answer.