Click here to Skip to main content
15,898,897 members
Please Sign up or sign in to vote.
1.40/5 (3 votes)
Find employee who is living in more than one city in the below two tables

EMP_TABLE
----------
EMPID
EMPNAME


CITY_TABLE
-----------
EMPID
PLACE

What I have tried:

I have lot of confusion in above query,please can anyone give me a solution for above one
Posted
Updated 16-Feb-17 9:05am
Comments
Suvendu Shekhar Giri 13-Feb-17 3:03am    
Share the incorrect query you have tried so far.

First of all, would recommend to try something before asking question in the forums.
You can try something like-
SQL
SELECT EMPID,EMPNAME,COUNT(PLACE) AS NoOfPlaces
FROM EMP_TABLE E
LEFT JOIN CITY_TABLE C ON E.EMPID=C.EMPID
GROUP BY EMPID,EMPNAME
HAVING COUNT(PLACE)>1


Hope, it helps :)
 
Share this answer
 
Comments
Naresh S 13-Feb-17 4:21am    
tried to execute with below query


select a.empname from emp5 a left join city b on a.empid=b.empid where a.empid in(select empid from city )
Try this:
select * from emp_table e where exists ( select 1 from city_table c where c.empid=e.empid having count(*) > 1)
 
Share this answer
 
v2
select e.empname from emp5 e inner join city b  on e.empid=b.empid  where e.empid
 in(select empid from city group by empid having count(*)>1)


I have given for two different places one empid

Please correct me if i am wrong.
 
Share this answer
 
select a.empname from emp5 a,city b where a.empid=b.empid group by a.empname having count(*)>1
 
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