SQL Queries

You might also like

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 1

1.

How to find out the duplicate column Select column_name, count(*) from table_name having count(*)>1

2. How to find 2nd max salary from emp ? Select max(sal) from emp where sal not in(select max(sal) from emp)

3. How to find max salary department wise in emp table? Select deptno, max(sal) from emp group by deptno;

4. How to find 2nd max salary department wise in emp table? Select deptno,max(sal) from emp where (deptno,sal) not in(select deptno,max(sal) from emp group by deptno) group by deptno; 5. How to display Top N salaries in emp? Select * from (select distinct sal from emp order by sal desc) where rownum<=&n

6. 22. How To display Last Record in emp table? Select * from ( select rownum as rn,emp.* from emp) where rn in(select count(*) from emp)

7. 23. How To display First and last Records in emp table? Select * from ( select rownum as rn,emp.* from emp) where rn in(1,(select count(*) from emp))

8. Display Odd/ Even number of records? Odd number of records: Select * from emp where (rowid,1) in (select rowid, mod(rownum,2) from emp);

Even number of records: Select * from emp where (rowid,0) in (select rowid, mod(rownum,2) from emp); 9. Eliminate duplicates rows from a table?

DELETE FROM table_name A WHERE ROWID > (SELECT min(rowid) FROM table_name B WHERE A.key_values = B.key_values);

You might also like