
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
MySQL query to generate row index (rank) in SELECT statement?
To generate a row index, use ROW_NUMBER(). Let us first create a table −
mysql> create table DemoTable ( Name varchar(40) ); Query OK, 0 rows affected (0.49 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values('Chris'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('Chris'); Query OK, 1 row affected (0.22 sec) mysql> insert into DemoTable values('Chris'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values('Robert'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values('Robert'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('Adam'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values('Adam'); Query OK, 1 row affected (0.22 sec) mysql> insert into DemoTable values('Adam'); Query OK, 1 row affected (0.08 sec) mysql> insert into DemoTable values('Adam'); Query OK, 1 row affected (0.08 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+--------+ | Name | +--------+ | Chris | | Chris | | Chris | | Robert | | Robert | | Adam | | Adam | | Adam | | Adam | +--------+ 9 rows in set (0.00 sec)
Following is the query to generate a row index in MySQL SELECT statement. Here, we have set rank for duplicate names −
mysql> select Name, row_number() over (partition by Name) as `Rank` from DemoTable;
This will produce the following output −
+--------+------+ | Name | Rank | +--------+------+ | Adam | 1 | | Adam | 2 | | Adam | 3 | | Adam | 4 | | Chris | 1 | | Chris | 2 | | Chris | 3 | | Robert | 1 | | Robert | 2 | +--------+------+ 9 rows in set (0.00 sec)
- Related Articles
- MySQL query to select maximum and minimum salary row?
- MySQL query to select one specific row and another random row?\n
- MySQL query to select rows except first row in descending order?
- Can we use SELECT NULL statement in a MySQL query?
- How to use prepared statement for select query in Java with MySQL?
- How to generate field in MySQL SELECT?
- MySQL query to delete row
- How to select last row in MySQL?
- Generate the row count (serial number) of records after returning the result in MySQL query?
- Select a random row in MySQL
- How to select next row pagination in MySQL?
- MySQL case statement inside a select statement?
- How to use NULL in MySQL SELECT statement?
- MySQL query to select a random row value (Id and Name) having multiple occurrences (Name)?
- MySQL LIMIT to select a single row

Advertisements