There is a database of tables: students, groups and statements. The last are the marks for the exams. It is necessary to somehow derive all possible pairs of students in which the first grade point has a higher average score than the second one, and sort it all into groups. It seems to me that it is required to form a large table with all possible combinations of students and their scores, and then just make a sample of it based on avg (1)> avg (2). But is it possible to sort through all combinations of students, and if so, how? Thank you in advance..
upd1. In the table of students, just a series of names: Petrov, Guskov, Ivanov, and the names of the groups in which they study.
In the table of statements lists the estimates for the items. 
The final goal is to get a table of 4 columns: Last name of 1 student, his average score, last name of the 2nd student, and his average score, while the average score of the first must be greater than the average score of the second

A.id!=B.idhere and get all the pairs except connecting yourself with yourself - Mike