Skip to content

Latest commit

 

History

History
84 lines (54 loc) · 1.83 KB

File metadata and controls

84 lines (54 loc) · 1.83 KB

📚 ROW NUMBER

The ROW_NUMBER() function assigns a unique sequential integer to rows within a result set.
It is often used for pagination, ranking, or ordering results uniquely.


🛠️ Basic Syntax

SELECT column1, 
       ROW_NUMBER() OVER (PARTITION BY column2 ORDER BY column3) AS row_num
FROM table_name;
  • PARTITION BY divides the rows into groups (optional).
  • ORDER BY determines the sequence of row numbers within each partition.

Example

SELECT employee_id, department_id, 
       ROW_NUMBER() OVER (PARTITION BY department_id ORDER BY salary DESC) AS row_num
FROM employees;
  • This query assigns a row number to each employee within their department, ordered by salary.

Key Points

  • Each row gets a unique number starting from 1 within its partition.
  • If PARTITION BY is omitted, the entire result set is treated as a single group.
  • Useful for pagination by selecting ranges of rows.

Additional Example

SELECT order_id, customer_id, 
       ROW_NUMBER() OVER (ORDER BY order_date) AS order_sequence
FROM orders;
  • This query numbers all orders by order date, without partitioning.

🎥 Video Notes


📝 Problem Description

Describe the problem, challenge, or topic discussed in a video related to SELECT FROM.
What concept was explained or what exercise was solved?


DataBase Given


💻 My SQL Code

-- Write your SQL code attempt or solution related to SQL COMMAND
SQL COMMAND

🧠 Solution Code / Explanation

SQL COMMAND

Explanation - Explain what you learned, any key takeaways, or how you solved the problem related to COMMAND._


⬅️ Previous: WINDOW FUNCTION Next ➡️ RANK