Professional Documents
Culture Documents
Types of pseudocolumn
1. RowID
2. RowNum
Row ID
It is a pseudocolumn which is present in a database for every table.
characteristics of Row ID
It is a unique
It is not null
RowNum
It is a preudocolumn which is present in a database for every table
Characteristics of RowNum
Row num is a number or integer
Pseudocolumn 1
Rownum is dynamic in nature.
select rownum,emp.*
from emp
where rownum<6;
select rownum,emp.*
from emp
where rownum between 1 and 5;
select rownum,emp.*
from emp
where rownum<=5;
select rownum,emp.*
from emp
where rownum In(1,2,3,4,5);
select rownum,emp.*
from emp
where rownum<=10;
select rownum,emp.*
from emp
where rownum=1;
select rownum,emp.*
from emp
where rownum=2;
Pseudocolumn 2
output is no rows selected (and this is the reson why rownum is dynamic in
nature)
thus we provide alias name for rownum so that it can be treated as normal
column instead of pseudocolumn
hence for that we need to take from clause twice in the query
select *
from (select rownum as r1,emp.*
from emp)
where r1=2;
select *
from (select rownum as r1,emp.*
from emp)
where r1=5;
select *
from (select rownum as r1,emp.*
from emp)
where r1 in (2,4,6,8,10);
select *
from (select rownum as r1,emp.*
from emp)
Pseudocolumn 3
where r1 In ( (select max(rownum) from emp),
(select max(rownum) from emp where rownum<(select max(rownum) from emp)),
(select max(rownum) from emp where rownum<((select max(rownum) from emp where rownum<
(select max(rownum) from emp)))));
select *
from (select rownum as r1,emp.*
from emp)
where r1 In ((subqury 1),
(subqury 2),
(subqury 3)
);
select *
from (select rownum as r1,emp.*
from emp)
where r1 in ((select max(rownum) from emp),
(select max(rownum) from emp where rownum<(select max(rownum) from emp)),
(select max(rownum) from emp where rownum<((select max(rownum) from emp where rownum<
(select max(rownum) from emp)))),
select max(rownum)from emp where rownum<(
(select max(rownum) from emp where rownum<((select max(rownum) from emp where rownum<
(select max(rownum) from emp))))),);
sal R1 SAL
2000 1 5000
4000 2 4000
3500 3 3500
4000 4 3000
5000 5 2000
3500
Pseudocolumn 4
5000 AFTER ORDER BY
3000 That’s how you can find maximum
salary and minimum salary along
with 3rd maximum salary
SELECT *
FROM (SELECT ROWNUM R1,SAL FROM (SELECT DISTINCT(SAL) FROM EMP ORDER BY SAL DESC))
WHERE R1=4;
SELECT *
FROM (SELECT ROWNUM R1,SAL
FROM (SELECT DISTINCT(SAL)
FROM EMP
ORDER BY SAL ASC))
WHERE R1=10;
SELECT *
FROM (SELECT ROWNUM R1,SAL
FROM (SELECT DISTINCT(SAL)
FROM EMP
ORDER BY SAL ASC))
WHERE R1 IN (2,4,6,8,10);
SELECT *
FROM (SELECT ROWNUM R1,SAL
FROM (SELECT DISTINCT(SAL)
FROM EMP
ORDER BY SAL ASC)),(SELECT ROWNUM R2,SAL
FROM (SELECT DISTINCT(SAL)
FROM EMP
ORDER BY SAL DESC)
)
WHERE R1=5 AND R2=6;
Pseudocolumn 5
Pseudocolumn 6