Click here to Skip to main content
15,913,685 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
suppose i have Two table
Employee{empid,empname,salary}
Contectdetails(empid,phoneno)
so what is the output for the query like
1)select all the employee that have No phone no?
2)select all the employee that have more then 2 phone no ?
please give me some idea..........
Posted
Comments
CHill60 25-Mar-13 6:19am    
What have you tried so far? This sounds like homework and you won't learn if you don't try first. Use the Improve question link to post your efforts so far
pandya purvang 25-Mar-13 6:32am    
sir i have tried using join but it is not shown proper output so please dont treat it as homework
Sushil Kumar Purohit 25-Mar-13 6:51am    
What about my answer purvang ?
pandya purvang 25-Mar-13 7:44am    
@Sushil Sir you have not make any join condition so may be its wrong okk i will check in sql then i will reply to u

Case 1:

SELECT EmpId FROM Contectdetails
WHERE phoneno IS NULL

Case 2:

SELECT EmpId FROM Contectdetails
GROUP BY EmpId HAVING COUNT(phoneno) > 2
 
Share this answer
 
This is a little complex, so it's best to build it in easy stages.
Assume you have three Employees:
EmpId	EmpName	Salary
1	Joe       	1000
2	Fred      	2000
3	Mike      	3000
And some telephone numbers:
EmpId	phoneNo
  1	1111111    
  3	3333331   
  3	3333332   
  3	3333333   

First, write a query that returns the EmpID and the number of phone numbers you have for them:
SQL
SELECT EmpId , COUNT(PhoneNo) AS pnc FROM ContectDetails 
GROUP BY EmpId

EmpId   pnc
  1      1
  3      3

You can then use this to JOIN with the Employee table:
SQL
SELECT * FROM Employee e
JOIN (SELECT EmpId , COUNT(PhoneNo) AS pnc FROM ContectDetails c
      GROUP BY (c.EmpId)) s
ON e.EmpId = s.EmpId
As you can see, the first query slots in with little or no modification.
Empid	EmpName	Salary	EmpId	pnc
1	Joe   	1000	  1	 1
3	Mike  	3000	  3	 3

All you have to do then, is apply a WHERE clause to filter the results:
SQL
SELECT * FROM Employee e
JOIN (SELECT EmpId , COUNT(PhoneNo) AS pnc FROM ContectDetails c
      GROUP BY (c.EmpId)) s
ON e.Empid = s.EmpId
WHERE pnc = 0
And
SQL
SELECT * FROM Employee e
JOIN (SELECT EmpId , COUNT(PhoneNo) AS pnc FROM ContectDetails c
      GROUP BY (c.EmpId)) s
ON e.Empid = s.EmpId
WHERE pnc > 2
 
Share this answer
 
Using join :
1.
SQL
select A.*,B.Phoneno from Employee A join Contectdetails B
on A.Emp_ID=B.Emp_ID
where ISNULL(B.Phoneno, 0)=0

2.
SQL
select A.*,B.Phoneno from Employee A join Contectdetails B
on A.Emp_ID=B.Emp_ID
where A.Emp_ID in(
SELECT Emp_Id FROM Contectdetails
GROUP BY Emp_Id HAVING COUNT(phoneno) > 2)
 
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