ROW_NUMBER()
ROW_NUMBER() is a SQL function in the Window Functions category. Assigns a unique sequential integer to each row within its partition (1, 2, 3...). The syntax is ROW_NUMBER() OVER (PARTITION BY column ORDER BY column). It accepts PARTITION BY column ORDER BY column. It returns sequential integer. A typical example: SELECT name, salary, ROW_NUMBER() OVER (ORDER BY salary DESC) AS rank FROM employees; -- 1 = highest salary, 2 = second highest, etc. -- Per department: SELECT name, dept, salary, ROW_NUMBER() OVER (PARTITION BY dept ORDER BY salary DESC) AS dept_rank FROM employees; A close relative is RANK(), which similar to ROW_NUMBER but gives the same rank to equal values. Leaves gaps in ranking. A close relative is DENSE_RANK(), which like RANK but without gaps. Equal values get the same rank, and the next number follows sequentially. A close relative is LAG(), which accesses data from a previous row within the same result set (useful for comparing values).