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

Im not sure why my code isn't working for me.

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.

Steven Parker
Steven Parker
231,184 Points

It looks like you just quoted the instructions (which we can get with the "View Challenge" button). You forgot to show us your code.

4 Answers

sorry, heres my code that i tryed.

SELECT title LENGTH title AS longest_length FROM books ORDER BY length <;

Steven Parker
Steven Parker
231,184 Points

There's a few issues:

  • when using a function like LENGTH, the argument must be in parentheses (like "LENGTH(title)")
  • the argument in parentheses must also be supplied when using the length to sort on
  • to reverse the default sort order, use the keyword "DESC" (instead of the "<" symbol)
  • the instructions say "Only retrieve the longest book.", so you'll need to "limit" the number of results
Steven Parker
Steven Parker
231,184 Points

You're very close now!
But no quotes around the length function, and use the entire function (including argument) again for ORDER BY.

Steven Parker
Steven Parker
231,184 Points

You were closer last time. Starting with your previous code, just remove the quotes and where you have "length" by itself, replace that with "LENGTH(title)". This challenge doesn't need a WHERE clause at all.

this is the code i am trying now and its still not working. i cant seem to figure out were im going wrong.

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

Im still haveing a brain fart i swear i still cant figure this code out. Here is the new one i tried.

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