How to show row numbers in PostgreSQL query?
To include sequential row numbers in your PostgreSQL query results, you can use the ROW_NUMBER()
function along with the OVER()
clause. If you want the results sorted, add an ORDER BY
clause within OVER()
.
Here's a general template:
Replace column_to_sort
, other_columns
, and table_name
with your real column names and table name respectively. This will output a new column row_number
in your result set, numbering rows in the order you specified.
However, if you don't care about the order and merely want to number the rows as they appear, simply omit the ORDER BY
clause:
This will append a row_number
column to the query result without any specific sorting.
Dealing with specific scenarios
There are several scenarios that you might encounter while implementing ROW_NUMBER() function:
Taming the NULL Beast
In case you have NULL values in your column_to_sort and you prefer them to be at the tail end, you can use the NULLS LAST
option:
This way, if any row has a NULL in the column_to_sort, it'll be considered the least of all and appear last.
Acing Performance on Sweaty Datasets
Remember, ROW_NUMBER()
could slow your query down when dealing with larger datasets. So, ensure your sort column is indexed properly. Alternatively, consider numbering at the app layer if it offers better performance.
Cultivating Compatibility with older PostgreSQL versions
For Postgres versions before 8.4 where ROW_NUMBER()
isn't available, you can use a count on a self-join as a workaround:
Replace id
with your primary key column. This is not as awesome as our ROW_NUMBER()
but, sometimes, necessity is indeed the mother of (re)invention.
Running wild when primary keys are absent
When your table doesn't have a primary key to order by, we can use the array_agg
function. Although, beware of the Scooby Doo-monster known as performance cost hiding behind that tree!
Untangling Complex Queries
When your query has complex window function usage (and even if it resembles a bowl of spaghetti), encapsulate the logic in a subquery or a CTE:
Sharing via SQL Fiddle
When troubleshooting or sharing your queries, SQL Fiddle is your friend in need. Write SQL, create schema, and run queries in a shared environment.
Was this article helpful?