I have three tables I want to iterate over. The tables are pretty big so I will show a small snippet of the tables. First table is Students:
| id | name | address | 
|---|---|---|
| 1 | John Smith | New York | 
| 2 | Rebeka Jens | Miami | 
| 3 | Amira Sarty | Boston | 
Second one is TakingCourse. This is the course the students are taking, so student_id is the id of the one in Students.
| id | student_id | course_id | 
|---|---|---|
| 20 | 1 | 26 | 
| 19 | 2 | 27 | 
| 18 | 3 | 28 | 
Last table is Courses. The id is the same as the course_id in the previous table. These are the courses the students are following and looks like this:
| id | type | 
|---|---|
| 26 | History | 
| 27 | Maths | 
| 28 | Science | 
I want to return a table with the location (address) and the type of courses that are taken there. So the results table should look like this:
| address | type | 
|---|
The pairs should be unique, and that is what's going wrong. I tried this:
select S.address, C.type
from   Students S, Courses C, TakingCourse TC
    where TC.course_id = C.id 
    and S.id = TC.student_id
And this does work, but the pairs are not all unique. I tried select distinct and it's still the same.
 
    