ROW_NUMBER() OVER ...

All functions > WINDOW FUNCTION > ROW_NUMBER() OVER ...

Returns a unique sequential number for each row within its partition, starting at 1.

Syntax

ROW_NUMBER() [WITHIN (WHERE condition)] OVER ([PARTITION BY expr [, ...]] [ORDER BY sort_item [, ...]] [ROWS|RANGE|GROUPS frame])

Notes

  • Assigns a unique sequential number to each row within a partition
  • Numbers start at 1 for the first row in each partition
  • Unlike RANK, ROW_NUMBER always assigns unique numbers (no ties)
  • ORDER BY determines the sequence of row numbers
  • PARTITION BY creates independent numbering groups
  • Useful for pagination, deduplication, and selecting top N rows per group
  • Always returns consecutive numbers without gaps

See also

Examples

FeatureQL
SELECT
    -- 1-based position in ORDER BY order, not id (100, 200, 300) and not v
    f1 := ZIP(ARRAY(100, 200, 300) AS id, ARRAY('a', 'b', 'c') AS v).TRANSFORM(
        SELECT ROW_NUMBER() OVER (ORDER BY id ASC)
    ).UNWRAP(),
    -- Row numbers only matching rows; rows outside WITHIN return NULL
    f2 := ZIP(
        ARRAY(1, 2, 3, 4) AS id,
        ARRAY(20, 10, 20, 40) AS s,
        ARRAY(TRUE, FALSE, TRUE, TRUE) AS keep
    ).TRANSFORM(
        SELECT ROW_NUMBER() WITHIN (WHERE keep) OVER (ORDER BY s ASC, id ASC)
    ).UNWRAP()
;
Result
f1 ARRAYf2 ARRAY
[1, 2, 3][1, null, 2, 3]