May 29, 2012

Real time Queries for SQL

Create emp table in your Schema. Insert 10 values, Then try bellow queries.

How to retrive the second record in the table(sql)?

(select rownum, e.* from emp e WHERE ROWNUM < 3) MINUS (select rownum,
e.* from emp e WHERE ROWNUM < 2) ;

How to find out the records except first record in the table(sql)?

(select rownum, e.* from emp e) MINUS (select rownum,e.* from emp
e WHERE ROWNUM < 2) ;

or

select * from emp where rowid > (select min(rowid) from emp);

How to find out the records except last record in the table(sql)?

select * from EMP WHERE ROWNUM <> (select COUNT(*) from EMP);

How to find out the last record in the table(sql)?

select * from emp where rownum<(select COUNT(*) from EMP)
minus
select * from emp where rownum<(select COUNT(*)-1 from EMP);
(Performance wise its not consider above query)

or

select * from emp where rowid < (select max(rowid) from emp);


How to retrive last record in the given table?

select * from emp where rowid=(select max(rowid) from emp);

How to retrive first record in the given table?

select * from emp where rowid = (select min(rowid) from emp);

How to find out the randon records in table or work space?
select distinct decode(rownum, 1, empno,6,empno,7, empno,ename ) from
emp;

or

select * from (select rownum rn, e.* from emp e) where rn in (1, 5, 9);

No comments:

Post a Comment

I'm certainly not an expert, but I'll try my hardest to explain what I do know and research what I don't know.

My Favorite Site's List

#update below script more than 500 posts