I have a one to many relationship mapping table in postgres and the tables are as below:
Students
| id | Name |
|---|---|
| 1 | Shibbir |
| 2 | Ahmed |
| 3 | John |
Courses
| id | Name |
|---|---|
| 1 | Data Structure |
| 2 | Algorithms |
| 3 | Math |
student_course_mapping
| studentId | courseId |
|---|---|
| 1 | 1 |
| 1 | 2 |
| 1 | 3 |
| 2 | 2 |
| 2 | 3 |
| 3 | 1 |
| 3 | 2 |
Given that a student must have unique course combinations: if student 1 have the course 1,2,3 then none of the other students can have same course combinations, thus student 2 has to to take course combination 2,3 and then finally student 3 has to take course combination 1,2.
Finally my question is: if I have a combination of course ids, then how to write the query to get the unique student id from student_course_mapping table who has exactly the same combination of courses.
example : input course ids: 1,2,3 then output should be 1 input course ids: 2,3 then output should be 2 input course ids: 1,2 then output should be 3
It would be great to be able to write a single query that gives the desired result.