0

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.

1
  • You should read about explicit joins, GROUP BY and aggregates. Commented Mar 8, 2013 at 11:48

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

2 Comments

This is for which question i have 2
@user1844638 Can't you at very least test it and see for yourself?
1

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

Comments

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.