Databases

Seth Johnson
Seth Johnson
13,545 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

KRIS NIKOLAISEN
PRO
KRIS NIKOLAISEN
Pro Student 50,936 Points

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
13,545 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!