Getting Started with Adobe After Effects - Part 6: Motion Blur


Upload Image Close it
Select File

Browse by Tags · View All
sql_server 217
t-sql 211
tsql 116
sqlserver 96
BRH 78
#SQLServer 66
#TSQL 56
SQL Server 34
function 11
SSMS 9

Archive · View All
August 2007 17
August 2010 8
June 2012 7
June 2011 7
November 2007 7
August 2012 6
May 2012 6
November 2011 6
August 2011 6
October 2011 6

Madhivanan's TSQL Blog

Question of the month May 2012 - How does direct usage of column alias work in ORDER BY clause?

May 9 2012 12:00AM by Madhivanan   

Consider the following two statements

--Query 1

select 
	name, 
	dateadd(day,datediff(day,0,create_date),0) as date_only 
from 
	sys.objects
where 
	date_only>getdate()-100

--Query 2

select 
	name, 
	dateadd(day,datediff(day,0,create_date),0) as date_only 
from 
	sys.objects
order by 
	date_only

While the first query throws an error Invalid column name 'date_only'. ,How does the second query work?

Tags: 


Madhivanan
3 · 39% · 12440
11
 
0
Lifesaver
 
 
0
Learned
 
0
Incorrect



Submit

6  Comments  

  • It is basically due to the sequence in which various clauses are evaluated/processed. In this specific case, WHERE is evaluated/processed before SELECT (hence the alias is not available at the time of evaluation/processing of WHERE clause) and ORDER BY is evaluated/processed after the SELECT (hence the alias is available at the time of evaluation/processing of ORDER BY).

    commented on May 9 2012 12:13AM
    Dattatrey Sindol (Datta)
    43 · 4% · 1333
  • Logical query processing evaluates FROM -> ON -> WHERE -> SELECT -> ORDER BY.

    In query 1, database query engine wants Actual column name to process the where condition and it is not present, that's why it is failed. In query 2, query engine can use column alias to get the required information, hence it is working fine.

    commented on May 9 2012 6:33AM
    Hardik Doshi
    20 · 9% · 2839
  • It's all about the way in which the clauses are evaluated. ORDER BY is evaluated after SELECT and hence, the database engine knows what the "date_only" alias means. WHERE is evaluated before SELECT, and therefore, column aliases do not work with WHERE clauses.

    commented on May 11 2012 1:56PM
    Nakul Vachhrajani
    4 · 33% · 10587
  • This is simple concept of query execution architechture.From the 11 layers of the Arch. the WHERE clause comes before the SELECT executes and ORDER BY clause executes after the SELECT.that's why "date_only" column identified by ORDER BY clasue but not by WHERE clause.

    commented on May 16 2012 7:37AM
    Shivendra Kumar Yadav
    64 · 3% · 892
  • There is an interesting discussion on the very same topic in sql server quiz 2010. Slightly diff. Variation was asked by itzik as question 2.

    commented on May 30 2012 10:50AM
    Ramireddy
    2 · 41% · 12972
  • In Logical query processing, 'Where' Clause is processed before 'Select' Clause, so while processing 'Where' Clause, it can not identify alias 'date_only' and will throw error. But 'Order By' is processed after 'Select' Clause, so it can identify by alias name and will not throw an error.

    commented on Jun 1 2012 7:27AM
    Viral
    280 · 0% · 151

Your Comment


Sign Up or Login to post a comment.

"Question of the month May 2012 - How does direct usage of column alias work in ORDER BY clause?" rated 5 out of 5 by 11 readers
Question of the month May 2012 - How does direct usage of column alias work in ORDER BY clause? , 5.0 out of 5 based on 11 ratings
    Copyright © Rivera Informatic Private Ltd Contact us      Privacy Policy      Terms of use      Report Abuse      Advertising      [ZULU1097]