150

I have a simple query:

SELECT u_name AS user_name FROM users WHERE user_name = "john";

I get Unknown Column 'user_name' in where clause. Can I not refer to 'user_name' in other parts of the statement even after select 'u_name as user_name'?

0

16 Answers 16

114

SQL is evaluated backwards, from right to left. So the where clause is parsed and evaluate prior to the select clause. Because of this the aliasing of u_name to user_name has not yet occurred.

4
  • 41
    Rather than "backwards" I think it makes more sense to say "inside out" May 26, 2010 at 3:35
  • 6
    It makes more sense to say that the entire statement is parsed, transformed, and optimised as a whole in a complex, multistage process. "SQL is evaluated backwards, from right to left" is just wrong Sep 28, 2016 at 19:54
  • 4
    incomplete answer as the user asked if 'user_name' can be used in the statement, which it can be after e.g. 'HAVING' Sep 18, 2018 at 20:02
  • 1
    It makes more sense, ACKSHULLAY, to explain in detail exactly how SQL works and to never ever summarize anything for the sake of brevity, clarity, or understanding. Clearly. Sep 6, 2022 at 19:16
65

What about:

SELECT u_name AS user_name FROM users HAVING user_name = "john";
2
  • 17
    Why would you use HAVING instead of WHERE in this case?
    – PeteGO
    Apr 28, 2014 at 17:54
  • 8
    @PeteGO refer to Paul Dixon's answer. tldr; HAVING is evaluated later than WHERE and, more importantly, SELECT.
    – jairbow
    Mar 19, 2017 at 18:16
41

See the following MySQL manual page: http://dev.mysql.com/doc/refman/5.0/en/select.html

"A select_expr can be given an alias using AS alias_name. The alias is used as the expression's column name and can be used in GROUP BY, ORDER BY, or HAVING clauses."

(...)

It is not permissible to refer to a column alias in a WHERE clause, because the column value might not yet be determined when the WHERE clause is executed. See Section B.5.4.4, “Problems with Column Aliases”.

1
  • Also, note (from the MySQL man): The SQL standard requires that HAVING must reference only columns in the GROUP BY clause or columns used in aggregate functions. However, MySQL supports an extension to this behavior, and permits HAVING to refer to columns in the SELECT list and columns in outer subqueries as well. Sep 22, 2017 at 10:47
13
select u_name as user_name from users where u_name = "john";

Think of it like this, your where clause evaluates first, to determine which rows (or joined rows) need to be returned. Once the where clause is executed, the select clause runs for it.

To put it a better way, imagine this:

select distinct(u_name) as user_name from users where u_name = "john";

You can't reference the first half without the second. Where always gets evaluated first, then the select clause.

11

If you're trying to perform a query like the following (find all the nodes with at least one attachment) where you've used a SELECT statement to create a new field which doesn't actually exist in the database, and try to use the alias for that result you'll run into the same problem:

SELECT nodes.*, (SELECT (COUNT(*) FROM attachments 
WHERE attachments.nodeid = nodes.id) AS attachmentcount 
FROM nodes
WHERE attachmentcount > 0;

You'll get an error "Unknown column 'attachmentcount' in WHERE clause".

Solution is actually fairly simple - just replace the alias with the statement which produces the alias, eg:

SELECT nodes.*, (SELECT (COUNT(*) FROM attachments 
WHERE attachments.nodeid = nodes.id) AS attachmentcount 
FROM nodes 
WHERE (SELECT (COUNT(*) FROM attachments WHERE attachments.nodeid = nodes.id) > 0;

You'll still get the alias returned, but now SQL shouldn't bork at the unknown alias.

5
  • 1
    I was facing this exact problem, and came across your answer - thank you! Just to note, it is (understandably) a little slow on large databases, but I'm dealing with a stupid inherited database setup anyway. Sep 8, 2011 at 10:55
  • I believe you have an extra ( in your query before the (COUNT(*) which isn't closed anywhere.
    – tftd
    Nov 27, 2012 at 16:09
  • 4
    But isn't the SELECT statement run twice?
    – Matej
    Feb 25, 2015 at 23:21
  • I'm by far no mysql expert but this seems very inperformat. I've experienced that nested selects make a query much slower.
    – GDY
    Aug 16, 2019 at 12:53
  • Maybe refactor the subquery as a CTE, it feels cleaner for me.
    – jdhao
    Sep 20, 2022 at 9:36
9

Your defined alias are not welcomed by the WHERE clause you have to use the HAVING clause for this

SELECT u_name AS user_name FROM users HAVING user_name = "john";

OR you can directly use the original column name with the WHERE

SELECT u_name AS user_name FROM users WHERE u_name = "john";

Same as you have the result in user defined alias as a result of subquery or any calculation it will be accessed by the HAVING clause not by the WHERE

SELECT u_name AS user_name ,
(SELECT last_name FROM users2 WHERE id=users.id) as user_last_name
FROM users  WHERE u_name = "john" HAVING user_last_name ='smith'
7

Either:

SELECT u_name AS user_name
FROM   users
WHERE  u_name = "john";

or:

SELECT user_name
from
(
SELECT u_name AS user_name
FROM   users
)
WHERE  u_name = "john";

The latter ought to be the same as the former if the RDBMS supports predicate pushing into the in-line view.

6

corrected:

SELECT u_name AS user_name FROM users WHERE u_name = 'john';
5

No you need to select it with correct name. If you gave the table you select from an alias you can use that though.

3

No you cannot. user_name is doesn't exist until return time.

3
SELECT user_name
FROM
(
SELECT name AS user_name
FROM   users
) AS test
WHERE  user_name = "john"
1
  • Why would you want to use a sub-query? Much simpler without.
    – PeteGO
    Apr 28, 2014 at 18:00
1

Unknown column in WHERE clause caused by lines 1 and 2 and resolved by line 3:

  1. $sql = "SELECT * FROM users WHERE username =".$userName;
  2. $sql = "SELECT * FROM users WHERE username =".$userName."";
  3. $sql = "SELECT * FROM users WHERE username ='".$userName."'";
1
  • What have you changed and why? Additionally, why did you post code that is widely open for SQL injections?
    – Nico Haase
    Nov 14, 2019 at 13:14
1

May be it helps.

You can

SET @somevar := '';
SELECT @somevar AS user_name FROM users WHERE (@somevar := `u_name`) = "john";

It works.

BUT MAKE SURE WHAT YOU DO!

  • Indexes are NOT USED here
  • There will be scanned FULL TABLE - you hasn't specified the LIMIT 1 part
  • So, - THIS QUERY WILL BE SLLLOOOOOOW on huge tables.

But, may be it helps in some cases

0

While you can alias your tables within your query (i.e., "SELECT u.username FROM users u;"), you have to use the actual names of the columns you're referencing. AS only impacts how the fields are returned.

1
  • I think you can use the alias in some RDBMS like MySql for instance. You're correct for Sql Server though.
    – PeteGO
    Apr 28, 2014 at 17:58
0

Just had this problem.

Make sure there is no space in the name of the entity in the database.

e.g. ' user_name' instead of 'user_name'

0
-4

I had the same problem, I found this useful.

mysql_query("SELECT * FROM `users` WHERE `user_name`='$user'");

remember to put $user in ' ' single quotes.

1
  • 4
    That code is widely open for SQL injection. Nobody should use this code to solve the given problem
    – Nico Haase
    Nov 14, 2019 at 13:13