ROW_NUMBER

Assigns a temporary sequential number to each row within a partition of a result set, starting at 1 for the first row in each partition.

Analyze Syntax

func.row_number().over(partition_by=[<columns>], order_by=[<columns>])

Analyze Examples

table.employee_id, table.first_name, table.last_name, table.department, table.salary, func.row_number().over(partition=table.department, order_by=table.salary).alias('row_num')

┌──────────────────────────────────────────────────────────────────────────────────────────────────────┐
   employee_id       first_name         last_name        department          salary      row_num 
├─────────────────┼──────────────────┼──────────────────┼──────────────────┼─────────────────┼─────────┤
               2  Jane              Smith             HR                          85000        1 
               5  Tom               Brown             HR                          75000        2 
               1  John              Doe               IT                          90000        1 
               3  Mike              Johnson           IT                          82000        2 
               4  Sara              Williams          Sales                       77000        1 
└──────────────────────────────────────────────────────────────────────────────────────────────────────┘

SQL Syntax

ROW_NUMBER() 
  OVER ( [ PARTITION BY <expr1> [, <expr2> ... ] ]
  ORDER BY <expr3> [ , <expr4> ... ] [ { ASC | DESC } ] )
ParameterRequired?Description
ORDER BYYesSpecifies the order of rows within each partition.
ASC / DESCNoSpecifies the sorting order within each partition. ASC (ascending) is the default.
QUALIFYNoFilters rows based on conditions.

SQL Examples

This example demonstrates the use of ROW_NUMBER() to assign sequential numbers to employees within their departments, ordered by descending salary.

-- Prepare the data
CREATE TABLE employees (
  employee_id INT,
  first_name VARCHAR,
  last_name VARCHAR,
  department VARCHAR,
  salary INT
);

INSERT INTO employees (employee_id, first_name, last_name, department, salary) VALUES
  (1, 'John', 'Doe', 'IT', 90000),
  (2, 'Jane', 'Smith', 'HR', 85000),
  (3, 'Mike', 'Johnson', 'IT', 82000),
  (4, 'Sara', 'Williams', 'Sales', 77000),
  (5, 'Tom', 'Brown', 'HR', 75000);

-- Select employee details along with the row number partitioned by department and ordered by salary in descending order.
SELECT
    employee_id,
    first_name,
    last_name,
    department,
    salary,
    ROW_NUMBER() OVER (PARTITION BY department ORDER BY salary DESC) AS row_num
FROM
    employees;

┌──────────────────────────────────────────────────────────────────────────────────────────────────────┐
   employee_id       first_name         last_name        department          salary      row_num 
├─────────────────┼──────────────────┼──────────────────┼──────────────────┼─────────────────┼─────────┤
               2  Jane              Smith             HR                          85000        1 
               5  Tom               Brown             HR                          75000        2 
               1  John              Doe               IT                          90000        1 
               3  Mike              Johnson           IT                          82000        2 
               4  Sara              Williams          Sales                       77000        1 
└──────────────────────────────────────────────────────────────────────────────────────────────────────┘
Last modified June 11, 2024 at 7:47 PM EST: adding window functions (6bcb2f2)