Menu

SQLite Window Functions: ROW_NUMBER, RANK, LAG and More

SQLite window functions calculate values across related rows without collapsing those rows into a single result per group. Use them for rankings, previous or next values, and totals alongside individual records. Unlike a GROUP BY aggregate, a window aggregate keeps one output row for each input row.

Window function syntax

window_function(arguments) OVER (
  PARTITION BY group_column
  ORDER BY sort_column
)
  • OVER identifies the window used by the function.
  • PARTITION BY splits rows into independent groups. Omit it to use the entire result set.
  • ORDER BY controls the order within each partition, which matters for ranking and offset functions.
  • Some functions also accept a window frame such as ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW. The default frame depends on the window specification; specify it explicitly when calculating running aggregates.

Example: rank rows and show group totals

WITH sales(team, amount) AS (
  VALUES ('A', 10), ('A', 20), ('B', 15)
)
SELECT team,
       amount,
       ROW_NUMBER() OVER (
         PARTITION BY team ORDER BY amount DESC
       ) AS row_num,
       SUM(amount) OVER (PARTITION BY team) AS team_total
FROM sales
ORDER BY team, amount DESC;
team amount row_num team_total
A 20 1 30
A 10 2 30
B 15 1 15

ROW_NUMBER() restarts at 1 for each team. SUM(...) OVER (...) repeats the team total beside each sale instead of returning only one row per team.

Choose a window function

  • ROW_NUMBER() assigns a unique sequential position within a partition.
  • RANK() gives tied rows the same rank and leaves gaps after ties.
  • DENSE_RANK() gives tied rows the same rank without gaps.
  • LAG() and LEAD() access previous and next rows.
  • FIRST_VALUE() and LAST_VALUE() read values from the window frame. For LAST_VALUE(), check the frame bounds carefully.

Window functions require SQLite 3.25.0 or later. Try the query in the SQLite Playground. For calculations that should return one row per group instead, see SQLite aggregate functions.

  1. cume_dist

    The SQLite cume_dist() function returns the cumulative distribution of the current row.
  2. dense_rank

    The SQLite dense_rank() function returns the rank within the partition in which the current row is located, starting at 1, with no gap.
  3. first_value

    The SQLite first_value() function returns the evaluated value from the first row of the window frame associated with the current row.
  4. lag

    The SQLite lag() function returns the value from the specified row before the current row in the partition where the current row is located.
  5. last_value

    The SQLite last_value() function returns the evaluated value from the last row of the window frame associated with the current row.
  6. lead

    The SQLite lead() function returns the value from the specified row after the current row in the partition where the current row is located.
  7. nth_value

    The SQLite nth_value() function returns the evaluated value from the specified row of the window frame associated with the current row.
  8. ntile

    The SQLite ntile() function divides all rows in the partition into the specified number of buckets as evenly as possible, and returns the rank of the bucket where the current row is located.
  9. percent_rank

    The SQLite percent_rank() function returns the relative rank within the partition where the current row is located, that is (rank() - 1) / (number of rows partition - 1).
  10. rank

    The SQLite rank() function returns the rank within the partition in which the current row is located, starting at 1, with gaps.
  11. row_number

    Learn SQLite ROW_NUMBER() with runnable PARTITION BY and ORDER BY examples, deterministic tie-breaking, top N per group, and its difference from RANK().