0

Hi i have the following table structure.

Professor (EMP ID,Name,Status,Salary,Age)
Course(Course ID,Course Name,Points)
Works(Course ID,EMP ID,Class ID)

I need to do the following.

Return List of Employees who have taken 2 different course M1 and M2 for the same class ‘class 10’

This is the query that i have written.

SELECT p.EmpID, p.Name, p.Status, p.Salary 
FROM professor p, course c, works w 
WHERE p.EmpID = w.EmpID
AND
w.CourseID = c.CourseID
AND
w.ClassID = 10
AND
c.CourseName IN ( SELECT CourseName FROM course WHERE CourseName = 'm1'
AND CourseName = 'm2')

But the query doesnot return any values even though there are data in the db.

1
  • is coursename nullable? Commented Mar 8, 2013 at 6:09

2 Answers 2

2

This problem is commonly called Relational Division

SELECT  a.EmpID, a.name
FROM    Professor a
        INNER JOIN Works b
            ON a.EmpID = b.EmpID AND b.ClassID = 10 
        INNER JOIN  Course c
            ON b.CourseID = c.CourseID
WHERE   c.CourseNAME IN ('M1', 'M2')
GROUP   BY a.EmpID, a.name
HAVING  COUNT(DISTINCT c.CourseNAME) = 2
Sign up to request clarification or add additional context in comments.

2 Comments

i am getting this error when i execute your query - Column 'EmpID' in on clause is ambiguous
oops my bad sorry for the typo, try again. a.EmpID = EmpID should be a.EmpID = b.EmpID
1

The subquery

( SELECT CourseName FROM course WHERE CourseName = 'm1' AND CourseName = 'm2')

will return nothing. Look at the "AND"

2 Comments

CourseName = 'm1' AND CourseName = 'm2' will ALWAYS return FALSE
Exactly this is my point. Hence the IN ... will never find anything and hence the whole query will return nothing

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.