0

I retrieved some city names from a table but along with names I want to show an auto generated Id eg:

    name | id
-------------------
    New Delhi | 1
    Kanpur | 2
    Mumbai | 3

How could i achieve this in PostGreSql? I don't wan't to generate a sequence and use nextval('sequence_name').

query for retrieving names: select city_name as name from cities;

1 Answer 1

1

You can use row_number() function in Postgres's window functions

As per documentation

row_number() - number of the current row within its partition, counting from 1

select city_name as name
      ,row_number() over() id 
from cities;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.