window

RANK

Equal scores share a position; subsequent positions skip the number of tied rows. Do not add id to the window ordering if ties should remain peers. Example target: PostgreSQL 17; focused usage form.

Formal Syntax & Structure

sql syntaxsql
RANK() OVER (PARTITION BY g ORDER BY x)

Working Example

Example Snippetsql
WITH t(id,g,x) AS (VALUES (1,'A',10),(2,'A',20),(3,'A',20),(4,'A',40),(5,'B',7)) SELECT id, RANK() OVER (PARTITION BY g ORDER BY x) AS result FROM t ORDER BY id;
SQL Reference