max(count(*))....???

Discussion in 'Databases' started by Namuk, Mar 30, 2008.

  1. #1
    i has a problem about this query...

    SELECT d.deptno, d.dname, COUNT(*)
    FROM dept d, emp e
    WHERE d.deptno = e.deptno
    GROUP BY d.deptno, d.dname
    HAVING COUNT(*) = (SELECT max(count(*))
    FROM emp
    group by deptno);


    error : Invalid use of group function

    how do i can get the max value of the count??..

    please help...
     
    Namuk, Mar 30, 2008 IP
  2. budster

    budster Peon

    Messages:
    18
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Hi Namuk

    This is one solution

    SELECT d.deptno, d.dname, COUNT(*)
    FROM dept d, emp e
    WHERE d.deptno = e.deptno
    GROUP BY d.deptno, d.dname
    HAVING COUNT(*) = (SELECT max(c)
    FROM ( SELECT deptno, COUNT(*) c
    FROM emp
    group by deptno));
     
    budster, Mar 31, 2008 IP
  3. mwasif

    mwasif Active Member

    Messages:
    816
    Likes Received:
    23
    Best Answers:
    1
    Trophy Points:
    70
    #3
    What do you think about this?
    SELECT d.deptno, d.dname, COUNT(*) AS cnt
    FROM dept d, emp e
    WHERE d.deptno = e.deptno
    GROUP BY d.deptno, d.dname
    ORDER BY cnt DESC 
    LIMIT 1
    Code (markup):
     
    mwasif, Mar 31, 2008 IP
  4. ravi72194

    ravi72194 Banned

    Messages:
    270
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #4
    query is working
     
    ravi72194, Mar 31, 2008 IP
  5. mwasif

    mwasif Active Member

    Messages:
    816
    Likes Received:
    23
    Best Answers:
    1
    Trophy Points:
    70
    #5
    Which one worked?
     
    mwasif, Mar 31, 2008 IP
  6. budster

    budster Peon

    Messages:
    18
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #6
    The two queries will give different results if two or more departments have the equally the most employees.
    It all depands on the result you are after.
     
    budster, Apr 1, 2008 IP