Click here to Skip to main content
15,917,594 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hi All
I have two tables: 1.tblEmp(comntain employees) and 2.tblDep(contain departments)
tblEmp contain
id
name
depid columns
depid is foreign key to table tblEmp.

Now the problem is that i have to pick only those records from tblDep which has atleast 5 employees.

Thanks in advance
Posted
Comments
Sharma Richa 31-Jul-12 9:18am    
If you down voted question then you should have courage to say why ?

what database is this?
MySql or MSSQL?

Thanks.
 
Share this answer
 
Comments
Sharma Richa 26-Jan-12 2:11am    
database is MSSQL
graciax8 26-Jan-12 2:21am    
try this one.

SELECT d.depID, count(e.ID)
FROM tblDep AS d
LEFT OUTER JOIN tblEmp AS e
GROUP BY e.depID
HAVING count(e.ID) > 5

Michel [mjbohn] 26-Jan-12 2:15am    
Please use the "Have a Question or Comment?" widget for your questions.
You can also reply to comments.
Sharma Richa 26-Jan-12 2:52am    
now problem is that i have to select employee name and other columns of both tables.
graciax8 26-Jan-12 3:44am    
just add the column on the SELECT statement
SELECT column>, column2, column3 FROM ...
I don't think you'll need a JOIN here. If I am interpreting correctly, your tblDep has a column with the number of employees (let's call it 'nremp') in department depid. So why not use:
SQL
select *
from tblEmp, tblDep
where tblEmp.depid = tblDep.depid
and tblDep.nremp >= 5
;
 
Share this answer
 
Comments
graciax8 26-Jan-12 2:33am    
you just used a comma to join the tables.
it's the same.
Here it is :

SQL
select d.depid, count(*) cnt  from tblDep d inner join tblEmp e on d.depid = e.depid
group by d.depid
having count(*)>4
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900