Solved

query ...

Posted on 2003-11-20
6
498 Views
Last Modified: 2012-05-04
There are 2 tables - Dept(DeptId,DeptName) and Emp(EmpId,EmpName,DeptId).
What will be the query for retrieving all DeptNames and the no of employees in that dept ?
0
Comment
Question by:Amita
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 3

Expert Comment

by:ubasche
ID: 9787200

For Oracle you can use this query:

select d.deptname, count(*) as num_of_emps
from d.dept, e.emp
where d.deptid = e.deptid
group by d.deptname;

or in case some departments don't have employees (?)

select d.deptname, count(*) as num_of_emps
from d.dept, e.emp
where d.deptid = e.deptid (+)
group by d.deptname;
0
 
LVL 6

Expert Comment

by:lausz
ID: 9787201
Try this

select deptname , count(empid )
from dept d, emp e
where d.deptid = e.deptid
group by  deptname

0
 
LVL 8

Expert Comment

by:SNilsson
ID: 9787210

Select Dept.DeptName, count(Emp.EmpId)
From Dept, Emp
Where
Emp.DeptId = Dept.DeptId
Group By
Dept.DeptName
0
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
LVL 5

Expert Comment

by:allanau20
ID: 9789381
Ubasche,

All of the above should work. But, just to help -- there's more than one way to skin sql, I mean a cat.

Try this:

SELECT     d.DeptName, Count (d.EmpId) as TotEmployeeInDepartment
FROM       Dept d
                INNER JOIN Emp e ON d.DeptId = e.DeptId
GROUP BY d.DeptName
0
 
LVL 35

Expert Comment

by:YZlat
ID: 9790558
select Dept.DeptName, Count(Emp.EmpID) as NumberOfEmployees
FROM Dept INNER JOIN Emp on Dept.DeptID = Emp.DeptID
ORDER BY Dept.DeptName
0
 
LVL 2

Accepted Solution

by:
David Little earned 50 total points
ID: 9791458
In order to make sure you get ALL departments, even if they don't have employees, change the join to a LEFT OUTER...

select Dept.DeptName,
         Count(Emp.EmpID) as NumberOfEmployees
FROM Dept LEFT OUTER JOIN Emp on Dept.DeptID = Emp.DeptID
ORDER BY Dept.DeptName
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Never store passwords in plain text or just their hash: it seems a no-brainier, but there are still plenty of people doing that. I present the why and how on this subject, offering my own real life solution that you can implement right away, bringin…
These days, all we hear about hacktivists took down so and so websites and retrieved thousands of user’s data. One of the techniques to get unauthorized access to database is by performing SQL injection. This article is quite lengthy which gives bas…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question