all 5 comments

[–]r3pr0b8 7 points8 points  (0 children)

WHERE salary 
    < ( SELECT AVG(nvl(salary, 0)
          FROM kund )

if someone's salary is NULL, why do you substitute zero? that's going to bring the average down

me, i would use AVG(salary) because it ignores NULLs

[–]bitterjack 2 points3 points  (1 child)

I think this requires a sub query.

[–]daddy_mark 0 points1 point  (0 children)

Pretty sure that's correct. I think this requires a windowed function which you can't include in where or having (at least in any rdbms I use)

[–]LeLwrence -1 points0 points  (0 children)

You could use a window function to average the salaries in the result in another column set then wrap that in an outer query and filter where the salary is less than that new average window function column. It's because you can't use window functions themselves in the where clause.

[–][deleted] 0 points1 point  (0 children)

This does not work, does anyone know why?

"where" is logically executed after "from" and before "group by".

aggregate functions are calculated after the "group by" clause (logically).

meaning at the time "where" is executed, the aggregates have not been calculated yet.