0
votes

enter image description here

I get this error:

Msg 147, Level 15, State 1, Line 3
An aggregate may not appear in the WHERE clause unless it is in a subquery contained in a HAVING clause or a select list, and the column being aggregated is an outer reference.

4

4 Answers

1
votes

Use TOP:

select top (1) name
from student s
order by grant desc;

The above returns one row always, even when there are ties for the highest grant. If you want all such rows, you can add the with ties modifier:

select top (1) with ties name
from student s
order by grant desc;
0
votes

You can use:

SELECT [name]
FROM [student]
WHERE [grant] = (SELECT max([grant]) FROM [student])
0
votes

The following code can be correct if only one student has the max grant:

select top (1) name
    from student s
    order by grant desc;

if you have two or more student with the same grant, you should write as below:

select * from
(
select 
id,
name,
Dens_Rank() over (order by grant desc) student_rank
)temp where temp.student_rank=1
0
votes

Please find below query -

SELECT * FROM student WHERE ROWNUM < 2 ORDER BY grant DESC;