0

I always thought I need groupby statement after I use any aggregated function. Like for exmaple max() in MSSQL.

But surprisingly if I have to find the second highest salary this query works:

select max(Salary)from Employee
      where Salary < (select max(Salary) from Employee);

Source of claim

Are there any exceptions to this rule that I am not aware of?

Shadow
  • 33,525
  • 10
  • 51
  • 64
  • 2
    In the absence of a `GROUP BY` clause aggregation occurs over *all* rows returned by the query. – Nick Apr 18 '20 at 01:22
  • The inverse is true; in the absence of any aggregating functions, a group by clause is never appropriate. And, when mixing aggregated and non-aggregated values, a group by clause is required (whatever anyone else including the creators of MySQL - tries to tell you) – Strawberry Apr 18 '20 at 07:18

0 Answers0