user9847911
user9847911

Reputation:

How to get the unmatched records from two tables using Joins

I have two tables one is teacher and another is Department which is mentioned below.

Teacher Table

Id Name 
1    xyz 
2.   Gjd
3.   Dftr
4    dhdk

Department Table

Id Name  EMPID
1    SQL.     2
2.   PHP.     4
3.   JAVA.   1
4    PEARL. 5

QUESTION i want those records of teacher which are not link with any Department.

Upvotes: 0

Views: 4193

Answers (3)

Ahmed Yousif
Ahmed Yousif

Reputation: 2348

you can use following statement using left join then filter Teacher that not matched

SELECT t.* 
FROM Teacher t
left join Department d on d.EMPID = t.Id 
where d.id is null

Upvotes: 3

Aagam Jain
Aagam Jain

Reputation: 1546

you can do it by inner query..

select * from teacher where id not in (select empid from department);

Upvotes: 0

Arihant
Arihant

Reputation: 745

SELECT * FROM teachers WHERE 
id NOT IN (SELECT DISTINCT EMPID FROM departments) ;

Hope this helps.!!

Upvotes: 0

Related Questions