smwikipedia
smwikipedia

Reputation: 64333

Why I cannot select from some selected results?

I am a bit new to TSQL programming. I am using MS SQL Server 2008 R2.

Why the following query doesn't work? I thought a select query just return a bunch of records and I should still be able to select from those records.

select * from
(
select * from dbo.[sometable]
)

Upvotes: 1

Views: 61

Answers (3)

Matt
Matt

Reputation: 15061

SELECT * 
FROM
    (SELECT *
     FROM dbo.[sometable]
    )
AS [NAME]

You need to include an alias for the sub query to be referenced by the outer query

Upvotes: 1

glaeran
glaeran

Reputation: 426

select * from
(
select * from dbo.[sometable]
) as X 

You always need to create alias if using FROM subquery even if later on you're not using this alias that's just how the thing works.

Upvotes: 4

Ehsan
Ehsan

Reputation: 32691

i want to select all the records from sometable then this should be enough

select * from dbo.[sometable]

However, if you want to filter your records of one table based on values from some other table then you can use nested queries like

select * from table1 where id in (select id from table2)

or you can use joins like

select * from table1 t
join table2 t2 on t.id = t2.id

Upvotes: 3

Related Questions