0% found this document useful (0 votes)
81 views1 page

SQL Queries

The document provides SQL queries to find duplicate columns, the second highest salary overall and by department, the top N salaries, and the first and last records in a table. It also shows how to display odd/even numbered records and eliminate duplicate rows.

Uploaded by

Deepak Kumar
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
81 views1 page

SQL Queries

The document provides SQL queries to find duplicate columns, the second highest salary overall and by department, the top N salaries, and the first and last records in a table. It also shows how to display odd/even numbered records and eliminate duplicate rows.

Uploaded by

Deepak Kumar
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd

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