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 |
Is join and inner join the same?
How we can update the view?
What is the difference between SQL and PLSQL
What is the result, when NULL is compared with NULL?
how to eliminate null values in a column i.e table vlaues 1 2 3 NULL 3 4 1 5 NULL i want output like this 1 2 3 3 4 1 5 i dnt want to use nvl is null and i dnt want replace the NULL value with any value i.e nvl(col,o);
What are properties of the transaction?
Why do we use function in pl sql?
What is meant by Join? What are the different types of Joins available? Explain.
What are procedures used for?
why not null constraint showing its constraint type as 'c'
Name the operator which is used in the query for pattern matching?
how instead of triger is helpful for mutating tables errors handlling ?
Oracle (3259)
SQL Server (4518)
MS Access (429)
MySQL (1402)
Postgre (483)
Sybase (267)
DB Architecture (141)
DB Administration (291)
DB Development (113)
SQL PLSQL (3330)
MongoDB (502)
IBM Informix (50)
Neo4j (82)
InfluxDB (0)
Apache CouchDB (44)
Firebird (5)
Database Management (1411)
Databases AllOther (288)