less than 1 minute read

Problem

problem-178

Query

SELECT
    s.score,
    DENSE_RANK() OVER w AS 'rank'
FROM
    Scores As s
WINDOW w AS (ORDER BY s.score DESC);

Editorial Solution

Approach 1: DENSE_RANK

SELECT
  S.score,
  DENSE_RANK() OVER (
    ORDER BY
      S.score DESC
  ) AS 'rank'
FROM
  Scores S;

Approach 2: Correlated subquery with COUNT(DISTINCT ...)

SELECT
  S1.score,
  (
    SELECT
      COUNT(DISTINCT S2.score)
    FROM
      Scores S2
    WHERE
      S2.score >= S1.score
  ) AS 'rank'
FROM
  Scores S1
ORDER BY
  S1.score DESC;

Tags:

Updated: