Pages

Tuesday 17 September 2013

ORACLE SELECT RECORD BETWEEN No1. TO No2.

In some cases we have to select the records from the database table between some specific value. Like I want to select record 1 TO 10 at first time 11 TO 20 at second time like this.

For this i have a solution with just a single query:

------------------------------------------------------------------------------------------------------------
SELECT * 
FROM (SELECT ALIAS1.*, ROW_NUMBER() OVER(ORDER BY column_name) AS MYROW FROM table_name ALIAS1 WHERE ALIAS1.column_name = where_clause) 
WHERE (MYROW BETWEEN 1 AND 10);
---------------------------------------------------------------------------------------------------------------------------------

EXPLANATION:-
In this Query Inner select query will select all the record from given table_name that satisfy the Inner WHERE clause and provide row number to each record and make it as table alias MYROW. Now outer WHERE clause will select 1 to 10 record from that record and display it as final output.


IF YOU HAVE BETTER WAY TO DO THIS THEN PLEASE PROVIDE IT AS COMMENT IT WILL VERY HELPFUL.

No comments:

Post a Comment