How One can easily select all even, odd, or Nth rows from a
table using SQL queries?
Answers were Sorted based on User's Feedback
Answer / guru
Please Use Below Query and Kindly post reply feedback
Odd number of records:
select * from emp where (rowid,1) in (select rowid,
mod(rownum,2) from emp);
Output:-
1
3
5
Even number of records:
select * from emp where (rowid,0) in (select rowid,
mod(rownum,2) from emp)
Output:-
2
4
6
For nth number,
Example we r considering number n=10
select * from emp where (rowid,0) in (select rowid,
mod(rownum,10) from emp)
Is This Answer Correct ? | 18 Yes | 8 No |
Answer / mritunjay
hi..mr guru all are correct except for the Ntn row
Odd number of records:
select * from emp where (rowid,1) in (select rowid,
mod(rownum,2) from emp);
Output:-
1
3
5
Even number of records:
select * from emp where (rowid,0) in (select rowid,
mod(rownum,2) from emp)
Output:-
2
4
6
For nth number,
Example we r considering number n=10
select * from emp where (rowid,1) in (select rowid,
rownum/:N) from emp)
Is This Answer Correct ? | 12 Yes | 8 No |
Answer / mahesh
SIMPLE ODD AND EVEN...
SQL> SELECT ROWNUM FROM EMP GROUP BY ROWNUM HAVING MOD(ROWNUM,2)=1
2 /
ROWNUM
----------
1
3
5
1* SELECT ROWNUM FROM EMP GROUP BY ROWNUM HAVING MOD(ROWNUM,2)=0
2 /
ROWNUM
----------
2
4
Goahead...
Is This Answer Correct ? | 0 Yes | 0 No |
Answer / guru
SELECT *
FROM emp
WHERE (ROWID,0) IN (SELECT ROWID, MOD(ROWNUM,4)
FROM emp);
Is This Answer Correct ? | 6 Yes | 17 No |
How many joins can you have in sql?
how many ways to get the current time? : Sql dba
What do you understand by case manipulation functions?
Why is sql*loader direct path so fast?
How to connect a sql*plus session to an oracle server?
What is sql*loader and what is it used for?
Explain Connect by Prior?
select top 3 sal from each dept?
17 Answers IBM, TCS,
How do you exit in sql?
how do you count the duplicate records in a table
what is self join and how it works??
Can a table have no primary key?