Sorting Records Based on Type Record Sorting and Null Fields When some values are not available, a table, a query, a form, or a report may display empty fields. These are also referred to as null fields or null values. In Lesson 22, we saw what a null or an empty field is. When sorting records in alphabetical, incremental, or chronological order, the empty fields always come first. When sorting records in reverse alphabetical, decrementing, or reverse chronological order, the empty fields will always come last in the result. On the other hand, if you sort the records in descending order, the non-null records would come first. Practical Learning: Sorting Records
Sorting Number-Based Fields As you may know already, the SQL supports various types of numeric values. The columns that use those values can be sorted in incremental order. To sort records based on a number-based column:
In the same way, you can sort the records in the reverse order. Of course, to sort the records in decrementing order, apply the DESC keyword after the name of the column. Practical Learning: Sorting Number-Based Fields
Sorting Boolean Fields Boolean fields are those that use False and True values. To sort records based on a Boolean field:
In the same way, you can sort the records in the reverse order. Of course, to sort the records in decrementing order, apply the DESC keyword after the name of the column. Practical Learning: Sorting Boolean Fields
|
Sorting More Than One Column Introduction Imagine you arrange records based on a certain column that has repeating values: ELECT Region,
StateName,
Capital,
AreaSqrMiles,
AdmissionUnionOrder
FROM States
ORDER BY Region;
If you get a situation where many records on a column have the same value, you can specify an additional column by which to sort the records. To arrange the list using more than one column using the SQL, after ORDER BY, type the columns separated by commas. Here is an example: SELECT Region,
StateName,
Capital,
AreaSqrMiles,
AdmissionUnionOrder
FROM States
ORDER BY Region, StateName;
Practical Learning: Sorting More Than One Column
Sorting Null and Non-Null Fields If you specify more than one record to sort by, the database engine sorts the primary column first. Then, on the second field, when two records have the same value, the NULL values would come first. Sorting Many Number-Based Fields If you have two integer-based fields that have repeating values, if you sort the list based on the first field, the records with similar values in the first field would be grouped. If the records are sorted in the second field, they would be sorted for each group of similar first field records. Options on Sorting Records Sorting by an Expression When sorting the records, the database engine mostly needs to have a value as reference, the value by which to arrange the records. Based on this, besides, or inside of, (a) column(s), you can use an expression to sort the records. Practical Learning: Sorting by an Expression
Using a Function to Sort Records Just as you use an expression as a basis for sorting records, you can use the return value of a function to arrange records. Practical Learning: Sorting Using a Function
|