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

Help: MySQL HAVING clarification

I am working on the String function portion of the database deep dive and ran across something.

I am trying to call up a series of variables from the dataset and then filter out the usernames that are 19 or more characters in length.

The command only works if I put an alias in for username

SELECT first_name, UPPER(last_name), LOWER(email), LENGTH(username) AS username_length FROM users HAVING username_length < 19;

versus the following.

SELECT first_name, UPPER(last_name), LOWER(email), LENGTH(username) FROM users HAVING username < 19;

Any ideas why?

Thanks!!

4 Answers

It looks like it's not so much that it is a bad query, but more like they are trying to teach you the "proper" way to form a solid query. It's a better idea for readability and gives you more flexibility when running queries in your code.

This is a great explanation of it right here:

http://www.tutorialarena.com/mysql/mysql-as-keyword.php

Regards, homie!

Nicholas,

I actually ran the first command in MySQL Workbench and it did not return anything. It seems as though it's not correct in some way.

Unfortunately, I have not completed the database deep dive so I am not too familiar with what the query is pulling from or what the lesson is. Which lesson is it so I can just jump in and take a look?

It's in the SQL Calculating...section under the STRING function lesson. It occurs within the video not the challenge.