Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Basic SQL query Get Count of Student in Join

Author  Topic 

dhinasql
Posting Yak Master

195 Posts

Posted - 2013-05-07 : 16:06:55
I have two table,

Table1:
DepId DepName
1 CSE
2. IT
3. MECH

Table2:
StudID Name DepID
1 ABC 1
2 XYZ 1
3 QWE 2
4 OPI 1

I want to display the Department Name from Table 1 if that is exist in Table 2

Expected Result

DEPNAME
CSE
IT

There is No student available for Mech in Table2 so it should not disply in result.

chadmat
The Chadinator

1974 Posts

Posted - 2013-05-07 : 16:12:08
SELECT DepName
FROM Table1 t1
WHERE t1.DepID in (SELECT DISTINCT DepID FROM Table2)

-Chad
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-05-08 : 00:14:43
[code]
SELECT DepName
FROM Table1 t1
WHERE EXISTS (SELECT 1 FROM table2 WHERE DepID = t1.ID)
[/code]

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
   

- Advertisement -