Thursday, October 14, 2010

No of employees in dept

SELECT COUNT (DISTINCT A||B) FROM TABLE --distinct at a time in a table

Query to find the employees in a department having maximum number of emps

SELECT e.empno, e.ename, e.sal, e.deptno
FROM       emp         e
,               (
                                SELECT      deptno
                                ,                 RANK () OVER (ORDER BY COUNT (*)     DESC)      AS rnk
                                FROM         emp
                                GROUP BY  deptno
                )               s
WHERE     e.deptno  = s.deptno
AND         s.rnk                        = 1
;

Or

select empno, ename, sal, a.deptno
from emp a,
(select deptno from (select deptno, count(1) over (partition by (deptno)) from emp order by 2 desc) where rownum =1) b
where a.deptno = b.deptno;

No comments:

Post a Comment