mysqldude

Ranking Columns

Version compatibility

StepMySQLMariaDB
5.65.78.08.4910.610.1111.411.8
@Rank := @Rank + 1 AS rank (original)
original post
✓✓✗ 1064✗ 1064✗ 1064✓✓✓✓
Dense rank with CASE and @prev_Age (original)
original post
✓✓✗ 1064✗ 1064✗ 1064✓✓✓✓
RANK() / DENSE_RANK() OVER (ORDER BY age) (modern)
modern rewrite
✗ 1064✗ 1064✓✓✓✓✓✓✓
@Rank := @Rank + 1 AS rank (original) original post
MySQL 5.6✓5.7✓8.0✗ 10648.4✗ 10649✗ 1064
MariaDB 10.6✓10.11✓11.4✓11.8✓
Dense rank with CASE and @prev_Age (original) original post
MySQL 5.6✓5.7✓8.0✗ 10648.4✗ 10649✗ 1064
MariaDB 10.6✓10.11✓11.4✓11.8✓
RANK() / DENSE_RANK() OVER (ORDER BY age) (modern) modern rewrite
MySQL 5.6✗ 10645.7✗ 10648.0✓8.4✓9✓
MariaDB 10.6✓10.11✓11.4✓11.8✓
✓works ✓ 1062fails with the error the step demonstrates ⚠works with a warning ≠runs, result differs ✗fails Tested 2026-11-23 on 5.6.51, 5.7.44, 8.0.46, 8.4.13, 9.7.4, 10.6.28, 10.11.19, 11.4.13, 11.8.9
What the warnings and failures say (3)
  • @Rank := @Rank + 1 AS rank (original) (MySQL 8.0, MySQL 8.4, MySQL 9): error 1064: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'rank FROM person p ORDER BY age' at line 1
  • Dense rank with CASE and @prev_Age (original) (MySQL 8.0, MySQL 8.4, MySQL 9): error 1064: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'rank, @prev_Age := age FROM person p, (SELECT
  • RANK() / DENSE_RANK() OVER (ORDER BY age) (modern) (MySQL 5.6, MySQL 5.7): error 1064: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(ORDER BY age) AS rnk, DENSE_RANK() OVER (ORD

Related errors: 1064, 1287

The original article

First published 2015-01-14. The text below is the original; the table above shows how it behaves on current versions.

MySQL unfortunately does not provide any RANK() function yet! However, using user session variables it is possible to get a ranking.

Let us imagine we want to get a rank based on the age of a person:

SELECT    first_name,
          age
          @Rank := @Rank + 1 AS rank
FROM      person p, (SELECT @Rank := 0) r
ORDER BY  age;

The (SELECT @Rank := 0) initialises the variable without requiring a separate SET command. This could also have been done as follows:

SET @Rank := 0;
SELECT    first_name,
          age,
          @Rank := @Rank + 1 AS rank
FROM      person p
ORDER BY  age;

Now let’s look at this in action.

CREATE TABLE person (id int, first_name varchar(20), age int);

INSERT INTO person VALUES (1, 'Tom', 25);
INSERT INTO person VALUES (2, 'Tim', 20);
INSERT INTO person VALUES (3, 'Jack', 30);
INSERT INTO person VALUES (4, 'John', 32);
INSERT INTO person VALUES (5, 'Brian', 22);
INSERT INTO person VALUES (6, 'Jane', 18);
INSERT INTO person VALUES (7, 'Bob', 36);
INSERT INTO person VALUES (8, 'Sally', 26);

mysql> SELECT    first_name,
    ->           age,
    ->           @Rank := @Rank + 1 AS rank
    -> FROM      person p
    -> ORDER BY  age;
+------------+------+------+
| first_name | age  | rank |
+------------+------+------+
| Jane       |   18 |    1 |
| Tim        |   20 |    2 |
| Brian      |   22 |    3 |
| Tom        |   25 |    4 |
| Sally      |   26 |    5 |
| Jack       |   30 |    6 |
| John       |   32 |    7 |
| Bob        |   36 |    8 |
+------------+------+------+
8 rows in set (0.00 sec)

Now this works well but what if we have two entries with the same rank. For example let’s imagine in the example above we add Billy with an age of 25 same as Tom. In this case we want both Tom and Billy to have a rank of 4 and Sally with a rank of 6 (skipping the rank 5).

In this case we will need to keep track of the previous rank and see

INSERT INTO person VALUES (9, 'Billy', 25);

SELECT    first_name,
          age,
          CASE
          WHEN @prev_Age = age THEN @Counter
          WHEN @prev_Age <> age THEN @Counter := @Counter + 1
          END AS rank,
          @prev_Age := age
FROM      person p, (SELECT @Counter := 0) c, (SELECT @prev_Rank := NULL) r, (SELECT @prev_Age := 0) a
ORDER BY  age;

+------------+------+------+------------------+
| first_name | age  | rank | @prev_Age := age |
+------------+------+------+------------------+
| Jane       |   18 | 1    |               18 |
| Tim        |   20 | 2    |               20 |
| Brian      |   22 | 3    |               22 |
| Tom        |   25 | 4    |               25 |
| Billy      |   25 | 4    |               25 |
| Sally      |   26 | 5    |               26 |
| Jack       |   30 | 6    |               30 |
| John       |   32 | 7    |               32 |
| Bob        |   36 | 8    |               36 |
+------------+------+------+------------------+
9 rows in set (0.01 sec)

In this case we have defined an order with both Tom and Sally with the same age having the same rank. However, we need to modify this to display actual rank so Jack should show rank 6 and not 5 as is the case here.

SELECT    first_name,
          age,
          CASE
          WHEN @prev_Age = age THEN @prev_Rank
          WHEN @prev_Age <> age THEN @prev_Rank := @Counter
          END AS rank,
          @prev_Age := age as age,
          @Counter := @Counter + 1 as counter
FROM      person p, (SELECT @Counter := 1) c, (SELECT @prev_Rank := 1) r, (SELECT @prev_Age := 0) a
ORDER BY  age;

+------------+------+------+------+---------+
| first_name | age  | rank | age  | counter |
+------------+------+------+------+---------+
| Jane       |   18 | 1    |   18 |       2 |
| Tim        |   20 | 2    |   20 |       3 |
| Brian      |   22 | 3    |   22 |       4 |
| Tom        |   25 | 4    |   25 |       5 |
| Billy      |   25 | 4    |   25 |       6 |
| Sally      |   26 | 6    |   26 |       7 |
| Jack       |   30 | 7    |   30 |       8 |
| John       |   32 | 8    |   32 |       9 |
| Bob        |   36 | 9    |   36 |      10 |
+------------+------+------+------+---------+
9 rows in set (0.00 sec)