The ROW_NUMBER function in SQL assigns a unique number to each row in the result set. It works by scanning the table rows, optionally grouping them into partitions using PARTITION BY, then ordering each partition by specified columns. After ordering, it assigns numbers starting at 1 within each partition. The final output includes the original rows plus the assigned row numbers. This helps to rank rows or paginate results. If PARTITION BY is omitted, numbering continues across all rows without restarting. The ORDER BY inside OVER() controls the order of numbering. This visual trace shows each step from scanning rows, partitioning, ordering, numbering, to returning the final result.