Skip to content
IGCSE·Tuition
ICT · Lessons

Sort records with a secondary key

A report can contain exactly the right records and still lose marks because they sit in the wrong order.

On this page
  1. How does a two-key sort work?
  2. Worked example
  3. The mistake to watch for
  4. Check yourself
  5. Where this leads next

Sorting with a secondary key means ordering records by one field and then, where two records tie, by a second field. Tasks usually say “sort by club, then by attendance with the highest first”.

This lesson sits in database queries and reports and follows applying multiple criteria.

How does a two-key sort work?

The software sorts by the primary key first. It then reorders only the records that share the same primary value, using the secondary key.

  1. Underline each sort instruction in the task and note ascending or descending for each.
  2. Put the keys in the order the task lists them: first mentioned is primary.
  3. Set the direction for each key separately.
  4. Check the result by reading down the primary column, then the secondary column inside each group.

Worked example

Using the invented Harbour Hill Clubs table from the module page.

Task: sort by Club in A to Z order, then by Sessions with the highest first.

Step 1, keys: primary Club ascending, secondary Sessions descending.

Step 2, groups: Chess, Drama, Robotics in alphabetical order.

Step 3, inside Chess: Imran (12), Dewi (11), Hui (10), Aiman (8).

Step 4, inside Drama: Bella (12), Chen (9), Gopal (6).

Step 5, inside Robotics: Farah (12), Evan (7), Jia (5).

The full order is therefore Imran, Dewi, Hui, Aiman, Bella, Chen, Gopal, Farah, Evan, Jia.

The mistake to watch for

Putting the keys the wrong way round, so Sessions becomes the primary key:

Sessions descending, then Club ascending.

The list starts Bella (12), Farah (12), Imran (12), then Dewi (11) and so on, mixing clubs together.

The records are all there, but the club groups have been scrambled. The task wanted groups first.

The correction is to place Club first and Sessions second. A quick test is to read the Club column: it should change only three times.

Check yourself

1. Sort by Year ascending, then Name A to Z. Write the order.

Show answer

Year 9: Dewi, Gopal, Jia. Year 10: Aiman, Chen, Farah, Imran. Year 11: Bella, Evan, Hui. Dewi, Gopal, Jia, Aiman, Chen, Farah, Imran, Bella, Evan, Hui.

2. Sort by Fee descending, then Name A to Z. Which three records come first?

Show answer

Fee 60 members are Evan, Farah and Jia, in A to Z order. Evan, Farah, Jia. Next would be the RM45 group: Bella, Chen, Gopal.

3. A student stores ID as text and sorts ascending. In what order do IDs 1 to 10 appear?

Show answer

Text sorts character by character, so “10” comes straight after “1”. The order is 1, 10, 2, 3, 4, 5, 6, 7, 8, 9. Fix it by storing ID as a number.

Where this leads next

Next, create a calculated field where supported so a report can show a value worked out from existing fields. The read-only SQL practice lab lets you see the order a query returns, and the practice set mixes all the skills.

Order mistakes are easy to repeat without noticing, which is where online one-to-one ICT tuition can help, because a teacher can watch your settings as you choose them.

Questions people ask

What is a secondary sort key?

It is the field used to order records that tie on the first field. The primary key does the main ordering. The secondary key only changes the order inside groups that have the same primary value, such as members of the same club.

Why did my numbers sort as 1, 10, 2, 3?

The field was stored as text, so the software sorted character by character. The text '10' starts with 1, which comes before 2. Store numbers as a number data type, or follow your task's instruction on field types before sorting.

Does ascending mean smallest first?

Yes. Ascending runs from smallest to largest, or A to Z for text. Descending runs from largest to smallest, or Z to A. Check the wording in the task, because 'highest attendance first' means descending on that field.

Updated:

Your next step

If sorted results sometimes come out in an order you did not expect, a one-to-one teacher can trace which key the software applied first and why.

Paid one-hour trial at your assigned teacher’s confirmed rate, starting from RM80. You agree the teacher’s hourly rate before the trial, and ongoing lessons continue at that same rate. The schedule is arranged with your teacher after the trial.

Tuition is arranged with a parent or guardian. Send them this page on WhatsApp and they can enquire for you.

Parent or guardian? Enquire here

9,000+ students helped through our service