Back to Selecting Data

In Memory Tables · Selecting Data · Practice Question

Select a Range of Rows, Ordered by a Column

Sign in to track your progress

Question

Given a table t with a column price, select all rows from index 2 to index 10, ordered by price in ascending order.

For example:

t:([] sym:15?`A`B`C; price:15?100.0)

Solution

select [2,10;<price] from t

Explanation

The square brackets after select are qSQL's row-limit clause, and they accept more than just a simple count.

2,10 is a range — start at index 2, take up to 10 rows — rather than the single number (-3, for example) used to take rows from the start or end of a table.

<price is a sort specification: < requests ascending order on price before the range is taken. The row range is applied after sorting, so [2,10;<price] means "sort the whole table by price ascending, then take rows 2 through 11 of that sorted order" — combining filtering by position with ordering in a single clause, without needing a separate xasc step beforehand.