Brennan Vincent Brennan Vincent - 2 months ago 8
SQL Question

Nested select statement in SQL Server

Can someone help me understand why the following doesn't work?

SELECT name FROM (SELECT name FROM agentinformation)


I guess my understanding of SQL is wrong, because I would have thought this would return the same thing as

SELECT name FROM agentinformation


Doesn't the inner select statement create a result set which the outer SELECT statement then queries?

Answer

You need to alias the subquery.

SELECT name FROM (SELECT name FROM agentinformation) a  

or to be more explicit

SELECT a.name FROM (SELECT name FROM agentinformation) a