sql queries
sql queries
7--update comm if comm is ther then sal*20/100,if comm is null then 5000 ?
12--wich emp r completinp 50years of exp in year of 2035 feb 10th 10:10min ?
select *from dept where deptno in(select deptno from emp group by deptno having
count(*) in (select max(count(*))from emp group by deptno))
select * from emp where sal in(select sal from emp group by sal having count(*)
in (select max(count(*)) from emp group by sal))
23--to display first and last char in between *** should be print ?
24--update emp comm ,if emp getting comm give same value, if not give 1000 ?
31--to display month 1st in one col and month last in one col ?
select add_months(trunc(sysdate,'rrrr'),(level-1))mon_beg,
last_day(add_months(trunc(sysdate,'yyyy'),(level-1)))mon_end from dual
connect by level<=12
select * from emp where deptno=10 and sal>(select min(sal) from emp where
deptno=20)
--OR--
select * from emp where deptno=10 and sal>some(select sal from emp where
deptno=20)
35--write a query to display india first and remaining counts in alpa order ?
36-- display emp details which emp is manager of any other employ ?
37--display emp details , which should not be mgr for any other employs
-- in not in case if sub query return null value , outer query will not work ?
select * from emp where empno not in (select nvl(mgr,0) from emp)
select * from emp where empno not in (select mgr from emp where mgr is not null)
38--display dept details which dept sum(sal) more than other deptn ?
select * from dept where deptno in(select deptno from emp group by deptno having
sum(sal)in
(select max(sum(sal)) from emp group by deptno))
39-- display emp which are working as james in manager departnent ?
SELECT * FROM EMP WHERE DEPTNO IN (SELECT DEPTNO FROM EMP WHERE EMPNO=
(SELECT MGR FROM EMP WHERE ENAME='JAMES'))
SELECT * FROM EMP WHERE SAL IN (SELECT MAX(SAL) FROM EMP GROUP BY DEPTNO)
SELECT * FROM EMP WHERE EMPNO=(SELECT MGR FROM EMP WHERE EMPNO=(select mgr from
emp where ename='ALLEN'))
select * from emp where sal in (select max(sal) from emp group by deptno)
59--dispaly table name, column name,created date , coloum position, data type ,
-- precision,scale ?
select c.TABLE_NAME,u.CREATED,c.COLUMN_NAME,c.DATA_TYPE,
nvl(c.DATA_PRECISION, c.DATA_LENGTH) PRECISION,
nvl(c.DATA_PRECISION, c.DATA_LENGTH),nvl(c.DATA_SCALE, 0) scale
from cols c, user_objects u where c.TABLE_NAME = u.OBJECT_NAME
begin
DBMS_UTILITY.compile_schema('bhaskar');
END;
74-- DISPLAY MAX SAL AND MIN SAL EMP DETAILS BY ROW WISE ?
76--display empee number and name for emp working as clerk and
--earning high sal among the clerks ?
select ename,empno,JOB from emp where job='CLERK' and sal in(select max(sal)
from emp where job='CLERK')
77--display name of salesmen who earns salary more than the highest salary
of the clerk ?
78-display name of CLERK who earns salary more than the lowest salary of the
SALESMEN?
select ename from emp where job='CLERK' and sal>(select min(sal) from emp where
job='SALESMEN')
79--display the names of emp who earns a salary more than of jones or greater
than that of scott?
select ENAME from emp where sal >
(select MAX(SAL) from emp where ename IN ('JONES', 'SCOTT'))
select * from emp where sal in (select max(sal) from emp group by deptno)
select * from emp where sal in (select max(sal) from emp group by job)
84--DISPLAY JOB GROUPS HAVING TOTAL SAL GREATER THAN THE MAXIMUM SALARY FOR
MANAGER?
86--display the names of emp from depment no 10 with sal greather than of all
employee working in other deptmoent ?
select job from emp where deptno=10 union select job from emp where deptno=20
minus (select job from emp where deptno=10 intersect
select job from emp where deptno=20)
108-- display emp who do not have any person working under him ?
select * from emp where empno not in (select mgr from emp where mgr is not null)
111--DISPLAY EMP NAME WHO NAME CONTAINS NOT LESS THAN 4 CHAR ?
SELECT * FROM EMP WHERE LENGTH(ENAME)>= 4
112--NAME OF EMP WHOSE NAME START WITH S AND LOC ENDS WITH K ?
114-- DISPALY EMP WHOSE SAL = 3000 AFTER GIVING 20% INCREMENT ?
117--DISPAY EMP ,EMPNO,DEPtNO,COMM WHOSE SAL BETWEEN 2000-5000 AND LOC IN CHICAGO
?
119--DISPLAY EMP WHO WORKS SAME DEPT WHERE HIS MANAGER IS WORKS ?
121--DISPLAY THE GRADE AND EMPEE NAME FOR DEPTNO 10 OR 30 BUT GRADE IS NOT 4
WHILE
--JOINED THE COMPANY BEFORE 21-DEC-82?
123--DELETE EMP WHO JOINED BEFOR 31-DEC-82 WHILE DEPT LOC NEW YORK AND CHICAGO ?
DELETE FROM EMP WHERE HIREDATE>'31-DEC-82' AND DEPTNO IN (SELECT D.DEPTNO FROM
DEPT_INFO D WHERE D.LOC IN ('CHICAGO','NEW YORK')
124--display job , dept name, ename, loc, for all who are working as manager ?
126--display employee name, job, dept name , his manager name and his grade
--.display department wise?
127--list out all the employee name,job ,sal ,salary,grade and dept name for
--every one in a company expect 'clerk' .sort on salary display the highest
salary?
128--display employee name,job and his manager .display also employees who are
--without manager?
131--display those employee whose salary is equal to average of max and min sal ?
133--display dname where atlest three are working and display only deptno ?
135--DISPLAY NAMES OF MANAGER WHOSE SAL IS MORE THAN AVERAGE OF HIS EMPLOYEES?
SELECT E.ENAME FROM EMP E WHERE E.SAL > (SELECT AVG(E1.SAL) FROM EMP E1
WHERE E1.MGR=E.EMPNO)
137-- DISPLAY THOSE EMP WHOSE SALARY IS LESS THAN HIS MANAGER BUT MORE THAN
-- SALARY OF OTHER MANAGERS ?
141--display manager who are not working under president but they are working
under
--any other manager?
DELETE FROM DEPT_INFO WHERE DEPTNO NOT IN (select DEPTNO from EMP )
143--DELETE THOSE RECORDS FROM EMP TABLE WHOSE DEPTNO NOT AVALIABLE IN DEPT TABLE
?
DELETE FROM EMP WHERE DEPTNO NOT IN (SELECT DEPTNO FROM DEPT_INFO)
144--DISPLAY THOSE NAMES WHOSE SALARY IS OUT OF GRADE AVALIABLE IN SALGRADE TABLE
?
SELECT ENAME FROM EMP WHERE SAL < (SELECT MIN(LOSAL) FROM SALGRADE)
OR SAL>(SELECT MAX(HISAL) FROM SALGRADE)
152--dispaly emp whose sal is less than (first 2 characters from hiredate
--combined with last 2 characters of sal) ?
153--dispaly those emploo whose 10% of salay is equal to the year joining ?
select * from emp e WHERE DEPTNO IN (select D.DEPTNO from dept_info d where
e.deptno=d.deptno and d.dname in ('SALES','RESEARCH') )
select E.ENAME,S.GRADE from EMP E ,SALGRADE S WHERE E.ENAME = 'JONES' AND E.SAL
BETWEEN S.LOSAL AND S.HISAL
156--DISPLAY EMPEE WHO JOINED THE COMPANY BEFORE 15TH OF THE MONTH ?
157--DISPALY EMPEE WHO JOINED BETWEEN 15TH AND 20TH OF THE MONTH?
DELETE FROM EMP WHERE DEPTNO IN (SELECT DEPTNO FROM DEPT_INFO GROUP BY
DEPTNO HAVING COUNT(DEPTNO)<3)
159--DELETE FROM EMP WHO JOINED THE COMPANY 10 YEARS BACK FROM TODAY ?
163--DISPLAY NAMES OF DEPT THOSE EMPEE WHO JOINED THE COMPANY ON THE SAME DATA ?
164--DISPLAY THOSE EMPLOYEE WHOSE GRADE IS EQUAL TO ANY NUMBER OF SAL BUT NOT
--EQUAL TO FIRST NUMBER OF SAL ?
167-- DISPALY THE MANAGER WHO HAVING MAX NO OF EMPLOYEE WORKING UNDER HIM ?
168--LIST OUT THE EMPEE NAMR AND SAL INCRESED BY 15% AND
--EXPRESS AS WHOLE NUMBER OF DOLLERS ?
171--PRINT LIST OF EMPEE DISPLAY 'JUST SALARY ' IF SAL MPRE THAN 1500,
--IF EXACTLY 1500 DISPLAY 'ON TARGET', IF LESS THAN 1500 DISPALY BELOW 1500?
SELECT ENAME,(CASE WHEN SAL > 1500 THEN 'JUST SALARY' WHEN SAL=1500 THEN
'ON TARGET' WHEN SAL<1500 THEN 'BELOW 1500' END) FROM EMP
172--GIVEN A STRING OF FORMAT 'NN/NN' ,VERFY THAT THE FIRST AND LAST 2 CHARACTER
--ARE NUMBERS . AND THAT THE MIDDLE CHARACTER IS '/' PRINT THE EXPRESSION 'YES'
--IF VALID 'NO' OF NOT VALID . USE THE FOLLOWING VALUES TO TEST YOYR SOLUTION
--'12/54' , 01/1A , '99/98' ?