Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 select top subqueries

Author  Topic 

iminore
Posting Yak Master

141 Posts

Posted - 2005-09-29 : 04:49:01
This is n odd request in that something works - but I can't remember why.
This statement:

select top 20 userid from (select top 20 userid from KATUsers order by userid)aref order by userid desc

What is the role of 'aref' ? And where would I find a reference to it in Books Online?

ditch
Master Smack Fu Yak Hacker

1466 Posts

Posted - 2005-09-29 : 04:52:25
aref is actually the name you are aliasing your subquery with ie the code between "()".
I bet this also works:
select top 20 userid from (select top 20 userid from KATUsers order by userid) xref order by userid desc

Duane.
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2005-09-29 : 05:00:52
It is the alias name of the subquery so that that subquery will be treated as a seperate table

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

iminore
Posting Yak Master

141 Posts

Posted - 2005-09-29 : 05:04:04
Agreed the alias can be called anything. I'm just not clear why SQL needs an alias (you get an error without it), but thanks for the help. BO has much about aliases and subqueries.
Go to Top of Page

ditch
Master Smack Fu Yak Hacker

1466 Posts

Posted - 2005-09-29 : 05:12:17
>> I'm just not clear why SQL needs an alias

Quite simple really - as madhivanan said - it is treated as a seperate table - so it therefore needs to have a name.
What if there are more than one subqueries in a sql statement?


Duane.
Go to Top of Page
   

- Advertisement -