Skip to content
Advertisement

Symmetric Pair in SQL with JOIN

I am trying to find the name of students where a symmetric pair exists. There are 3 tables:

Edit from your comment: A student is called as being a part of a symmetric pair if the marks obtained by that student in science is equal to the marks obtained by some other student in mathematics and the marks obtained in mathematics are the same as marks obtained by the other student in science.

Advertisement

Answer

Given the structure of the data, I would assume that students could have multiple marks in each subject. Otherwise, why store the values in separate tables?

To solve this problem, I would preaggregate the marks:

This returns the matching ids. You need an additional join if you want to bring in the names.

Note: You have stored the values as floats. This is quite dangerous. You should be storing the values as decimal/numeric. Two values that look the same might actually be different.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement