ORDER BY¶
Description¶
The ORDER BY clause sorts query results by a specified column in ascending (ASC) or descending (DESC) order.
Syntax¶
(See the Syntax Notation Guide for notation rules).
Basic syntax¶
Full syntax¶
Parameters¶
| Parameter | Description |
|---|---|
<column> |
The name of the column by which to sort results. |
ASC |
Sorts in ascending order (smallest to largest / alphabetical). This is the default. |
DESC |
Sorts in descending order (largest to smallest / reverse alphabetical). |
Type-Aware Sorting Behavior¶
Sorting respects the underlying data type of the column:
INTColumns: Numerically sorted by parsed integer value (std::stoi).FLOATColumns: Numerically sorted by parsed float value (std::stof).STRINGColumns: Lexicographically sorted by character code points.BOOLColumns:falsesorts beforetruein ascending order.
Examples¶
1. Default Ascending Sort (students)¶
2. Explicit Descending Sort (students)¶
3. Combined with WHERE and LIMIT (students)¶
Errors & Edge Cases¶
| Error Condition | Error Message |
|---|---|
| Unknown Column in ORDER BY | Unknown column '<col>' in ORDER BY |