tags:

views:

59

answers:

2

I have a query:

SELECT exhibitor_participation+0 AS exhibitor_participation_value 
FROM `exhibitor_registry` 
WHERE ((exhibitor_participation_value & 1) = 1)

Question:

Why does it return this:?

Unknown column 'exhibitor_participation_value' in 'where clause'

Isn't it supposed to be successful since I defined exhibitor_participation_value in the SELECT clause?

+3  A: 

It might depend on your database but generally no. Column aliases defined in the select clause are not available in the where clause.

Jason Punyon
That is, if you give a column an alias in the select (columns) clause, you cannot use or reference that alias in the where clause.
Philip Kelley
@Philip Kelley: Thanks. I clarified.
Jason Punyon
+1  A: 

The where clause is operating on the raw columns of the dataset. The select clause at the same level operates on the same thing, and can optionally give aliases for outer layers of code. But critically, the aliases "wrap" the current level and aren't available there - specifically, you can't use the aliases in the where clause.

You could have done something like this:

SELECT *
FROM
    (SELECT exhibitor_participation+0 AS exhibitor_participation_value
     FROM `exhibitor_registry`)
WHERE ((exhibitor_participation_value & 1) = 1)

and the alias can be used in the where clause, since it's now the name of one of the columns being selected at that level. Pushing the select down into an inner view may limit the ability of the database to come up with an appropriate query plan (which as usual will depend on many factors and vary between each individual query). On the other hand, if the where condition takes a non-negligible amount of time to calculate, you may actually save time by calculating it once in the inner view rather than potentially twice (again, the optimiser may reuse the first value here - it depends).

As with all performance-related issues, profile to see what is actually needed in your specific situation.

Andrzej Doyle
What if you were calculating something with a long formula that then needed to be checked in the where clause? Wouldn't keeping things DRY make the SQL easier to maintain?
Jason Punyon
On which rdbms does this kind of wrapping cause an afficiency hit?
Jens Schauder
It might depend on the flavour of DBMS but I would like to see a benchmark to indicate that there is any performance impact from using inline views.
APC
Also, supplementing Jason's observation, if we have an alias for a formula which we want to test in the WHERE clause, executing it once (in the projection) is a jolly fine idea.
APC
@APC - probably not for this example, but depending on the contents of the inline view selectors, and the intelligence of the query optimizer, it can lead to doing a lot of extra work that's simply unnecessary. I've seen that happen on SQL Server myself, where driving a where clause down into inline views vastly decreased execution time.
Andrzej Doyle
@Jason, APC - that much is true. I'll edit the answer to make a more balanced conclusions.
Andrzej Doyle
@APC: I was going to say something to that effect, but I didn't want to make a statement about performance I couldn't back up :)
Jason Punyon
On the other hand Jason, with an inline view you either need to repeat the column selectors in the outer select, or just go with a `select *`, and neither one is great. Bleh for SQL. :-)
Andrzej Doyle