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

Development Tools Database Foundations SQL Calculating, Aggregating and Other Functions Grouping, Joining and Cleaning Up

HAVING on an ALIAS

HAVING AVG(score) < 2;

has a different result than

HAVING average < 2;

This makes little sense to me as average is merely an alias of AVG(score). Can someone explain?

1 Answer

Hi Mikael,

Can you post your code and results? I just tried with the treehouse_movie_db from the course and the following query:

SELECT title, avg(score) AS average
FROM movies LEFT OUTER JOIN reviews
ON movies.id = reviews.movie_id
GROUP BY movie_id HAVING **condition**;

where condition was replaced with:

average < 2; returning Starman with avg 1.5

avg(score) < 2; returning Starman with avg 1.5

average > 3; returning Aliens (3.0732), Moulin Rouge (3.8333), Mama Mia (3.6154)

avg(score) > 3; returning Aliens (3.0732), Moulin Rouge (3.8333), Mama Mia (3.6154)

In other words, in my small trial, I got the same result whether I used the alias or the calculation. Hope to hear from you.