SQL Queries Set-1
SQL Queries Set-1
###############
# 7 ############## List dept no., Dept name for all the departments in which t
here are no employees in the department. ###############
select * from dept where deptno not in (select deptno from emp);
alternate solution:
select * from dept a where not e
xists (select * from emp b where a.deptno = b.deptno);
altertnate solution:
select empno,ename,b.deptno,dnam
e from emp a, dept b where a.deptno(+) = b.deptno and empno is null;
# 8 ############## How to get 3 Max salaries ? ###############
select distinct sal from emp a where 3 >= (select count(distinct sal) fr
om emp b where a.sal <= b.sal) order by a.sal desc;
# 9 ############## How to get 3 Min salaries ? ###############
select distinct sal from emp a where 3 >= (select count(distinct sal) f
rom emp b where a.sal >= b.sal);
# 10 ############## Select DISTINCT RECORDS from emp table. ###############
select * from emp a where rowid = (select max(rowid) from emp b where
a.empno=b.empno);
# 11 ############## How to delete duplicate rows in a table? ###############
delete from emp a where rowid != (select max(rowid) from emp b where a.
empno=b.empno);
# 12 ############## Count of number of employees in department wise. #####
##########
select count(EMPNO), b.deptno, dname from emp a, dept b where a.deptno(
+)=b.deptno group by b.deptno,dname;
# 13 ############## In emp table add comm+sal as total sal.
select ename,(sal+nvl(comm,0)) as totalsal from emp;
# 14 ##############
###############
###############
select * from emp where sal> any(select sal from emp where sal<3000);
# 15 ############## How can I create an empty table emp1 with same structure a
s emp? ###############
Create table emp1 as select * from emp where 1=2;
# 16 ############## Select all records where dept no of both emp and dept tabl
e matches. ###############
select * from emp where exists(select * from dept where emp.deptno=dept.
deptno)
# 17 ############## If there are two tables emp1 and emp2, and both have commo
n record. How can I fetch all the recods but common records only once? #######
########