1. Find highest salary? select * from emp where sal=(select max(sal) from emp); 2.

Find lowest salary? select * from emp where sal=(select min(sal) from emp); 3. Find the top 5 employee salaries? select * from (select * from emp order by sal desc) where rownum<6; 4. Find the lowest 5 employee salaries? select * from (select * from emp order by sal asc) where rownum<6; 5. Find the particular employee salary? for maximum: select * from emp where sal in(select min(sal)from (select sal from emp group by sal order by sal desc) where rownum<=&n); select * from emp a where &n=(select count(distinct(sal)) from emp b where a.sal<=b.sal); for minimum: select * from emp where sal in(select max(sal) from(select sal from emp group by sal order by sal asc) where rownum<=&n); select * from emp a where &n=(select count(distinct(sal)) from emp b where a.sal>=b.sal) 6. Query for combining two tables(INNER JOIN)? select emp.empno,emp.ename,dept.deptno from emp,dept where emp.deptno=dept.deptno; By using aliases: select e.empno,e.ename,d.deptno from emp e,dept d where e.deptno=d.deptno; select empno,ename,sal,dept.* from emp join dept on emp.deptno=dept.deptno: 7. Query for joining table it self(SELF JOIN)? select e.ename “employee name”,e1.ename “manger name” from emp e,emp e1 where e.mgr=e1.empno;

Query to display 3rd highest and 3rd lowest salary? select * from emp e1 where 3=(select count(distinct sal) from emp e2 where e1. 10. select deptno.deptno.d.* from emp full outer join dept on emp.sal) union select * from emp e3 where 3=(select count(distinct sal) from emp e4 where e3.dept.sal<=e2. select ename from emp group by rownum.deptno(+)=d.sal.ename. 13.sal.deptno=dept.dept.ename. select empno. Right Outer Join: select empno. Query to display the duplicate records? select * from dup where rowid not in(select max(rowid)from dup group by eno). Query to delete the duplicate records? delete from dup where rowid not in(select max(rowid)from dup group by eno).deptno=dept. Query to display the records from M to N.ename having rownum>1 and rownum<6. Query for joining two tables(OUTER JOIN)? select e.deptno 9.deptno from emp e. Query to display Nth record from the table? select * from emp where rownum<=&n minus select * from emp where rownum<&n.sal>=e4. 12. .1.deptno=dept.8. 11. 11.ename. How to display duplicate rows in a table? select * from emp where deptno=any (select deptno from emp having count(deptno)>1 group by deptno).* from emp left outer join dept on emp. Left Outer Join: select empno.deptno.ename.sal.dept d where e.deptno. select * from emp where rownum<=7 minus select * from emp where rownum<5.* from emp right outer join dept on emp.dept.deptno order by e.ename.sal).sal from emp where rowid in(select rowid from emp where rownum<=7 minus select rowid from emp where rownum<4).

. . Query to display odd records only? select * from emp where (rowid.mod (rownum.2) from emp).0) in (select rowid. drop first 5. Query to display first N records? select * from(select * from emp order by rowid) where rownum<=&n. last 5 records in emp table)? select * from emp where rownum<=(select count(*)-5 from emp) minus select * from emp where rownum<=5. 16. Query to display middle records (i.mod (rownum.e. Query to display even records only? select * from emp where (rowid. 15.2) from emp).14.1) in (select rowid. 17.

Sign up to vote on this title
UsefulNot useful