30 Day Risk-Free Guarantee:
100% money back if you're unsatisfied.
Book (308 Pages):
  • 150 Programming Interview Questions and Solutions
  • Five Proven Approaches to Solving Tough Algorithm Questions
  • Ten Mistakes Candidates Make -- And How to Avoid Them
  • Steps to Prepare for Behavioral and Technical Questions
  • Interview War Stories: A View from the Interviewer's Side
  • Book Preview and More Info

Video (One Hour):
  • Watch CareerCup's founder perform a totally unscripted technical interview of a candidate.
  • Overview of what interviewers look for in software engineering jobs.
  • Technical questions with white-boarding coding where the candidate does well - and struggles.
  • Interviewer reviews with what went well and poorly.
  • Video Preview and More Info

Resume Review (24 - 48hr)
  • Get your resume reviewed by a trained engineering interviewer. More Info
All Products / Services


Express Prep Package (Book)
$29.99 for e-book | $32.45 for paperback
Buy E-Book Buy on Amazon


Standard Prep Package (E-Book & Video)
Emailed Instantly | $39.99
Buy Standard

Elite Prep Package (E-Book, Video & Resume)
Emailed Instantly | $99.99
Buy Elite
 



Interviewer> If we have a table containing Emp_ID,Emp_Name and Dept_ID(Primary key) and another table having Emp_Name and Dept_ID (foreign key), how would u find the number of employees in each dept?

8


Anonymous on February 11, 2007 |Edit | Edit

Since the Dept_Id is the primary key in the first table ,which is quite bizzare....it makes sense that the each dept has one employee..

Anonymous on February 12, 2007 |Edit | Edit

The primary key for the first table should obviously be Emp_ID

select COUNT(*) from Emp, Dept where Emp.Emp_Name=Dept.Emp and Emp.Dept_ID=Dept.Dept_ID GROUP BY Dept_ID

Tanuj on July 21, 2007 |Edit | Edit

This was pretty easy question .. to make things a bit complicated the Interviewer can ask you , if say some department (for eg. dept 20 has no employees) ... Then the above query wont work .....
Then use Outer join in the Where clause ...
where emp.dept_id(+) = dept.dept_id ... Also note the position of the outer join , the most important part...

Anonymous on October 15, 2008 |Edit | Edit

why would two tables be needed? Either table has Dept_ID, Emp_ID or Emp_Name shouldn't matter because we just need distinctive count

Would this work?

SELECT COUNT (DISTINCTIVE Emp_ID) FROM Emp GROUP BY Dept_ID

Assume Emp_ID is the primary key. Of course the fact that there's a second table with foreign key suggests some sort of JOIN but ...

anonymous on March 05, 2009 |Edit | Edit

I think the question has been mis-framed.
Table 1 - Emp_ID, Emp_Name, Dept_ID (foreign key)
Table 2 - Dept_Name, Dept_ID (primary key)
The solution to the question would be -
SELECT Dept_ID, count(Emp_ID)
FROM Table 1
GROUP BY Dept_ID ;

anonymous on March 05, 2009 |Edit | Edit

I think the question has been mis-framed.
Table 1 - Emp_ID, Emp_Name, Dept_ID (foreign key)
Table 2 - Dept_Name, Dept_ID (primary key)
The solution to the question would be -


SELECT Dept_ID, count(Emp_ID)
FROM Table 1
GROUP BY Dept_ID ;

Rajesh Konda on May 06, 2009 |Edit | Edit

I think the question is, for each dept, given by name, I want to know how many employees are in that department. As someone suggested above, a dept may not have any employees. In that case, I was to see a '0' against that department. The query would be


Select Dept_Name, Count(*) as NoOfEmpls
from
(
Select E.Emp_ID, D.Dept_ID, D.Dept_Name
from Employees E
rightjoin
Department D
On E.Dept_ID=D.Dept_ID
)

By doing a rightjoin you are ensuring that even if a department doesn't have employees, it would still be counted.

Rajesh

Anonymous on June 09, 2010 |Edit | Edit

Count(*) should be Count(E.Emp_ID)

Add a Text Comment | Add Runnable Code
Name:
Comment:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.








Amazon (1033)Bloomberg LP (403)Qualcomm (117)Adobe (88)Goldman Sachs (78)NetApp (49)IBM (43)Morgan Stanley (33)CapitalIQ (30)Sophos (25)Achieve Internet (23)Electronic Arts (19)Motorola (18)Research In Motion (17)Flipkart (16)
Microsoft (867)Google (141)NVIDIA (98)Yahoo (82)Epic Systems (69)Expedia (47)VMWare Inc (43)Apple (32)Cisco Systems (28)Facebook (23)Infosys (22)Agilent Technologies (19)Sage Software (17)Deshaw Inc (16)FlexTrade (15)
More Companies »
Software Engineer / Dev... (1062)Financial Software Deve... (170)Testing / Quality Assur... (56)Analyst (35)Virus Researcher (25)Field Sales (15)Developer Program Engin... (9)Front-end Software Engi... (6)MyJoB (5)area sales manager (4)Assistant (3)Cabin crew (2)Accountant (1)personnel (1)Intern (1)
Software Engineer in Te... (288)Program Manager (65)Development Support Eng... (47)INTERN(MSIDC) (28)Web Developer (18)System Administrator (10)Consultant (10)Production Engineer (5)Associate Technology L2 (5)AcquireKnowledge (4)Product Security Engine... (3)Solutions Architect (2)Gamer (1)mts (1)Fresh graduate interview (0)
More Job Titles »
Algorithm (1073)Terminology & Trivia (294)C (166)Object Oriented Design (159)Java (121)Testing (114)Arrays (101)Operating System (78)Database (70)Linked List (62)String Manipulation (56)Networking / Web / Inte... (44)Threads (36)Linux Kernel (33)PHP (22)
Coding (511)C++ (204)Behavioral (159)Data Structures (155)Experience (116)Brain Teasers (111)Computer Architecture &... (79)General Questions and C... (73)Trees and Graphs (69)Math & Computation (57)Application / UI Design (45)Ideas (38)System Design (35)Puzzles (30)Bit Manipulation (20)
More Topics »
CareerCup Official Interview Book Daily Questions Requests for Help Mock Interviews Video for Cracking the Coding Interview Job Placement Service CareerCup Blog
My Profile Edit Profile & Email Settings Sign Out