0

I have two tables. One is the Course table and the second is the Teacher table. I want to get all Teacher who does not teach 'Math'. How can I do this?

Course Table

course_id   course  teacher_id  marks
 1          Physics     1        60
 2          Math        1        60
 3          Chemestry   1        60
 4          English     2        60
 5          Hindi       2        60
 6          Physics     2        60
 7          Chemestry   3        60
 8          English     4        60
 9          Math        5        60
10          Math        6        60

Teacher Table

teacher_id  name      salary    gender
   1       Teacher1    20         1
   2       Teacher2    30         1
   3       Teacher3    40         2
   4       Teacher4    50         2
   5       Teacher5    60         1
   6       Teacher6    70         2
Lalit Kumar B
  • 47,486
  • 13
  • 97
  • 124
ankit singh
  • 367
  • 1
  • 3
  • 13
  • You have to use a `JOIN`, there are many tutorials out there. Tinker around with join and post your query if you can't figure out a solution. – JDurstberger Apr 16 '15 at 05:23
  • Use 'Foreign Key' in Course Table field of 'teacher_id'. So it makes the code to move clearly without JOINs – Abdulla Nilam Apr 16 '15 at 05:25

5 Answers5

1

I want to get all teacher who does not teachs math.

You need to join both the tables on teacher_id and then filter out the rows based on the course.

SQL> SELECT DISTINCT t.name
  2  FROM course c,
  3    teacher t
  4  WHERE c.teacher_id = t.teacher_id
  5  AND c.course     <> 'Math';

NAME
--------
Teacher2
Teacher1
Teacher4
Teacher3

SQL>

EDIT Since you have teachers teaching multiple courses, you need to filter out further:

SQL> WITH DATA AS
  2    (SELECT c.*,
  3      t.name
  4    FROM course c,
  5      teacher t
  6    WHERE c.teacher_id = t.teacher_id
  7    AND c.course      <> 'Math'
  8    )
  9  SELECT DISTINCT name
 10  FROM data
 11  WHERE teacher_id NOT IN
 12    (SELECT teacher_id FROM course WHERE course = 'Math'
 13    )
 14  /

NAME
--------
Teacher2
Teacher4
Teacher3

SQL>

NOTE Please keep in mind that the other solution using NOT EXISTS clause is better in terms of performance, since the table scans are less and even index scans. With proper indexes, the not exists query would be an optimal method.

Lalit Kumar B
  • 47,486
  • 13
  • 97
  • 124
1
select * 
from teacher t 
where not exists 
(select 1 from course c where c.teacher_id = t.teacher_id and c.course = 'Math')

@LalitKumarB Ben is absolutely right

knagaev
  • 2,897
  • 16
  • 20
0

inner join

select t.teacher_id, t.name
from teacher t, Course c 
where c.course='math' and t.teacher_id=c.teacher_id;

EDIT

you can do it using join and subquery.

select * from course join teacher 
on course.teacher_id=teacher.teacher_id
where teacher.teacher_id not in 
(select distinct teacher_id from course where course = 'Math')
Community
  • 1
  • 1
Ameya Deshpande
  • 3,580
  • 4
  • 30
  • 46
-1
Select * from Teacher 
join Course 
on Teacher.teacher.id = Course.teacher.id 
where Course.course != 'Math'
ajaykumartak
  • 776
  • 9
  • 29
-1
select 
    t.name
from teacher t 
left join course c 
on c.teacher_id = t.teacher_id 
    where  c.course_id <> 2
user4221591
  • 2,084
  • 7
  • 34
  • 68