Could you please help me with SQL statement to find duplicate names present in first Select statement and the second.
For example : I need to find whether student with same name in Class 1 is present or not in Class 2 or 3.
select firstname from College where classid = 1
and Second select statement is :
select firstname from College where classid in (2, 3)
If there is matching occurrences for any name then I want to select that record.
Advertisement
Answer
Join could be one option
select a.firstname from ( select firstname from College where classid = 1 )a join ( select firstname from College where classid in(2,3) )b on a.firstname=b.firstname
OR alternatively, you can try using exists
select firstname from College c where classid = 1 and exists (select 1 from College c1 where c.firstname=c1.firstname and classid in(2,3))