Click to See Complete Forum and Search --> : Sql query help


preciousmind
03-31-2009, 11:55 AM
I Want a help to write a query for the following example

Table A: Employee
Structure:
EMPNO int
ENAME varchar2(35)
DEPTID

Data:
EMPNO ENAME DEPTID
101 JONES 10
102 CLARK 10
103 SMITH 20
104 MARTIN 30
105 HAROLD 30

Table B: Department
Structure:
DEPTID int
DNAME varchar2(35)

Data:
DEPTID DNAME
10 SALES
20 ACCOUNTS
30 IT
40 ADMIN
========================================
Now I want a query with the following output

DNAME EMPLOYEE COUNT
SALES 2
ACCOUNTS 1
IT 2
ADMIN 0

=====================================

Any help is appreciated in advance :)

dingbat
03-31-2009, 04:20 PM
Do a Select from Department, and left join Employee. Group by DEPTID and use count(*) for the count.

preciousmind
04-01-2009, 02:07 AM
I wrote this query
select dname, count(empno) "Employee Count" from dept,emp where
emp.deptno(+)=dept.deptno group by dname order by "Employee Count",dname

The tables used are from default sample in oracle voz dept and emp tables

I got the following output:

DNAME Employee Count
-------------- --------------
OPERATIONS 0
ACCOUNTING 3
RESEARCH 5
SALES 6

Thanx for the help ding ;)