Sorting Data in Databases
Sorting is a crucial function in databases. It helps organise and present data in a meaningful way.
Using a Single Criterion to Sort Data
- You can sort data based on a single criterion - such as by name, date, or numerical value
- For example, you might sort a list of students in ascending order by their last names
- To sort the customer's tables by
LastName
in either ascending or descending order:
- Open the table in Datasheet View
- Click on the column header for the field to be sorted. For example, a table of customers to be sorted by
LastName
, click on theLastName
column header - Click on the "Sort Ascending" or "Sort Descending" button in the toolbar at the top of the screen
Using Multiple Criteria to Sort Data
- You can also sort data based on multiple criteria
- For instance, you might want to sort a list of products first by category (ascending), and within each category, by price (descending)
- To sort the customer's table first by
City
, and then byLastName
within each city:- Open the table in Datasheet View
- Click on the column header for the first field to be sorted. For example, sorting by
City
and then byLastName
within each city, first, click on theCity
column header - Click on the "Sort Ascending" or "Sort Descending" button in the toolbar
- Next, hold down the Shift key and click on the column header for the second field to be sorted by (
LastName
in this example) - While still holding down the Shift key, click on the "Sort Ascending" or "Sort Descending" button again
Ascending and Descending Order
- Ascending Order - Data is sorted from smallest to largest (e.g., from A to Z, or from 1 to 100)
- Descending Order - Data is sorted from largest to smallest (e.g., from Z to A, or from 100 to 1)
Exam Tip
- Remember, when sorting by multiple criteria, the data is first sorted by the first criterion. Within each group of the first criterion, it is sorted by the second criterion, and so on