can the ROW_NUMBER() function be used without the PARTITION clause?

Gil Robinson 0 Reputation points
2025-05-09T14:17:25.23+00:00

I'd like to get row numbers for my output without having to use ROW_NUMBER() OVER(PARTITION BY ...). Is this possible, and can you provide an example?

Developer technologies | Transact-SQL
Developer technologies | Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Viorel 124.9K Reputation points
    2025-05-09T15:22:34.75+00:00

    It requires an order by, where you can specify any columns, for example:

    select *, row_number() over(order by MyColumn1) as num 
    from MyTable
    

    If the order is not important, then, for example:

    select *, row_number() over(order by @@version) as num 
    from MyTable
    

    To assign numbers in random order:

    select *, row_number() over(order by newid()) as num 
    from MyTable
    
    0 comments No comments

  2. Ramana Kopparapu 306 Reputation points
    2025-10-23T14:18:10.99+00:00

    Partition By clause is not mandatory.

    select *, Row_number() Over (order by salary desc) rn from emp-- this query just assign sequential number after the data sorted on sal column in descending order...

    Gender-wise Salary rankings:

    select *, Row_number() Over (partition by gender order by salary desc) rn from emp-- this query assigns ranks once after data partioned on gender column

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.