MySQL error 1241: Operand should contain 1 column, INTERSECT two subqueries

database-designMySQL

I'm running this query in MYSQL and I'm getting the error 1241: Operand should contain 1 column(s).

I want to know the names and student id's of the students enrolled in Database Management Systems and Knowledge Discovery in Databases (both course titles in the CoursesOffer table)

  • Students(stuID: String, stuName: String, gender: String, birthdate: Date, enterYear: Year, gpa: Float)
  • Departments(deptName: String, numPhDs: Integer)
  • ProfessorWorks(profID: String, profName: String, deptName: String)
  • CoursesOffer(cNo: String, cTitle: String, deptName: String)
  • Majors(deptName: String, stuID: String, degreeProgram: String, attendYear: Year, attendSemester: String)
  • Sections(cNo: String, semester: String, sectNo: String, profID: String)
  • Enrolls(stuID: String, cNo: String, sectNo: String, semester: String, grade: String)

My query looks like

SELECT S.StuName, S.StuID 
FROM Students S NATURAL JOIN CoursesOffer C NATURAL JOIN Enrolls E 
WHERE cTitle='Database Management Systems'
IN (
SELECT S2.StuName, S2.StuID 
FROM Students S2 NATURAL JOIN CoursesOffer C2 NATURAL JOIN Enrolls E2 
WHERE cTitle='Knowledge Discovery in Databases');

Best Answer

You probably want to replace:

WHERE cTitle='Database Management Systems'
IN (
SELECT S2.StuName, S2.StuID 
...
  ) ;

with:

WHERE cTitle='Database Management Systems'
  AND (S.StuName, S.StuID) IN (                -- this changed
SELECT S2.StuName, S2.StuID 
...
  ) ;