RANK()
RANK() is a SQL function in the Window Functions category. Similar to ROW_NUMBER but gives the same rank to equal values. Leaves gaps in ranking. The syntax is RANK() OVER (PARTITION BY column ORDER BY column). It accepts PARTITION BY column ORDER BY column. It returns rank integer (with gaps). A typical example: SELECT score, RANK() OVER (ORDER BY score DESC) FROM exam_results; -- Scores: 100, 95, 95, 90 -- Ranks: 1, 2, 2, 4 (gap at 3) A close relative is ROW_NUMBER(), which assigns a unique sequential integer to each row within its partition (1, 2, 3...). A close relative is DENSE_RANK(), which like RANK but without gaps. Equal values get the same rank, and the next number follows sequentially. A close relative is LAG(), which accesses data from a previous row within the same result set (useful for comparing values). More about this category: Window/analytical functions — ROW_NUMBER, RANK, DENSE_RANK, LAG, LEAD, NTILE.