I have 2 tables:
T1 {
id: string;
school: string
}
and
T2 {
school_id: string;
student_id: string;
tuition_fee: double
}
The 2 tables are associated by T1.id and T2.school_id. T1 contains information of each school. It's relatively small. T2 contains information of the tuition fee for each student. It can be huge (millions of records).
Edit:
Now, I want to find out which 3 schools collect most tuition fees from its students. Assume that it's impossible that 2 schools collect the same amount of total tuition fees from all its students. And my solution is as follow:
- Group records in T2 by school_id;
- Compute sum of tuition_fee in each group;
- Sort the groups by the sum in descending order;
- Get the top 3.
My questions are:
- Any better way to do this?
- What's the most efficient SQL to do this?
- Any other consideration to take into account to improve the speed? e.g. using indexing, view, etc.