Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nested select statement in SQL Server

Why doesn't the following 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?

like image 913
Brennan Vincent Avatar asked Jan 07 '11 20:01

Brennan Vincent


People also ask

What is nested query in SQL Server?

A Subquery or Inner query or a Nested query is a query within another SQL query and embedded within the WHERE clause. A subquery is used to return data that will be used in the main query as a condition to further restrict the data to be retrieved.

What is nested queries in SQL give an example?

A nested query is a query that has another query embedded within it. The embedded query is called a subquery. A subquery typically appears within the WHERE clause of a query. It can sometimes appear in the FROM clause or HAVING clause.

How do I select within a select in SQL?

The subquery can be nested inside a SELECT, INSERT, UPDATE, or DELETE statement or inside another subquery. A subquery is usually added within the WHERE Clause of another SQL SELECT statement. You can use the comparison operators, such as >, <, or =.


1 Answers

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   
like image 168
Joe Stefanelli Avatar answered Sep 20 '22 20:09

Joe Stefanelli