Here is my MySQL query:
SELECT name FROM table;
How can I also select an increment counter alongside name? Expected output:
In MySQL 8 and above you can also use the ROW_NUMBER() Window function.
SELECT
name,
ROW_NUMBER() OVER ()
FROM table
Result:
Jay 1
roy 2
ravi 3
ram 4
As shown by juergen d, it would be a good idea to put an ORDER BY to have a deterministic query.
The ORDER BY can apply to the query and the counter independently. So:
SELECT
name,
ROW_NUMBER() OVER (ORDER BY name DESC)
FROM table
ORDER BY name
would give you a counter in decreasing order.
Result:
Jay 4
ram 3
ravi 2
roy 1