-1

I have an interview question to select the maximum salary with the ID.

The correct answer should look like:

SELECT WORKER_REF_ID,  BONUS_AMOUNT FROM Practice.Bonus
WHERE BONUS_AMOUNT = (SELECT MAX(BONUS_AMOUNT) FROM Practice.Bonus);

Why shouldn't we use the subquery here instead of

SELECT WORKER_REF_ID,  BONUS_AMOUNT FROM Practice.Bonus
WHERE BONUS_AMOUNT = MAX(BONUS_AMOUNT);

?

Sorry, I am a new beginner with a simple question.

Raymond Nijland
  • 11,488
  • 2
  • 22
  • 34
Huan Zheng
  • 19
  • 2

1 Answers1

1

It's because max is an aggregate function.

You can think of the where clause as a set of rules that get evaluated once for each row in the source data.

You need to provide an aggregate function like max with a set of rows to aggregate. It doesn't make sense to ask for the "maximum" of a single value like bonus_amount.

Jeffrey Kemp
  • 59,135
  • 14
  • 106
  • 158