[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

query ...

Posted on 2003-11-20
6
Medium Priority
?
509 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
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!

 
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 200 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
In this blog post, we’ll look at how using thread_statistics can cause high memory usage.
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

656 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