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

longest length

I'm asked to return the title, and length as an alias with the highest one being returned.

I'm not seeing what the issue is?

2 Answers

Steven Parker
Steven Parker
167,746 Points

Do you mean you don't understand the instructions? Let's break them down:

"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."

So you'll need to:

  • select the title, and the length of the title
  • give the length an alias as the name
  • order by the length of the title
  • order with descending values (longest titles first)
  • limit the results to just the first one

I'll bet you can get it now — give it a try.

Steven,

Yes I see what I did...

select title, LENGTH(title) as longest_length FROM books ORDER BY length desc limit 1; --This failed

select title, LENGTH(title) as longest_length FROM books ORDER BY longest_length desc limit 1; --This PASSED

I didn't order by by Alias longest_length appropriately.

Thanks!

Steven Parker
Steven Parker
167,746 Points

Glad to help. In future questions, be sure to include your query code with the question.