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 afterSELECT(so it may use result aliases such astotal_views) and beforeLIMIT(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 BYthe 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- queries:
ORDER BY … DESC LIMIT kgives the 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’sORDER BY date DESC(DBIS_VL07 > PREFIX, FILTER, ORDER BY, LIMIT).
Example#
Sources#
- Slides 05, p. 31 (ORDER BY), p. 32 (LIMIT and OFFSET)
- Slides 03, p. 10 (rows have no order)
- DBIS_VL05 > ORDER BY, DBIS_VL05 > LIMIT and OFFSET, DBIS_VL03 > Sets vs. multisets
- Exams: DBIS Exam WS2324 (not published) (Tasks 2.1, 3.3), DBIS Exam WS2324-2 (not published) (Task 3.3)