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

Mary Urban
Mary Urban
6,321 Points

Having problems with objective "Getting Length of String"

Even though I know how to get the correct answer as I checked the library database and I did get the longest title (Harry Potter, Order of Phoenix with 41 characters) but it is not in the exact format they are asking, and they don't let me pass even though it is correct.

To get the right result (but not what they are asking) I used:

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

When I use:

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

This doesn't work at all.

I don't know what I am missing here.

Thanks.

Jacob Herrington
Jacob Herrington
15,835 Points

Hey Mary, in the future, you can use the Get Help button on challenges to link the specific challenge so that we can check the solutions we are sharing with you against the challenge. Also, check out the Markdown Cheatsheet, so you can format your code!

Mary Urban
Mary Urban
6,321 Points

Yes, I thought I did click the Get Help button right on that objective...

6 Answers

Jacob Herrington
Jacob Herrington
15,835 Points

Mary, I can't be sure without testing with the challenge -- but SQL likes strings to be wrapped in single quotes, not double quotes. Try changing your query to:

SELECT title, LENGTH(title) AS 'Longest Length' FROM books ORDER BY longest_length DESC LIMIT 1;
Mary Urban
Mary Urban
6,321 Points

hmmm... the instructor uses double quotes in all the courses I have taken so far, and it's also used that way in the cheetsheets provided in the courses.. I did test the single quotes, but it doesn't work, unfortunately. I also tested this out in the SQL playground (my original that worked) and it was fine, but when I try to use the quotes for a two word title, it says SQL Error: no such column: longest_length

Jacob Herrington
Jacob Herrington
15,835 Points

Which specific challenge are you on? I'll see if I can get it to work.

Mary Urban
Mary Urban
6,321 Points

Hi, its the course "Reporting with SQL" and it's the objective "Getting the Length of a String" right after the video Getting the Length of Text.

Jacob Herrington
Jacob Herrington
15,835 Points

Hey again Mary!!

I was able to solve the challenge.

Here is my query:

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

Here is your query:

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

It would appear that the double/single quotes don't matter for whatever RDMS this course is using (I think MySQL), but there is another REALLY little syntax error. It's really not even a syntax error. Do you see the difference?

It was simply that the directions asked you to alias the length of the title as "longest_length" where you used "Longest Length" -- very small, and while your solution does technically work fine, it just didn't pass the challenge. This may seem stupid, but it a real world situation, if your database had a specific schema/naming system you needed to follow this could end up being an important distinction.

Mary Urban
Mary Urban
6,321 Points

I see that you didn't capitalize "longest length" in either place in the query? It did work though now, but I thought the idea of putting in an alias was to make it more presentable in a report? If I am remembering back to the SQL basics, they used capitals....

When I tried capitals in the playground just now like this:

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

It worked there but not in the objective in the course.

Jacob Herrington
Jacob Herrington
15,835 Points

You are correct, if I were writing an accounting report for non-techies I would probably do this:

SELECT 
  invoice_number AS 'Invoice Number'
  , invoice_qty AS 'Invoice Quantity'
  , invoice_ttl AS 'Invoice Total'
  , date as 'Date'
FROM invoices
WHERE 1=1;

That way they would end up less intimidated by the ugly spelling in the SQL database (which I personally hate by the way). The only reason I would say you might want to have an "ugly" alias is if you are following a specific casing or naming convention (like snake_case or camelCase).

In this challenge, I think Andrew Chalkley was just using the snake_case casing convention.

Alias exists for the convenience of the database users -- and so it largely comes down to personal preference.

Mary Urban
Mary Urban
6,321 Points

Thanks for your help! Hopefully my code is coming through more readable when I use the > symbol? It looks like it's at least bolded. Or do I use backticks? I looked at the Markdown Cheetsheet and I don't see any particular one for SQL. Why do yours show up in black? Sorry, its the first time I have used this.

Jacob Herrington
Jacob Herrington
15,835 Points

No problem!

Check out the Markdown Cheatsheet if you use three backticks it will let you use syntax highlighting for your code.

-- ```SQL
-- Code goes here
-- ```

SELECT stuff FROM place WHERE things;
#```Python
# Code goes here
#```

def Hello():
  print("Hello!")