Question

These are my tables

mysql> select * from professor;
    +-------+--------+--------+--------+------+
    | empid | name   | status | salary | age  |
    +-------+--------+--------+--------+------+
    |     1 | Arun   |      1 |   2000 |   23 |
    |     2 | Benoy  |      0 |   3000 |   25 |
    |     3 | Chacko |      1 |   1000 |   36 |
    |     4 | Divin  |      0 |   5000 |   32 |
    |     5 | Edwin  |      1 |   2500 |   55 |
    +-------+--------+--------+--------+------+
    5 rows in set (0.00 sec)

mysql> select * from works;
+----------+-------+---------+
| courseid | empid | classid |
+----------+-------+---------+
|        1 |     1 |      10 |
|        2 |     2 |       9 |
|        3 |     3 |       8 |
|        4 |     4 |      10 |
|        5 |     5 |       9 |
|        6 |     1 |       9 |
|        2 |     3 |      10 |
|        2 |     1 |       7 |
|        4 |     2 |       6 |
|        7 |     5 |       6 |
|        3 |     5 |       2 |
|        2 |     4 |       6 |
|        2 |     5 |       2 |
+----------+-------+---------+
15 rows in set (0.00 sec)

mysql> select * from course;
+----------+------------+--------+
| courseid | coursename | points |
+----------+------------+--------+
|        1 | Maths      |      4 |
|        2 | Science    |      4 |
|        3 | English    |     85 |
|        4 | Social     |      4 |
|        5 | Malayalam  |     99 |
|        6 | Arts       |     40 |
|        7 | Biology    |    100 |
+----------+------------+--------+
7 rows in set (0.00 sec)

Question is :

Return the names of full professors who have taught at least two courses in one Class 

My query is :

select professor.name from professor
inner join works
on professor.empid=works.empid
group by works.empid
having count(distinct works.courseid)>=2

The ouput I get now is :

Arun
Benoy
Chacko
Divin
Edwin

I am supposed to get the ouput as 'Edwin' as he is the only person who has taught 2 subjects in the same class. Pls help

Was it helpful?

Solution

you must consider your group by is wrong change it to this

   group by classid,works.empid

like that you will count courseid in classid .

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top