Reputation: 1073
Hi i am writing a mysql query for the following db structure.
Professor (EMP ID,Name,Status,Salary,Age)
Course(Course ID,Course Name,Points)
Works(Course ID,EMP ID,Class ID)
Assumptions:
Each course has only one instructor in each semester
Status can take values from “Full”, “Associate”, and “Assistant”
I need to do the following.
Return the names of full professors who ever taught at least two courses IN one Class
SELECT p.name
FROM professor p, works w
WHERE p.empid = w.empid
AND p.status = ‘full’
AND w.classid IN (SELECT classid
FROM works
WHERE count(courseid)>1)
Return the name(s) of the professor(s) who taught the most number of courses IN Class 10
SELECT p.names
FROM professor p, works w
WHERE p.empid =w.empid
AND w.classid IN (SELECT classid
FROM works
WHERE classid = 10
AND coursed = max(coursed))
But these queries are returning wrong results. I am new to mysql please help.
Upvotes: 0
Views: 103
Reputation: 16497
Alternative answer for the first one:
SELECT DISTINCT p.empid, p.name
FROM professor AS p
JOIN works AS w ON
p.empid = w.empid
JOIN works w2 ON
w.empid=w2.empid AND w.classid=w2.classid AND w.courseid<>w2.courseid
Upvotes: 1
Reputation: 19882
Try it like this
SELECT
p.*,
COUNT(c.Course_ID) AS TCount
FROM Professor AS P
LEFT JOIN Works AS w ON w.EMP_ID = p.EMP_ID
LEFT JOIN Course AS c ON w.Course_ID = c.Course_ID
GROUP BY p.EMP_ID , c.Course_ID
HAVING TCount > 1
Upvotes: 1