Given the following data:
Course_ID | Teacher_ID | Minimum_grade | Maximum_grade |
---|---|---|---|
1 | 1 | 7 | 8 |
2 | 2 | 5 | 6 |
3 | 2 | 5 | 7 |
4 | 3 | 5 | 8 |
5 | 3 | 6 | 7 |
6 | 4 | 5 | 6 |
7 | 4 | 4 | 6 |
8 | 4 | 5 | 7 |
using Postgresql, I need to write a query to:
select all teachers who taught 2 separate courses X and Y where
the difference between the minimum grade of course X and maximum grade
of course Y is greater than 2.
Thus, only teacher 4 should be selected.
I tried the following:
SELECT Teacher_ID, MAX(Maximum_grade), MIN(Minimum_grade)
FROM dataset
GROUP BY Teacher_ID
HAVING count(Teacher_ID) > 1 AND (MAX(Maximum_grade) - Min(Minimum_grade)) > 2;
This selects teachers 3 and 4 which should not be the case.
I assume this is because the code I wrote compares the minimum grade of course 4 with the maximum grade of the same course, which should not happen.
I have no idea how to improve my code to obtain the desired results, any help would be much appreciated.
5
Answers
result = TEACHER_ID 3 , DIFF 3 , NUMBER_OF_COURSE 2
USING PYTHON
DF:
Code:
Output:
I would solve this by selecting from your table twice, once with an eye to find the minimum grade and the second time to find the maximum. When we join those together we want to do so for teachers who share their id but making sure that the classes have different ids:
You might try it here, though I don’t know how long this sites keep these fiddles around: http://sqlfiddle.com/#!9/1c4a72/1
This query can still be optimised (or) converted into sub-queries. This is from the top my head. This consists of two parts, first we need to satisfy the two conditions,
The above query fetches the records for the above condition, with just a little contradiction. The contradiction is, sometimes the difference b/w is calculated within the same course, ie
To correct this, I’m just selecting records where max(max_grade) and min(min_grade) are from different rows( hence that courses will also be different).
Edit:
@JonSG has converted the CTE to subquery, fiddle link: http://sqlfiddle.com/#!9/1c4a72/9
. Thanks JonSG
You can join the dataset with itself intersecting on same teacher with different courses and get the maximum differences per teacher from this cartesian product:
Because of the join’s condition requiring different courses, teachers with only one course will not come out and differences of grades within the same course will be excluded