Cambridge IGCSE ICT 0417 – Databases: Sorting Data
18 Databases
Objective
Be able to use a single criterion, or multiple criteria to sort data into ascending or descending order.
What is Sorting?
Sorting arranges the records in a table so that they appear in a particular order. The order can be:
Ascending – from smallest to largest, A to Z, earliest to latest.
Descending – from largest to smallest, Z to A, latest to earliest.
Why Sort Data?
Find information quickly (e.g., the highest sales figure).
Prepare data for printing or reporting.
Identify duplicates or out‑of‑range values.
Make it easier to compare records.
Sorting by a Single Criterion
A single criterion means you choose one field (column) to determine the order.
Select the table or query you wish to sort.
Locate the Sort option (often in the toolbar or right‑click menu).
Choose the field you want to sort by.
Select Ascending or Descending.
Apply the sort – the records are now displayed in the chosen order.
Example – Single‑Field Sort
Consider the following table of student scores:
Student ID
Name
Score
102
Alice
78
215
Bob
85
317
Charlie
62
428
Diana
91
Sorting by Score in Descending order gives:
Student ID
Name
Score
428
Diana
91
215
Bob
85
102
Alice
78
317
Charlie
62
Sorting by Multiple Criteria
When two or more fields are used, the database sorts by the first field, then within each group it sorts by the second field, and so on.
Open the Sort dialog (or use the query design grid).
Enter the first field in the Sort By column and choose the direction.
Enter the second field in the next row and choose its direction.
Continue for additional fields if required.
Run the sort – records are ordered by the hierarchy of criteria.
Example – Two‑Field Sort
Suppose we have a product inventory table:
Category
Product Name
Price ($)
Stock
Electronics
Headphones
45
120
Electronics
Smartphone
299
45
Furniture
Desk
150
30
Furniture
Chair
85
80
Electronics
Tablet
199
60
Furniture
Bookshelf
120
25
Sort first by Category (Ascending) and then by Price (Descending). The result:
Category
Product Name
Price ($)
Stock
Electronics
Smartphone
299
45
Electronics
Tablet
199
60
Electronics
Headphones
45
120
Furniture
Desk
150
30
Furniture
Bookshelf
120
25
Furniture
Chair
85
80
Key Points to Remember
Ascending order is the default for alphabetical and numeric fields.
When sorting dates, the earliest date is first in ascending order.
Multiple‑criteria sorting is useful for hierarchical data (e.g., Country → City → Postal Code).
Sorting does not change the underlying data; it only changes the view unless you save the sorted query as a new table.
Be aware of data types – mixing text and numbers in the same field can give unexpected results.
Common Pitfalls
Sorting a text field that contains numbers will sort alphabetically (e.g., “10” comes before “2”). Convert the field to a numeric type if numeric sorting is required.
Leading spaces in text fields affect order – clean data before sorting.
Sorting on a field that contains null (blank) values may place those records at the top or bottom depending on the software.
Practice Activities
Open the sample “Employees” table. Sort the records by Last Name in ascending order. Note the first and last records.
Using the same table, sort by Department (Ascending) and then by Salary (Descending). List the top three salaries in each department.
Create a query that shows all orders from the “Orders” table, sorted by Order Date (Descending) and then by Customer ID (Ascending). Export the result to a CS \cdot file.
Explain why sorting the “Product Code” field (stored as text) gave an unexpected order, and describe how to correct it.
Suggested diagram: Flowchart showing the steps to apply a single‑criterion sort and a multiple‑criterion sort in a typical database program.