tags:

views:

33

answers:

2

Hi All,

Is

select id from (select * from employee ) as t

is same as

;with temp as (select * from employee) select id from temp

?????

Both will return same result.But regarding performance oriented.

A: 

CTE's are for recursive queries.

leppie
...among other uses e.g. save repeating a derived table in multiple subqueries.
onedaywhen
+1  A: 

Yes, in your example they are the same, you should view the execution plan to see any differences in how they work and hence performance. If you 'include execution plan' in SSMS and execute both queries in one batch, you will get a 'Query cost (relevant to the batch)' which will tell you which query performs better. I would guess that they are equivalent.

The advantage of a CTE (your second statement) over a derived table is that they can reference themselves and be used for recursion.

Chris Diver
I examined the Client Statistics for the both type of queries, i found the total execution times are same. How do i know the space occupied in Memory for the execution.Thanks for your response
Manoj
Look at the execution plan, hover over the arrows and the tool tip should give you more information.
Chris Diver