views:

18

answers:

3

I have a syntax error in this subquery that I cannot seem to figure out why it won't work. All the parens are matched

select min(max_s) 
from 
(select max(salary) from instructor group by dept_name) 
as s(max_s);

Error: near "(": syntax error
+1  A: 

The problem is in the AS s(max_s) table alias, which doesn't look quite right. You should alias the column name inside the subquery, for example:

select min(s.max_s) 
from 
(select max(salary) as max_s from instructor group by dept_name) 
as s
martin clayton
A: 

Don't put parens after a table alias.

Bill Karwin
+1  A: 

Use:

SELECT MIN(x.max_s) 
  FROM (SELECT MAX(i.salary) AS max_s 
          FROM INSTRUCTOR i
      GROUP BY i.dept_name) x
OMG Ponies
I don't understand why the second half of your answer would be true. I'm trying to find the minimum after grouping by department and then finding the maximums of that group.
controlfreak123
OMG Ponies
thats quite alright. I also noticed that I can leave off the x alias because it knows that max_s is in the subquery.
controlfreak123
@controlfreak123: Some databases (mySQL, SQL Server) require a table alias for a derived table/inline view; I always define table aliases.
OMG Ponies
I see. Good to know!
controlfreak123