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

Takyi Akwah
Takyi Akwah
Pro Student 1,049 Points

Help with Length function

Somebody please show me where I am going wrong.

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: There's something wrong with your SQL statement. Please review your code and try again.

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

2 Answers

You don't need to order by 'longest_length' just order by "Length".

SELECT title, LENGTH(title) as longest_length FROM books order by longest_length desc limit 1;

You can avoid the order by clause and limit by just keeping it simple:

select title, max(length(title)) as longest_length from books