title: MYSQL query to show all students which have common courses in table
body: <p>First of all I have this table structure and data as below:
  <img src="https://i.stack.imgur.com/1xxyM.png" alt="enter image description here"></p>
  
  <p>I would like to return all the other students for each common course they have for a specific student.</p>
  
  <p>and I am using this query:</p>
  
  <pre><code>SELECT  DISTINCT students.name AS StudentName, students.id as StudentId, courses.id AS   
  CourseId,courses.name AS CourseName, universities.id AS University FROM partners
  INNER JOIN students ON students.id = partners.student_id
  INNER JOIN courses ON courses.id = partners.course_id
  INNER JOIN universities ON universities.id = partners.university_id
  WHERE students.id != '1' AND partners.course_id IN('6','2','1')
  GROUP BY students.name
  ORDER BY date
  </code></pre>
  
  <p>It returns me correct result but the thing is it does not seem to be very effecient especially I am wondering How would I pass on the real app the data in  IN() statement.</p>
  
  <p>I hope I am clear.</p>
  
  <p>Thanks</p>