Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a query be used in place of a table in SQL Server

This should really be allowed - I do not understand why it is not.

SELECT * 
FROM (
    SELECT * 
    FROM MyTable
)
like image 795
Jack Kada Avatar asked Dec 30 '25 07:12

Jack Kada


2 Answers

In SQL Server it is allowed, but the inner select has to be given a name, such as:

SELECT *  
FROM ( 
    SELECT *  
    FROM MyTable
) m

When a name is not supplied it will throw an incorrect syntax error near ')' message.

like image 62
Andrew Avatar answered Jan 01 '26 19:01

Andrew


If you add a table alias it should work:

SELECT * 
FROM (
    SELECT * 
    FROM MyTable
) as A