patternsqlMinor
What am I doing wrong with subquery
Viewed 0 times
whatwithdoingsubquerywrong
Problem
I cannot work this out, how do I get the subquery to return the lowest course weight for each lecturer, it currently only returns the lowest and is my subquery wrong or outer query?
Question: Show each lecturer’s lowest coursework weighting, displaying the Staff ID, the Module ID and the weighting selected.
Question: Show each lecturer’s lowest coursework weighting, displaying the Staff ID, the Module ID and the weighting selected.
SELECT m1.ModuleID, m1.CWWeight, l.StaffID
FROM dbo.module as m1
INNER JOIN dbo.Lecturer as l
ON m1.ModuleConvenor = l.StaffID
WHERE m1.CWWeight = (SELECT MIN(m2.CWWeight)
FROM dbo.Module as m2)Solution
There are many many ways to do this. Here is one that only slightly changes your attempt:
SELECT m1.ModuleID, m1.CWWeight, l.StaffID
FROM dbo.module as m1
INNER JOIN dbo.Lecturer as l
ON m1.ModuleConvenor = l.StaffID
WHERE m1.CWWeight = (SELECT MIN(m2.CWWeight)
FROM dbo.Module as m2
WHERE m2.ModuleConvenor = l.StaffID) ;Code Snippets
SELECT m1.ModuleID, m1.CWWeight, l.StaffID
FROM dbo.module as m1
INNER JOIN dbo.Lecturer as l
ON m1.ModuleConvenor = l.StaffID
WHERE m1.CWWeight = (SELECT MIN(m2.CWWeight)
FROM dbo.Module as m2
WHERE m2.ModuleConvenor = l.StaffID) ;Context
StackExchange Database Administrators Q#239400, answer score: 5
Revisions (0)
No revisions yet.