SQL Assign
SQL Assign
1 select mgr
2 from emp
3 where sal>1500;
MGR
----------
7698
7839
7839
7839
7566
7566
7 rows selected.
===================================================================================
=================================================================
2.List all employees with sal>1200 and <2000
1 select *
2 from emp
3 where sal>1200 and sal<2000;
===================================================================================
=================================================================
3.list all employees with sal is 1600 oe sal is 800 or sal is 1900.
1 select *
2 from emp
3 where sal in(1600,800,1900);
1 select *
2 from emp
3 where ename like '%R_';
1 select *
2 from emp
3 where ename like 'A%' and ename like '%N';
****************Q2.solve following*****************
===================================================================================
=================================================================
1.list all employee with salary>1250 and dept no=30.
1 select *
2 from emp
3 where sal>1250 and deptno=30;
1 select *
2 from emp
3* where sal between 1250 and 3000;
10 rows selected.
===================================================================================
=================================================================
3. list all employees with sal >1250 and <3000.
1 select *
2 from emp
3* where sal >1250 and sal<3000
6 rows selected.
===================================================================================
=================================================================
4.list all employees with salary either equal to 3000 or 1250 or 1250 or 2500.
select *
2 from emp
3 where sal in (3000,1250,2500);
select *
2 from emp
3 where ename='SMITH';
===================================================================================
=================================================================
6.list all employee with name starting with S.
1 select *
2 from emp
3* where ename like 'S%'
1 select *
2 from emp
3 where ename like '%S';
1 select *
2 from emp
3* where ename like '%I_'
EMPNO ENAME
---------- ----------
7499 ALLEN
===================================================================================
=================================================================
11.list all employees with name starts with either A or starts with S or starts
with W.
1 select *
2 from emp
3 where regexp_like(ename,'(^A|^S|^W)');
10 rows selected.
===================================================================================
=================================================================
10.List all employees with name starts with A and B at 3rd position and P at second
last position.
select *
2 from emp
3 where ename like 'A_BP_';
no rows selected
===================================================================================
=================================================================
12. find max sal and min sal for each job.
1 select job,max(sal),min(sal)
2 from emp
3 group by job;
10 rows selected.
===================================================================================
=================================================================
14.find sum of sal of all employess working in dept no 10.
1 select deptno,sum(sal)
2 from emp
3 group by deptno
4 having deptno=10;
DEPTNO SUM(SAL)
---------- ----------
10 8750
===================================================================================
=================================================================
15. find maximum salary,average sal for each job in every department.
1 select job ,deptno,max(sal),avg(sal)
2 from emp
3* group by job, deptno
select deptno,max(sal)
2 from emp
3 group by deptno
4 having deptno>15
5 order by deptno;
DEPTNO MAX(SAL)
---------- ----------
20 3000
30 2850
===================================================================================
=================================================================
17.find sum salary for every department if sum is>3000.
select deptno,sum(sal)
2 from emp
3 group by deptno
4 having sum(sal)>3000;
DEPTNO SUM(SAL)
---------- ----------
30 9400
20 10875
10 8750
===================================================================================
=================================================================
18. list all department which has minimum 5 employess.
1 select deptno,count(*)
2 from emp
3 group by deptno
4 having count(*)<5;
DEPTNO COUNT(*)
---------- ----------
10 3
===================================================================================
=================================================================
19.count how many employees earn salary more than 2000 in each job.
14 rows selected.
===================================================================================
=================================================================
21. list all names and jobs so that the length of name should be 15 if it is
samller then add spaces to left.
ENAME JOB
---------- ---------
LPAD(ENAME,15,'_')
------------------------------------------------------------
SMITH CLERK
__________SMITH
ALLEN SALESMAN
__________ALLEN
WARD SALESMAN
___________WARD
ENAME JOB
---------- ---------
LPAD(ENAME,15,'_')
------------------------------------------------------------
JONES MANAGER
__________JONES
MARTIN SALESMAN
_________MARTIN
BLAKE MANAGER
__________BLAKE
ENAME JOB
---------- ---------
LPAD(ENAME,15,'_')
------------------------------------------------------------
CLARK MANAGER
__________CLARK
SCOTT ANALYST
__________SCOTT
KING PRESIDENT
___________KING
ENAME JOB
---------- ---------
LPAD(ENAME,15,'_')
------------------------------------------------------------
TURNER SALESMAN
_________TURNER
ADAMS CLERK
__________ADAMS
JAMES CLERK
__________JAMES
ENAME JOB
---------- ---------
LPAD(ENAME,15,'_')
------------------------------------------------------------
FORD ANALYST
___________FORD
MILLER CLERK
_________MILLER
14 rows selected.
===================================================================================
=================================================================
22. display min sal,max sal ,average sal for all employees woking under same
manager.
7 rows selected.
===================================================================================
=================================================================
23.
1 select sum(sal+nvl(comm,0)),avg(sal+nvl(comm,0))
2 from emp
3* where deptno in(10,20) and sal>2000
SUM(SAL+NVL(COMM,0)) AVG(SAL+NVL(COMM,0))
-------------------- --------------------
16425 3285
===================================================================================
=================================================================
24.list all employees who joined in aug 1980 and salary is >1500 and <2500.
no rows selected
===================================================================================
=================================================================
25. list all employees joined in either aug or may or dec.
1 select empno,ename
2 from emp
3* where to_char(hiredate,'mon') in ('aug', 'dec' ,'may')
SQL> /
EMPNO ENAME
---------- ----------
7369 SMITH
7698 BLAKE
7788 SCOTT
7900 JAMES
7902 FORD
===================================================================================
=================================================================
26.display name and hiredate in dd/mm/yy format for all employees whose job is
clerk and they earn some commission.
1 select ename,to_char(hiredate,'dd/mm/yy')
2 from emp
3 where job='CLERK' and sal is not null;
ENAME TO_CHAR(
---------- --------
SMITH 17/12/80
ADAMS 12/01/83
JAMES 03/12/81
MILLER 23/01/82
===================================================================================
=================================================================
27.list empcode,empno,name and job for each employee.(note:empcode is 3 to 5
characters from name and last 2 characters of job).
1 select empno,ename,job,concat(substr(ename,3,5),substr(job,-2,2))empcode
2 from emp
14 rows selected.
===================================================================================
=================================================================
28.display thousand separator and $ symbol for commission if it is null then
display it as 0 for all employees whose name starts with A ends with N.
1 select ename,empno,comm,case
2 when comm is not null then to_char(comm,'$999,999,999')
3 else '0'
4 end
5 from emp
6* where ename like 'A%N';
1 select empno,ename,sal,comm,case
2 when comm>=600 then 'excellent keep it up'
3 when comm<600 or comm is not null then 'good'
4 else 'need improvement'
5 end
6 from emp;
14 rows selected.
===================================================================================
=================================================================
31.
Table created.
1 row created.
1 row created.
SID SNAME
---------- ----------
1 ruchita
2 ash
1 row created.
1 row created.
1 row created.
CID CNAME
---------- ----------
1 dbt
2 dac
3 dbda
1 row created.
1 row created.
1 row created.
1 row created.
Table created.
no rows selected
===================================================================================
=================================================================
34.
Table altered.
Table altered.
Table altered.
Table altered.
Table altered.
===================================================================================
=================================================================
35.
update emp
2 set sal=sal+(sal*0.15)+nvl(comm,0),
3 job='MANAGER',mgr=7777
4 where deptno=10;
3 rows updated.
14 rows selected.
===================================================================================
=================================================================
36.
1 update emp
2 set job= 'Sr.CLERK'
3* where ename='SMITH'
1 row updated.
JOB ENAME
--------- ----------
Sr.CLERK SMITH
===================================================================================
=================================================================
37.increase salary of all employees by 15% if they are earning some commision.
1 select empno,ename,sal
2 from emp
3 where sal>(select sal
4 from emp
5 where ename='SMITH');
13 rows selected.
===================================================================================
================================================================
39.list all employees who are working in smith's department.
1 select empno,ename,deptno
2 from emp
3 where deptno=(select deptno
4 from emp
5 where ename='SMITH');
===================================================================================
=================================================================
41.
6 rows deleted.
8 rows selected.
===================================================================================
=================================================================
42.
1 update emp
2 set sal=(select sal
3 from emp where ename='MILLER')
4* where ename='ALLEN'
1 row updated.
1 select sal
2 from emp where ename='ALLEN';
SAL
----------
1300
===================================================================================
=================================================================
43.
update emp
set sal = (select sal
from emp
where ename='MILLER')
where deptno=(select deptno
from emp
where ename='WARD');
6 rows updated.
14 rows selected.
===================================================================================
=================================================================
44. list all employees with salary >either smith's salary or alllen' s sal.
1 select *
2 from emp
3 where sal>(select sal
4 from emp
5 where ename='SMITH')
6 or sal>(select sal
7 from emp
8 where ename='ALLEN')
13 rows selected.
===================================================================================
=================================================================
45. list all employees who earn more than average sal of dept 10.
1 select *
2 from emp
3 where sal>(select avg(sal)
4 from emp
5 where deptno=10);
1 select *
2 from emp
3 where sal>(select avg(sal)
4 from emp where deptno=(select deptno
5 from emp
6 where ename='ALLEN'));
1 select *
2 from emp e
3 where sal>(select avg(sal)
4 from emp m
5 where e.deptno=m.deptno);
6 rows selected.
===================================================================================
=================================================================
49.
1 select * from emp e
2 where e.sal<(select m.sal from emp m where e.mgr=m.empno)
1 select e.ename,d.dname
2 from emp e,dept d
3 where e.deptno=d.deptno and d.dname='purchase';
ENAME DNAME
---------- --------------
ADAMS purchase
===================================================================================
=================================================================
50. list all employees who are earning more than avg salry of their job.
1 select *
2 from emp e
3 where sal>(select avg(sal)
4 from emp m
5 where e.job=m.job);
6 rows selected.
===================================================================================
=================================================================
51. display emp name and department name.
1 select e.deptno,e.ename,d.deptno,d.dname
2 from emp e,dept d
3 where e.deptno=d.deptno
14 rows selected.
===================================================================================
=================================================================
52. display empno,ename,department name and grade(use emp,dept and salgrade table).
1 select e.empno,e.ename,d.dname,s.grade
2 from emp e inner join salgrade s
3 on sal between s.losal and s.hisal inner join dept d
4 on e.deptno=d.deptno;
14 rows selected.
===================================================================================
=================================================================
53.
1 select e.empno,e.ename,e.mgr,p.ename
2 from emp e full outer join emp p
3 on e.mgr=p.mgr;
13 rows selected.
===================================================================================
===============================================================
54.
PNAME CNAME
-------------------- --------------------
lays chips
pepsi cold drimks
nachos chips
dairymilk chocolate
pringles chips
snacks
6 rows selected.
===================================================================================
===============================================================
55.
1) faculty table
Table created.
FID FNAME S S
---------- -------------------- - -
10 ruchita DBDA DAC
11 prajkta DAC DTIIS
12 ash DBDA DIOT
13 payal DBDA DAC
2) courses table
3) room table
subquestions:-
1) select c.cid,c.cname,r.rid,r.rname
2 from courses c full outer join room r
3 on c.rid=r.rid
4 where(c.cid is null ) or (r.rid is null) ;
3) 1 select c.cid,c.rid,c.cname,r.rname,r.rid
2 from courses c full outer join room r
3 on c.rid=r.rid;
4) select r.rid,r.rname,c.cid,c.cname
2 from room r full outer join courses c
3 on c.rid=r.rid
4 where r.rid is null;
5)select f.fid,f.fname,c.cid,c.cname
from faculty f full outer join courses c
on f.fid=c.fid
where f.spskill1 ='DBDA' or f.spskill2='DBDA';
6)
1 select f.fid,f.fname,c.cid,c.cname,r.rid,r.rname
2 from faculty f inner join courses c on f.fid=c.fid inner join room r
3 on c.rid=r.rid
(1)product table
Table created.
(2)
salesman table
category table
Table created.
CATID CNAME
---------- --------------------
DESCRIPTION
--------------------------------------------------
1 chips
very crunchy
2 chocolate
very chocolaty
3 snacks
yummy
CATID CNAME
---------- --------------------
DESCRIPTION
--------------------------------------------------
4 cold drimks
thanda thnnda cool cool
PNAME CNAME
-------------------- --------------------
lays chips
nachos chips
pringles chips
2) select p.pname,s.sname
2 from product p inner join salesman s
3 on p.sid=s.sid and s.sname='kirti';
PNAME SNAME
-------------------- --------------------
lays kirti
nachos kirti
SNAME
--------------------
prasad
kirti
4)
1 select c.cname
2 from category c
3 where not exists(select *
4 from product p
5 where p.catid=c.catid);
CNAME
--------------------
snacks
5)1 select *
2 from product where catid is null
no rows selected.
SNAME CITY
-------------------- --------------------
rahul pune
prasad nashik
Table altered.
desc salesman;