On this page
  1. RANK
  2. Syntax & Overview
  3. Notes & Best Practices
  4. Example

RANK

Prev: `ROW NUMBER` | Up: functions | Next: `LAG`
Rel: functions, Aggregations, CTEs & Window Functions
Ref: PostgreSQL — SQL Commands
Tags: #public #databases #sql #functions #tier/standard


Syntax & Overview

RANK() OVER (ORDER BY score DESC)  /* + DENSE_RANK() */

Assigns ranks with ties (RANK leaves gaps 1, 1, 3; DENSE_RANK does not 1, 1, 2).

Notes & Best Practices

Example

SELECT name, score,
       RANK() OVER (ORDER BY score DESC) AS rnk
FROM players;

Linked from

Related notes