SQL: ROWNUM Vs ROW NUMBER Oracle

GoLearningPoint
GoLearningPoint
1.2 هزار بار بازدید - 4 سال پیش - SQL: What is the Difference
SQL: What is the Difference between ROWNUM And ROW_NUMBER()?
ROWNUM is a Pseudocolumn. Placeholder that assigns sequence to your resultset.
ROW_NUMBER() is an Analytical Function. Group wise sequencing.

If you place ORDER BY clause in the query, the ROWNUM column’s value gets jumbled.The
order is lost and no more we see the values being fetched in sequence. The alternate
option is to use ROW_NUMBER analytical function.
Select ROWNUM, E.* From Employee E Order By ID DESC;
Select E.*,
ROW_NUMBER() Over(Order By ID DESC) Rn
From Employee E;

ROWNUM is useful when you have to limit a number of row fetch, without worrying about
the exact data being fetched.

ROW_NUMBER is more useful when you have to generate a sequence of numbers after
sorting the data fetch on specific column or limiting data fetched after sorting.

ROWNUM is generated before sorting so it can be used in WHERE clause whereas
ROW_NUMBER cannot be used in WHERE clause, it can be used to filter only after
sorting, by using an outer query.

Select * From Employee Where ROWNUM less than =5;

Select * From
(
Select E.*,
ROW_NUMBER() Over(PARTITION BY DEPTID Order By SALARY DESC) Rn
From Employee E
) Where Rn=1;

Home Page
https://tipsfororacle.blogspot.com/

#GoLearningPoint
4 سال پیش در تاریخ 1399/04/28 منتشر شده است.
1,242 بـار بازدید شده
... بیشتر