James Taylor James Taylor - 1 month ago 16
SQL Question

Double Nested Select

Im tring to get this query to work

SELECT studentid, fName, lName, dob
FROM Student
WHERE courseNo IN
(SELECT courseNo
FROM Student
WHERE courseNo = 1001 IN
(SELECT dob
FROM Student
WHERE dob < `1985-09-12`));


But for some reason, it is saying empty set?

All the data is in the same table of Student. If it helps, dob is formatted YYYY-MM-DD.

Answer

There is an attribute missing between WHERE courseNo = 1001, and IN. I am not sure about exactly you need, but maybe could be like this?:

SELECT studentid, fName, lName, dob FROM Student WHERE courseNo IN (SELECT courseNo FROM Student WHERE courseNo = 1001 AND dob IN (SELECT dob FROM Student WHERE dob < '1985-09-12'));

And if so, make the query simpler it is suited for you? Try something like this:

SELECT studentid, fName, lName, dob FROM Student WHERE courseNo = 1001 and dob < '1985-09-12';

Comments