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

Seth Johnson
Seth Johnson
15,199 Points

Question about use of aliases in query statements...

I'm hoping someone can help clear this up as it hasn't been explained in the videos thus far, and I feel like it's really something that should have been majorly emphasized.

Once an alias has been written in a query statement, can or cannot it be used as part of a conditional such as WHERE or ORDER BY in that same query?

An example of something like this would be:

SELECT title || LENGTH(title) AS 'longest_length' FROM books ORDER BY longest_length desc limit 1;

If this is true and accurate, I would have thought it would've been very explicitly pointed out as a thing; the implications are that I was supposed to have somehow figured that out on my own, which tbh, doesn't seem particularly well-tailored to a course labeled as being for "beginners." But, maybe that's just me. Any and all help and/or insight would be greatly appreciated, thanks!

2 Answers

In programming you will always be learning more than you have been presented with by researching (asking questions, books, Internet, self testing - the number of times I've read documentation only to say "That's it?") but yes you can use aliases in clauses beyond selecting columns. You can alias tables as well and use those aliases in complex joins. That is part of their functionality in making queries easier to read and understand. I must say the alias in your example doesn't make much sense descriptively in that you combined a field and its length and gave it an alias of longest_length.

Seth Johnson
Seth Johnson
15,199 Points

Thanks Kris, I eventually figured it out (sort of), I was just incredibly frustrated at the time of writing my inquiry. I appreciate your response!