Others

Microsoft Access Query for the Top Two Subjects per Student

Chanuka GeekiyanageChanuka Geekiyanage Nov 18, 2026 868 views

Question details

Return the two highest-scoring subjects for each student and choose how to handle tied scores.

Return the Top Two Subjects per Student in Microsoft Access
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.
Before you start

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.

Solution 1Recommended

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.

1
Open SQL View

In Access, select Create > Query Design, close the Add Tables dialog if it appears, and switch to SQL View.

2
Enter the query

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;

3
Run and check the results

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.

Return at most two subjects using SubjectID as a tie-breaker
Use a unique tie-breaker: If a student can have multiple rows with the same SubjectID, use a unique score-record ID instead of SubjectID in the tie-break comparison and final ordering.
Free Microsoft Office alternative

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.

Review and sort exported student score results in a spreadsheetOpen and edit Microsoft Excel XLSX and XLS filesUse familiar spreadsheet tools for an easier transition from Microsoft OfficeWork with a free, lightweight office suite
microsoft office alternative - wps office

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.