Sorting means arranging records in a particular order, such as alphabetical order, numerical order, or date order. In MS Access, you can sort query results in ascending or descending order.
Sorting organizes records according to the values in one or more fields. For example, you can arrange student names from A to Z or fees from highest to lowest.
| StudentID | StudentName | Course | Fees |
|---|---|---|---|
| 103 | Rahul | ADCA | 8000 |
| 101 | Amit | ADCA | 10000 |
| 104 | Neha | Web Development | 12000 |
| 102 | Priya | Tally Prime | 4000 |
Sorting these records by StudentName in ascending order would display Amit, Neha, Priya, and Rahul.
Ascending order arranges values from smallest to largest or alphabetically from A to Z.
Descending order arranges values from largest to smallest or alphabetically from Z to A.
ASC for ascending order and
DESC for descending order.
In MS Access SQL, the ORDER BY clause
specifies how the query results should be sorted.
SELECT *
FROM Students
ORDER BY StudentName ASC;
This query displays students alphabetically by name.
SELECT StudentName, Fees
FROM Students
ORDER BY Fees DESC;
This query displays students with the highest fees first.
SELECT StudentName, AdmissionDate
FROM Students
ORDER BY AdmissionDate ASC;
This query displays students from the earliest admission date to the latest.
Follow these steps to sort records in Design View:
qryStudentsSortedByName.| Field | Sort | Show |
|---|---|---|
| StudentName | Ascending | Yes |
| Course | (Not set) | Yes |
| Fees | (Not set) | Yes |
The query sorts by StudentName while displaying the other selected fields.
You can sort records using more than one field. Access applies the primary sort first and uses the next sort field to order records that have equal primary values.
SELECT StudentName, Course, Fees
FROM Students
ORDER BY Course ASC, StudentName ASC;
The query arranges courses alphabetically. Within each course, student names are also arranged alphabetically.
SELECT StudentName, Course, Fees
FROM Students
ORDER BY Course ASC, Fees DESC;
This query groups the output order by course name and places students with higher fees first within each course.
You can combine sorting with filtering criteria. The WHERE clause filters records, while ORDER BY arranges the remaining results.
SELECT StudentName, Course, Fees
FROM Students
WHERE Course = "ADCA"
ORDER BY StudentName ASC;
This query displays only ADCA students and sorts them alphabetically by name.
SELECT StudentName, Fees
FROM Students
WHERE Fees > 5000
ORDER BY Fees DESC;
This query displays students whose fees exceed 5000, starting with the highest fee.
WHERE comes before ORDER BY
in the SQL statement.
A field may contain Null values, meaning the value is missing or unknown. When you sort a query, Access places Null values according to its sorting rules and the field type.
SELECT StudentName, AdmissionDate
FROM Students
ORDER BY AdmissionDate ASC;
If some students do not have an AdmissionDate, their records may appear together at one end of the sorted results. The exact position depends on the sort order and Access behavior.
SELECT StudentName, AdmissionDate
FROM Students
WHERE AdmissionDate IS NOT NULL
ORDER BY AdmissionDate ASC;
This query excludes records without an admission date and sorts the remaining records from oldest to newest.
Sorting changes the order of the query results. It does not automatically change the order in which records are stored in the underlying table.
Create a Students table with StudentID, StudentName, Course, Fees, and AdmissionDate fields. Add at least ten sample records.
Goal: Learn to sort query results using Design View and SQL View.
1. Which SQL clause is used to sort query results?
2. Which keyword sorts fees from highest to lowest?
3. Does sorting query results automatically change the stored table order?