SQL Order-By Expressions

An SQL Order-By expression is a comma-delimited list of Value Expressions, where each expression can be followed by ASC or DESC to specify ascending or descending order, respectively. Query results will be sorted by the order of the expressions in the Order-By expression.

All Order-By expressions (including the default, empty, Order-By expression) have rowHash() appended to them, so as to provide a stable sort order at all times.

The Order-By expressions support ordering with complex types (e.g. row) using lexicographical ordering. Consider this fictive dataset docs containing the list of programming languages mentioned in a given set of documents:

rowName terms
doc1 "c++,python,c++,java,c++"
doc2 "scala,scala,java,java,scala,java,scala,c++"
doc3 "python,ada,ada"

The rows returned by this query

SELECT tokenize(terms) as term FROM docs ORDER BY tokenize(terms) 

are lexicographically ordered using the ordered column's names (i.e. the programming language name) and the column's values.


More details about the ordering of complex types can be found in MLDB Type System.

Should the Order-By expression return values of varying types, in ascending order they will be sorted in this fashion: First null values, then atomic values, then row values, and finally embedding values.