• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

SQL Question -- why can't I use an aggregate function here?

 
Bartender
Posts: 1849
15
Eclipse IDE Spring VI Editor Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This, to me, looks like it should return the department_id and AVG(salary) where the AVG(salary) is greater than $8000..... but the tutorial says it returns an error....

Why?

from Skillsoft SQL course - creating and manipulating group functions

Okay, so if this is synctactically incorrect.... how SHOULD it look? Do I need a subquery instead?
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Janeice,
Aggregate functions can't go in the where clause because that goes against each row rather than the grouped function. Instead it goes in the "having" clause.
 
Janeice DelVecchio
Bartender
Posts: 1849
15
Eclipse IDE Spring VI Editor Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, Jeanne! The book had something complicated to say about the order the statement gets processed. Your answer makes a ton more sense.

So I would do this:



Or this (I think) would be ok too:


Great!

Can I use an ORDER BY with the HAVING, too?
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Janeice DelVecchio wrote:
Can I use an ORDER BY with the HAVING, too?


Yes.
 
reply
    Bookmark Topic Watch Topic
  • New Topic