|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Monday, November 10, 2008 12:16 PM
Points: 2,
Visits: 12
|
|
Hi,
I have Department Table having DepartmentId,Departmentname columns and Employee Table having EmployeeId,EmployeeName,DepartmentId.
I have to Display List all the departments that don’t have any employees(Without using sub query).
Please do reply as soon as possible.
Thanks, Chow.
|
|
|
|
|
SSC-Addicted
      
Group: General Forum Members
Last Login: Tuesday, April 16, 2013 6:34 AM
Points: 476,
Visits: 1,361
|
|
|
|
|
|
SSCrazy Eights
        
Group: General Forum Members
Last Login: Saturday, May 04, 2013 11:13 AM
Points: 9,855,
Visits: 9,374
|
|
|
|
|
|
SSCrazy Eights
        
Group: General Forum Members
Last Login: Saturday, May 04, 2013 11:13 AM
Points: 9,855,
Visits: 9,374
|
|
This should do it:
Select DepartmentId, Departmentname From Department Left Outer Join Employee ON Employee.DepartmentID = Department.DepartmentID Where Employee.DepartmentID IS NULL
-- RBarryYoung, (302)375-0451 blog: MovingSQL.com, Twitter: @RBarryYoung Proactive Performance Solutions, Inc. "Performance is our middle name."
|
|
|
|
|
Old Hand
      
Group: General Forum Members
Last Login: Wednesday, September 12, 2012 5:17 AM
Points: 329,
Visits: 461
|
|
rbarryyoung (10/29/2008)
naidussdba (10/29/2008) I have to Display List all the departments that don’t have any employees(Without using sub query).So why this unusual restriction? Becuase it is an interview question ;)
Madhivanan
Failing to plan is Planning to fail
|
|
|
|
|
SSC-Addicted
      
Group: General Forum Members
Last Login: Tuesday, April 16, 2013 6:34 AM
Points: 476,
Visits: 1,361
|
|
Madhivanan (10/30/2008)
rbarryyoung (10/29/2008)
naidussdba (10/29/2008) I have to Display List all the departments that don’t have any employees(Without using sub query).So why this unusual restriction? Becuase it is an interview question ;)
Or a homework question :P
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Friday, May 17, 2013 11:53 AM
Points: 1,303,
Visits: 1,661
|
|
| I would group by department having count(*) = 0.
|
|
|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Tuesday, April 03, 2012 7:36 AM
Points: 127,
Visits: 517
|
|
Ian Massi (10/31/2008) I would group by department having count(*) = 0.
won't the count be equal to or greater than 1 always? if the count is 0, then it doesn't exist
I guess if you did a left join a did a count on the employeeid which a count(null) = 0? since null's are excluded from aggrigate functions?
probably still faster to do there left join where emp.deptid is null since it won't require grouping everything together.
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Friday, May 17, 2013 11:53 AM
Points: 1,303,
Visits: 1,661
|
|
| Oh yeah, what on Earth was I thinking! The left outer join was definitely the way to go.
|
|
|
|