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

David Corrales
David Corrales
4,646 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
172,033 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,646 Points

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

Shantanu Trivedi
Shantanu Trivedi
1,337 Points

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
172,033 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.