Study Vault Search notes, aliases and full text
Graph
ConceptDBISAI-written · not yet proofread

Ordering

Also ORDER BY

Ordering (ORDER BY) turns the unordered multiset of result rows into a sequence sorted by one or more expressions, ascending or descending, on which LIMIT and OFFSET select a segment.

Properties#

  • Clause order: GROUP BY → ORDER BY → LIMIT / OFFSET; in the logical evaluation order, ordering comes after SELECT (so it may use result aliases such as total_views) and before LIMIT (SQL Query Structure).
  • Result is a sequence, not a table: the output of an ordering is no longer a multiset. That is why it belongs at the end of a query; without ORDER BY the row order of a result is unspecified, and “the order of rows is not specified” is a valid remark in exam solutions.
  • Ties: rows with equal sort keys may come in any order; add further keys to make the order unique.
  • Top-kk queries: ORDER BY … DESC LIMIT k gives the kk rows with the largest key; per-group maxima need Grouping instead.
  • SPARQL: the same clauses exist, but the direction is written like a function of the key: ORDER BY DESC(?date) instead of SQL’s ORDER BY date DESC (DBIS_VL07 > PREFIX, FILTER, ORDER BY, LIMIT).

Example#

Sources#

Search

Type In
↑ ↓ move ↵ open ⇥ next section Ctrl↵ results page Prefix # to search text only