Microsoft Access Query for the Top Two Subjects per Student
Question details
Return the two highest-scoring subjects for each student and choose how to handle tied scores.

- Product
- Microsoft Access
- Device & OS
- not provided
- Scenario
- A Scores table contains student IDs, subject IDs, and scores, and the user wants a per-student top-two query.
- Observed behavior
- A TOP 2 query can return more than two subjects for a student when scores are tied at the cutoff.
In Access, check that your table and field names match Scores, StudentID, SubjectID, and Scores; replace them in the queries if needed. For the two-row query, ensure StudentID, SubjectID, and score values are not null and that SubjectID uniquely identifies a row within each student.
Return at most two subjects using SubjectID as a tie-breaker
Use a correlated count to select two rows per student, choosing the lower SubjectID first when scores are equal.
The query counts rows for the same student that rank ahead of each row. A higher score ranks first; for equal scores, a lower SubjectID ranks first. A student with only one eligible row will return one row.
In Access, select Create > Query Design, close the Add Tables dialog if it appears, and switch to SQL View.
Replace the SQL text with: SELECT S.StudentID, S.SubjectID, S.[Scores] FROM Scores AS S WHERE S.StudentID IS NOT NULL AND S.SubjectID IS NOT NULL AND S.[Scores] IS NOT NULL AND (SELECT COUNT(*) FROM Scores AS T WHERE T.StudentID = S.StudentID AND T.SubjectID IS NOT NULL AND T.[Scores] IS NOT NULL AND (T.[Scores] > S.[Scores] OR (T.[Scores] = S.[Scores] AND T.SubjectID < S.SubjectID))) < 2 ORDER BY S.StudentID, S.[Scores] DESC, S.SubjectID ASC;
Select Run and verify that each student has no more than two rows. For tied scores, confirm that the lower SubjectID is the preferred choice.

Include all subjects tied at the top-two cutoff
Use TOP 2 when every subject tied at the cutoff should appear, even if a student has more than two results.
Review exported Access scores in WPS Spreadsheets
WPS Office is a free, lightweight Office alternative with a familiar interface. Export Access query results to an Excel workbook, then open them in WPS Spreadsheets for review and sharing. WPS does not run or fix the Access query itself.

Frequently Asked Questions
Why does TOP 2 return three subjects for one student?
Access includes ties at the TOP cutoff. If a student has scores of 90, 85, and 85, both subjects with 85 qualify alongside the subject with 90.
Does DISTINCT guarantee two subjects per student?
No. DISTINCT in the subquery selects distinct score values, not individual subject rows. Multiple subjects can still match either selected score.
How can I choose which tied subject appears?
Add a deterministic tie-breaker, such as SubjectID or a unique score-record ID. In the correlated count query, the lower tie-breaker value ranks first when scores match.
What happens if a student has fewer than two scores?
The two-row query returns the eligible rows available for that student. It does not create a second row when only one score exists.
Can I inspect Access results in WPS Spreadsheets?
Yes. Run the query in Access, export its results to an Excel workbook, and open that workbook in WPS Spreadsheets. The Access query must still be created and run in Access.




