Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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

pinky
pinky
2,248 Points

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
217,577 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

pinky
pinky
2,248 Points

sorry, heres my code that i tryed.

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

Steven Parker
Steven Parker
217,577 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
217,577 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
217,577 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.

pinky
pinky
2,248 Points

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;

pinky
pinky
2,248 Points

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;