Tuesday, July 15, 2014

SQL QUARIES

SQL QUARIES:



1.  Display all the information of the EMP table?
select * from emp

2.  Display unique Jobs from EMP table?
select distinct(job) from emp

3.  List the emps in the asc order of their Salaries?
select * from emp order by sal asc

4.  List the details of the emps in asc order of the Dptnos and desc of Jobs?
select * from emp order by deptno asc,job desc

5.  Display all the unique job groups in the descending order?
select distinct(job) from emp order by job desc

6.  Display all the details of all Mgrs?
select * from emp where job='manager'

7.  List the emps who joined before 1981.
select * from emp where hiredate >'1981'

8.  List the Empno, Ename, Sal, Daily sal of all emps in the asc order of Annsal.
select empno,ename,sal,sal/30,sal*12 as annsal from emp order by annsal asc

9.  Display the Empno, Ename, job, Hiredate, Exp of all Mgrs
select empno,ename,job,hiredate,datediff(yy,hiredate,getdate())as exps from emp
where job='manager'

10. List the Empno, Ename, Sal, Exp of all emps working for Mgr 7369.
select empno,ename,sal,datediff(yy,hiredate,getdate())as exps from emp
 WHERE mgr='7369'

11. Display all the details of the emps whose Comm. Is more than their Sal.
select * from emp where comm >sal

12. List the emps in the asc order of Designations of those joined after the second half of 1981.
SELECT * FROM EMP WHERE DATEPART(Q,HIREDATE)=2 AND DATEPART('YYYY',HIREDATE)='1981'

13. List the emps along with their Exp and Daily Sal is more than Rs.100.
select *,sal/30 as dailysal,datediff(yy,hiredate,getdate()) as exp from emp
where sal/30 >100

14. List the emps who are either ‘CLERK’ or ‘ANALYST’ in the Desc order.
select * from emp where job in('clerk','analyst') order by job desc

15. List the emps who joined on 1-MAY-81,3-DEC-81,17-DEC-81,19-JAN-80 in asc order of seniority.
select * from emp where hiredate in('05-01-81','12-03-81','12-17-81','1-19-80')
order by hiredate asc

16. List the emp who are working for the Deptno 10 or20.
select * from emp where deptno in(10,20)

17. List the emps who are joined in the year 81.
select * from emp where datepart(yyyy,hiredate)= '1981'

18. List the emps who are joined in the month of Aug 1980.
 select * from emp where datepart(yyyy,hiredate)= '1980' and datepart(mm,hiredate)='08' 

19. List the emps Who Annual sal ranging from 22000 and 45000.
select *,sal*12 from emp where sal*12 between 22000 and 45000

20. List the Enames those are having five characters in their Names.
select * from emp where len(ename)=5

21. List the Enames those are starting with ‘S’ and with five characters.
select * from emp where ename like 's%'

22. List the emps those are having four chars and third character must be ‘r’.
select * from emp where len(ename)=4 and ename like'__r%'

23. List the Five character names starting with ‘S’ and ending with ‘H’.
select * from emp where ename like 's___h%'

24. List the emps who joined in January.
select * from emp where datepart(mm,hiredate)='01'

25. List the emps who joined in the month of which second character is ‘a’.
select * from emp where hiredate like '_a%'

26. List the emps whose Sal is four digit number ending with Zero.
select * from emp where sal like '___0%'

27. List the emps whose names having a character set ‘ll’ together.
select * from emp where ename like '%LL%'

28. List the emps those who joined in 80’s.
select * from emp where datepart(yyyy,hiredate)='1980'

29. List the emps who does not belong to Deptno 20.
select * from emp where deptno <> 20

30. List all the emps except ‘PRESIDENT’ & ‘MGR” in asc order of Salaries.
select * from emp where job not in ('manager' ,'president') order by sal asc
           
31. List all the emps who joined before or after 1981.
select * from emp where datepart(yyyy,hiredate) <> '1981'

32. List the emps whose Empno not starting with digit78.
select * from emp where empno not like'78%'

33. List the emps who are working under ‘MGR’.
select * from emp where mgr in(select empno from emp where job='manager')

34. List the emps who joined in any year but not belongs to the month of March.
select * from emp where datepart(mm,hiredate)<>'03'

35. List all the Clerks of Deptno 20.
select * from emp where deptno=20 and job='clerk'
           
36. List the emps of Deptno 30 or 10 joined in the year 1981.
select * from emp where deptno in(10,30)and datepart(yyyy,hiredate) = 1981
           
37. Display the details of SMITH.
select * from emp where ename='smith'

38. Display the location of  SMITH.
select * from emp e , dept d  where ename='smith' and e.deptno=d.deptno  --or
select * from emp e inner join dept d on e.deptno=d.deptno where ename='smith'

39. List the total information of EMP table along with DNAME
and Loc of all the emps Working Under ‘ACCOUNTING’ & ‘RESEARCH’ in the asc Deptno.
select e.*,d.dname,d.loc from emp e inner join dept d on e.deptno=d.deptno and
 d.dname in('accounting','research') order by deptno asc       --or
SELECT * FROM EMP E,DEPT D WHERE D.DNAME IN('ACCOUNTING','RESEARCH')
AND E.DEPTNO=D.DEPTNO ORDER BY E.DEPTNO ASC;

40. List  the  Empno,  Ename,  Sal,  Dname  of  all  the  ‘MGRS’  and  ‘ANALYST’ working
in New York, Dallas with an exp more than 7 years without receiving the Comm asc order of Loc.
select e.empno,e.ename,e.sal,d.dname,d.loc from emp e inner join dept d on e.deptno=d.deptno
 where job in('manager','analyst')
 and d.loc in('newyork','dallas')
 and datediff(yy,hiredate,getdate())>7
 and comm is null order by d.loc asc             

41. Display the Empno, Ename, Sal, Dname, Loc, Deptno, Job of all emps working at
ChICAGO or working for ACCOUNTING dept with Ann Sal>28000, but the Sal should
not be=3000 or 2800 who doesn’t belongs to the Mgr and whose no is having a digit ‘7’ or ‘8’ in
3rd  position in the asc order of Deptno and desc order of job.
select e.*,d.* from emp e,dept d where (d.loc ='chicago' or d.dname ='ACCOUNTING')
and e.deptno=d.deptno
and e.sal*12>28000 and (e.sal <>2800 or e.sal <>3000)
and e.JOB NOT IN ('MANAGER')
and (e.empno like'__7_' and e.empno like'__8_')
order by e.deptno asc,e.job desc

42. Display the total information of the emps along with Grades in the asc order.
SELECT *FROM EMP E,SALGRADE S WHERE E.SAL BETWEEN S.losal AND S.hisal ORDER BY GRADE ASC;

43. List all the Grade2 and Grade 3 emps.
SELECT *FROM EMP E,SALGRADE S WHERE E.SAL BETWEEN S.losal AND S.hisal AND S.GRADE IN(2,3)

44. Display all Grade 4,5 Analyst and Mgr.
SELECT *FROM EMP E,SALGRADE S,DEPT D WHERE E.SAL BETWEEN S.losal AND S.hisal AND S.GRADE IN(4,5)
AND  JOB IN('MANAGER','Analyst')

45. List the Empno, Ename, Sal, Dname, Grade, Exp, and Ann Sal of emps working for Dept10 or20.
SELECT E.*,D.DNAME,S.GRADE,E.SAL*12 AS ANNSAL,DATEDIFF(YYYY,HIREDATE,GETDATE()) AS YEARS
FROM EMP E,DEPT D,SALGRADE S where e.deptno in (10,20) and
E.deptno = D.deptno and E.sal between S.losal and S.hisal ;

46. List all the information of emp with Loc and the Grade of all the emps belong to the Grade
range from 2 to 4 working at the Dept those are not starting with char set ‘OP’ and not
ending with ‘S’ with the designation having a char ‘a’ any where joined in
the year 1981 but not in the month of Mar or Sep and Sal not end with‘00’ in the asc order of Grades?
SELECT *FROM EMP E,SALGRADE S,DEPT D WHERE E.DEPTNO=D.DEPTNO AND
E.SAL BETWEEN S.losal AND S.hisal AND S.GRADE IN(2,3,4) AND
(D.DNAME NOT LIKE'OP%' AND D.DNAME NOT LIKE'%S') AND
 empno in (select empno from emp where job like '%A%')and
sal not like '' and
 datepart(yyyy,hiredate)= '1981' AND
 datepart(mm,hiredate) NOT IN ('03','09') ORDER BY S.GRADE ASC

47. List the details of the emps whose Salaries more than the employee BLAKE.
SELECT * FROM EMP WHERE SAL >(SELECT SAL FROM EMP WHERE ENAME='BLAKE')

48. List the emps whose Jobs are same as ALLEN.
select * from emp where job=(select job from emp where ename='allen')

49. List the emps who are senior to King.
SELECT * FROM EMP WHERE HIREDATE <(SELECT HIREDATE FROM EMP WHERE ENAME='KING')

50. List the Emps of Deptno 20 whose Jobs are same as Deptno10.
SELECT * FROM EMP WHERE DEPTNO=20 AND JOB IN(SELECT JOB FROM EMP WHERE DEPTNO=10)

51. List the Emps whose Sal is same as FORD or SMITH in desc order of Sal.
SELECT * FROM EMP WHERE SAL in (select sal from emp where ename='ford' or ename ='smith')
 ORDER BY SAL DESC;

52. List the emps Whose Jobs are same as MILLER or Sal is more than ALLEN.
SELECT * FROM EMP WHERE  job=(select JOB from emp where ename='miller' )or
sal>(SELECT SAL FROM EMP WHERE ENAME='allen')

53. List the Emps whose Sal is > the total remuneration of the SALESMAN.
SELECT MAX(SAL) FROM EMP WHERE SAL>
(SELECT SUM((SAL+ISNULL(COMM,0))) FROM EMP WHERE JOB='SALESMAN')

54. List the emps who are senior to BLAKE working at CHICAGO & BOSTON.
SELECT * FROM EMP ,DEPT  WHERE EMP.HIREDATE <
(SELECT EMP.HIREDATE FROM EMP WHERE EMP.ENAME ='BLAKE') AND
DEPT.LOC IN('CHICAGO','BOSTON') AND EMP.DEPTNO=DEPT.DEPTNO

55. List the Emps of Grade 3,4 belongs to the dept ACCOUNTING and RESEARCH whose
Sal is more than ALLEN and exp more than SMITH in the asc order of EXP.
SELECT * FROM EMP E WHERE E.DEPTNO IN(SELECT D.DEPTNO FROM DEPT D WHERE
D.DNAME IN('ACCOUNTING','RESEARCH'))AND
E.SAL >(SELECT SAL FROM EMP WHERE ENAME='ALLEN') AND
E.HIREDATE<(SELECT HIREDATE FROM EMP WHERE ENAME='SMITH') AND
E.EMPNO IN(SELECT E.EMPNO FROM EMP E,SALGRADE S WHERE E.SAL BETWEEN S.LOSAL AND
S.HISAL AND s.grade in (3,4) ) order by hiredate desc

56. List the emps whose jobs same as SMITH or ALLEN.
SELECT * FROM EMP WHERE JOB IN(SELECT JOB FROM EMP WHERE ENAME ='SMITH'OR ENAME='ALLEN')

57. Any jobs of deptno 10 those that are not found in deptno 20.
SELECT * FROM EMP WHERE DEPTNO=10 AND JOB NOT IN(SELECT JOB FROM EMP WHERE DEPTNO=20)

58. Find the highest sal of EMP table.
SELECT MAX(SAL) FROM EMP

59. Find details of highest paid employee.
SELECT * FROM EMP WHERE SAL IN(SELECT MAX(SAL) FROM EMP)

60. Find the highest paid employee of sales department.
SELECT * FROM EMP WHERE SAL IN(SELECT MAX(SAL) FROM EMP WHERE JOB='SALESMAN')

61. List the most recently hired emp of grade3 belongs to  location CHICAGO.
select * from emp e,dept d where d.loc='CHICAGO' and hiredate in
(select max(hiredate)  from emp e,salgrade s
where sal between losal and hisal and grade=3) 

62. List the employees who are senior to most recently hired employee working under king.
  select * from emp where hiredate <(select max(hiredate) from emp where mgr in
  (select empno from emp where ename='king'))

  select top(1) ename from emp where hiredate<(select
  hiredate from emp where ename='king')order by
 hiredate desc

63. List the details of the employee belongs to newyork with grade 3 to 5 except
'PRESIDENT’ whose sal> the highest paid employee of Chicago in a group where
there is manager and salesman not working under king?
select * from emp where deptno in (select deptno from dept where dept.loc ='NEW YORK') and empno in
 (select empno from emp e,salgrade s where e.sal between s.losal and s.hisal and s.grade in (3,4,5) ) and job !=
 'PRESIDENT' and sal >(select max(sal) from emp where deptno in (select deptno from dept where dept.loc = 'CHICAGO')
  and job in ('MANAGER','SALESMAN') and mgr not in (select empno from emp where ename='KING'))

64. List the details of the senior employee belongs to 1981.
SELECT * FROM EMP WHERE HIREDATE IN
(SELECT MIN(HIREDATE) FROM EMP WHERE DATEPART(YYYY,HIREDATE) = '1981')

--65. List the employees who joined in 1981 with the job same as the most senior person of the year 1981.
SELECT * FROM EMP WHERE JOB IN(SELECT JOB FROM EMP WHERE HIREDATE IN
(SELECT MIN(HIREDATE) FROM EMP WHERE DATEPART(YYYY,HIREDATE) = '1981'))

66. List the most senior empl working under the king and grade is more than 3.
SELECT * FROM EMP where hiredate in (select hiredate from emp where empno in
(select empno from emp e ,salgrade s where e.sal between s.losal and s.hisal and s.grade in (4,5)))
and mgr in (select empno from emp where ename = 'KING');

67. Find the total sal given to the MGR.
select sum(sal) from emp where job='manager'

68. Find the total annual sal to distribute job wise in the year 81.
select job,sum(sal*12)as tsal from emp where datepart(yyyy,hiredate)='1981' group by job

69. Display total sal employee belonging to grade 3.
select sum(sal) from emp where empno in(select empno from emp e,salgrade s where e.sal
between s.losal and s.hisal and s.grade=3)

70. Display the average salaries of all the clerks.
select avg(sal) from emp where job='clerk'

71. List the employeein dept 20 whose sal is >the average sal 0f dept 10 emps.
select * from emp where deptno=20 and sal>(select avg(sal) from emp where deptno=10)
           
72. Display the number of employee  for each job group deptno wise.
select count(*),job,deptno from emp group by job,deptno

73. List the manager no and the number of employees working for those mgrs in the ascending Mgrno.
select e.mgr,count(*) from emp e,emp m where e.mgr=m.empno group by e.mgr order by e.mgr    asc;

74. List the department,details where at least two emps are working
 SELECT DEPTNO,COUNT(*) FROM EMP GROUP BY DEPTNO HAVING COUNT(*)>2;

75. Display the Grade, Number of emps, and max sal of each grade.
select s.grade,count(*) as cnt,max(sal) as sals from emp e,salgrade s where
  e.sal between s.losal and s.hisal group by s.grade

76. Display dname, grade, No. of emps where at least two emps are clerks.
select d.dname,s.grade,count(*) from emp e,dept d,salgrade s where
  e.deptno=d.deptno and e.job='clerk' and e.sal between s.losal and s.hisal
  group by d.dname,s.grade having count(*)>=2

77. List the details of the department where maximum number of emps are working.
select * from dept where deptno in (select deptno from emp group by deptno
having count(*) in (select max(count(*)) from emp group by deptno) )

78. Display the emps whose manager name is jones.
 select * from emp where mgr in
(select empno from emp where ename = 'JONES')

79. List the employees whose salary is more than 3000 after giving 20% increment.
select * from emp where (sal*0.2) >3000

80. List the emps with dept names.
select e.*,d.dname from emp e,dept d where e.deptno=d.deptno

81. List the emps who are not working in sales dept.
select * from EMP where DEPTNO NOT in(select DEPTNO from dept WHERE dNAME='SALES')
           
82. List the emps name ,dept, sal and comm. For those whose salary is between 2000
and 5000 while loc is Chicago.
select e.ename,d.deptno,e.sal,e.comm from emp e,dept d where e.deptno=d.deptno
and d.loc='chicago' and e.sal between 2000 and 5000

83. List the emps whose sal is greater than his managers salary
 select * from emp w,emp m where w.mgr = m.empno and w.sal > m.sal

84. List the grade, EMP name for the deptno 10 or deptno 30 but sal grade is not
4 while they joined the company before ’31-dec-82’.
select s.grade,e.ename,e.hiredate from emp e,salgrade s where e.sal between s.losal and s.hisal
and e.deptno in(10,30) and s.grade not in(4) and hiredate<'12/31/82'

85. List the name ,job, dname, location for those who are working as MGRS.
select e.ename,e.job,d.dname,d.loc from emp e,dept d where e.deptno=d.deptno and empno in
(select empno from emp where job='manager')

86. List the emps whose mgr name is jones and also list their manager name
select w.*,m.ename from emp w,emp m where w.mgr=m.empno and m.ename='jones'

87. List the name and salary of ford if his salary is equal to hisal of his grade.
select e.*,s.* from emp e,salgrade s where ename='jones' and e.sal between s.losal and s.hisal
and e.sal =s.hsial

88. Lit the name, job, dname ,sal, grade dept wise
select e.ename,e.job,d.dname,e.sal,s.grade from emp e,dept d,salgrade s where e.deptno=d.deptno
and e.sal between s.losal and s.hisal order by e.deptno

89. List the emp name, job, sal, grade and dname except clerks and sort on the basis of highest sal
select e.ename,e.job,d.dname,e.sal,s.grade from emp e,dept d,salgrade s where e.deptno=d.deptno
and e.sal between s.losal and s.hisal and e.job not in('clerk') order by e.sal desc

90. List the emps name, job  who are with out manager.
select e.ename,e.job from emp e where mgr is null

91. List the names of the emps who are getting the highest sal dept wise.
select * from emp where sal in(select max(sal) from emp group by deptno)

92. List the emps whose sal is equal to the average of max and minimum
select avg(sal) from emp where sal=(select max(sal)+min(sal)/2 from emp)
           
93. List the no. of emps in each department where the no. is more than 3.
select count(*),deptno from emp group by deptno having count(*)>3

94. List the names of depts. Where atleast 3 are working in that department.
select d.dname,count(*) from emp e ,dept d where e.deptno = d.deptno
group by d.dname having count(*) >= 3

95. List the managers whose sal is more than his employess avg salary.
 select * from emp m where m.empno in(select mgr from emp) and m.sal>
 (select avg(e.sal) from emp e where e.mgr=m.empno)


96. List the name,salary,comm. For those employees whose net pay is greater than
or equal to any other employee salary of the company.
select ename,sal,comm from emp where (sal+isnull(comm,0)) >= any (select sal from emp)

97. Find out least 5 earners of the company.
select * from emp e where 5>(select count(*) from emp e where e.sal>sal)

98. Find out emps whose salaries greater than salaries of their managers.
select * from emp w,emp m where w.mgr=m.empno and w.sal>m.sal

99. List the managers who are not working under the president.
select * from emp where empno in(select mgr from emp) and mgr not in
 (select empno from emp where job = 'PRESIDENT')

100. List the records from emp whose deptno isnot in dept.
select * from dept where deptno not in(select deptno from emp)

101. List the Enames who are retiring after 31-Dec-89 the max Job period is 20Y.
select ename,hiredate from emp where hiredate <'12/31/89'

102. List those Emps whose Salary is odd value.

103. List the emp’s whose Salary contain 3 digits.
select * from emp where len(sal)=3

104. List the emps who joined in the month of DEC.
select * from emp where datepart(mm,hiredate)='12'

105. List the emps whose names contains ‘A’.
select * from emp where ename like'%a%'

106. List the emps Whose 10% of Salary is equal to year of joining.
select * from emp where datepart(yyyy,hiredate) in (select .1*sal from emp)

107. List first 50% of chars of Ename in Lower Case and remaining are upper Case.
select lower(substring(ename,1,round(len(ename)/2))) || substring(ename,round(len
(ename)/2)+1,len(ename))from emp

108. List the Dname whose No. of Emps is =to number of chars in the Dname.
select * from dept d where len(dname) in
(select count(*) from emp e where e.deptno = d.deptno )   --or
select d.dname,count(*) from emp e ,dept d where e.deptno = d.deptno
group by d.dname having count(*) = len (d.dname);

109. List the emps those who joined in company before 15th of the month.
select * from emp where datepart(dd,hiredate)<'15'

110. List the Dname, no of chars of which is = no. of emp’s in any other Dept.
select * from dept d where len(dname) in
(select count(*) from emp where d.deptno <> deptno group by deptno ) --or)
select * from dept d , (select count(*) s,e.deptno "M"from emp e group by e.deptno) d1
where len(dname)=d1.s and d1.M <>d.deptno;

111. List the emps who are working as Managers.
select * from emp where job = 'MANAGER'

112. List THE Name of dept where highest no.of emps are working.
select dname from dept where deptno in(select deptno from emp group by deptno
having count(*) in (select max(count(*)) from emp group by e.deptno)) --or

select dname from dept where deptno in
(select deptno from emp group by deptno
having count(*) in
(select max(count(*)) from emp group by deptno) );


113. List the emps who joined in the company on the same date.
select * from emp e where hiredate in
(select hiredate from emp where e.empno <> empno)

114. List the details of the emps whose Grade is equal to one tenth of Sales Dept.
select *  from emp e ,salgrade s where e.sal between s.losal and s.hisal and s.grade =0.1*
(select deptno from dept where dname='SALES')

115. List the name of the dept where more than average no. of emps are working.
select d.dname from dept d, emp e where e.deptno = d.deptno
group by d.dname
having count(*) > (select count(*),deptno from emp group by deptno); --wrong

116. List the Managers name who is having max no.of emps working under him.

select m.ename,count(*) from emp w,emp m
where w.mgr = m.empno
group by m.ename
having count(*) = (select max(count(*)) from emp group by mgr);
(OR)
B) select * from emp where empno = (select mgr from emp group by mgr having
 count(*) = (select max(count(*)) from emp group by mgr))


117) Print a list of emp’s Listing ‘just salary’ if Salary is more than 1500, on target if
Salary is 1500 and ‘Below 1500’ if Salary is less than 1500.
select empno,ename,sal,job,case
When sal =1500 then'ON TARGET'
WHEN SAL <1500 THEN 'BELOW 1500'
wHEN SAL>1500 THEN 'JUST SALARY'
ELSE 'NOTHING'
END "REVISED SALARY" FROM EMP

118) List those Managers who are getting less than his emps Salary.
SELECT * FROM EMP W WHERE EMPNO IN(SELECT MGR FROM EMP WHERE W.SAL<SAL)  --OR
select distinct m.ename,m.sal from emp w,emp m where w.mgr = m.empno and w.sal>m.sal

119) Print the details of all the emps who are sub-ordinates to Blake.
select * from emp where mgr in (select empno from emp where ename = 'BLAKE')

120) List the emps who are working as Managers using co-related sub-query.
select * from emp where empno in (select mgr from emp)

121) List the emps whose Mgr name is ‘Jones’ and also with his Manager name.
select w.ename,m.ename,(select ename from emp where m.mgr = empno) "his MANAGER"
from emp w,emp m where w.mgr = m.empno and m.ename = 'JONES' --or
 select e.ename,w.ename,m.ename from emp e,emp w,emp m where e.mgr = w.empno
 and w.ename = 'JONES' and w.mgr = m.empno

122) Define a variable representing the expression used to calculate on emps total annual
remuneration use the variable in a statement, which finds all emps who can earn 30000 a year or more.
A) Set define on
B) Define  annual = 12*nvl2(comm.,sal+comm.,sal)  (here define variable is a session variable)
C) Select * from emp where &annual > 30000;

123) Find out how may Managers are their in the company.
select count(*) from emp where job = 'MANAGER' --or
select count(*) from emp where empno in (select mgr from emp) --OR
select count(distinct m.empno) from emp w,emp m where w.mgr = m.empno

124) Find  Average  salary  and  Average  total  remuneration  for  each  Job  type.
Remember Salesman earn commission.secommm
 select avg(sal),avg(sal+ISNULL(comm,0)) from emp ;
select avg(sal),avg(sal+ISNULL(comm,0)) from emp where job='salesman'

125) Check whether all the emps numbers are indeed unique.
select empno,count(*) from emp group by empno

126) List the emps who are drawing less than 1000 Sort the output by Salary.
SELECT * FROM EMP WHERE SAL<1000 ORDER BY SAL

127) List the employee Name, Job, Annual Salary, deptno, Dept name and grade who
earn 36000 a year or who are not CLERKS.
SELECT E.ENAME,E.JOB,E.SAL*12 AS ANNUVALSAL,E.DEPTNO,D.DNAME,S.GRADE FROM EMP E,DEPT D,
SALGRADE S WHERE E.DEPTNO=D.DEPTNO
AND E.SAL BETWEEN S.LOSAL AND S.HISAL AND E.JOB <>'CLERK' AND E.SAL*12>=36000

128) Find out the Job that was filled in the first half of 1983 and same job
that was filled during the same period of 1984.
select * from emp where DATEPART(MM,HIREDATE) <= 06 and DATEPART(YY,HIREDATE) = 1984 and job in
 (select job from emp where DATEPART(MM,HIREDATE) <= 06 and DATEPART(YY,HIREDATE) <= 1983)

129) Find out the emps who joined in the company before their Managers.
select * from emp w,emp m where w.mgr = m.empno and
w.hiredate< m.hiredate   --OR
 select * from emp e where hiredate < (select hiredate from emp where empno = e.mgr)

130) Find all the emps who earn the minimum Salary for each job wise in ascending order.
select * from emp where sal in(select min(sal) from emp group by job) order by sal asc

131) Find  out  all  the  emps  who  earn  highest  salary  in  each  job  type.
Sort  in descending salary order.
select * from emp where sal in(select max(sal) from emp group by job) order by sal desc

132) Find out the most recently hired emps in each Dept order by Hiredate.
select * from emp e where hiredate in(select max(hiredate) from emp where e.deptno=deptno )
order by hiredate asc

133) List the employee name,Salary and Deptno for each employee who earns a salary
greater than the average for their department order by Deptno.           
select * from emp e where sal>(select avg(sal) from emp e where e.deptno=deptno)order by deptno asc

134) List the Deptno where there are no emps.
select deptno,count(*) from emp group by deptno having count(*)=0
           
135) List the No.of emp’s and Avg salary within each department for each job.
select count(ename),avg(sal),deptno,job from emp group by deptno,job

136) Find the maximum average salary drawn for each job except for ‘President’.
select max(avg(sal)) from emp where job not in('president') group by job    --wrong
select max(sal),avg(sal),job from emp where job not in('president') group by job

137) Find the name and Job of the emps who earn Max salary and Commission.
select * from emp where sal = (select max(sal) from emp) and comm is not null
           
138) List  the  Name,  Job  and  Salary  of the  emps  who  are  not  belonging  to  the department 10
but who have the same job and Salary as the emps of dept 10.
select ename,job,sal from emp where deptno <>'10' and job in(select job from emp where deptno=10)
and sal in(select sal from emp where deptno=10)

139) List the Deptno, Name, Job, Salary and Sal+Comm of the SALESMAN who are earning
maximum salary and commission in descending order.
select deptno,sal,(sal+isnull(comm,0)) as tsal,job,comm from emp where job='salesman'
and sal in(select max(sal+isnull(comm,0)) from emp where comm is not null)order by (sal+isnull(comm,0)) desc

140) List the Deptno, Name, Job, Salary and Sal+Comm of the emps who earn the
second highest earnings (sal + comm.).
select top(2) sal ,deptno,ename,job,(sal+isnull(comm,0))as tsal from emp

select deptno,ename,sal,job,(sal+isnull(comm,0))as tsal from emp e where 2 =
(select count(distinct (sal+isnull(comm,0)) )
from emp where (e.sal+isnull(comm,0))<(e.sal+isnull(comm,0)))         

141) List the Deptno and their average salaries for dept with the average salary less than
the averages for all department
select deptno,avg(sal) from emp group by deptno
having avg(sal) <(select avg(Sal) from emp)

142)list the Names and Salaries of the emps along with their manager names
and salaries for those emps who earn more salary than their Manager.
select w.ename,w.sal,m.ename,m.sal from emp w,emp m
where w.mgr = m.empno and w.sal > m.sal

143) list t the Name, Job, Salary of the emps in the department with the highest average salary.
select * from emp where deptno in (select deptno from emp e having avg(sal)=
(select max(avg(sal)) from emp group by deptno ))group by deptno   --wrong

144List the empno,sal,comm. Of emps.
select empno,sal,comm from emp

145List the details of the emps in the ascending order of the sal.
select * from emp order by sal asc

146List the dept in the ascending order of the job and the desc order of the emps print empno, ename.
select * from emp order by job asc,empno desc

147Display the unique dept of the emps.
select * from dept where deptno in (select distinct(deptno) from emp)

148Display the unique dept with jobs.
select distinct(job),deptno from emp

149Display the details of the blake.
select * from emp where ename='blake'


150) List all the clerks.
select * from emp where job='clerk'

151) list all the employees joined on 1st may 81.
select * from emp where hiredate ='05/01/1981'

152) List the empno,ename,sal,deptno of the dept 10 emps in the ascending order of salary.
select * from emp where deptno=10 order by sal asc

153) List the emps whose salaries are less than 3500.
select * from emp where sal<3500 order by sal desc

154) List the empno,ename,sal of all the emp joined before 1 apr 81.
select * from emp where hiredate <'04/01/1981'

155) List the emp whose annual sal is <25000 in the asc order of the salaries.
select *,sal*12 as tsal from emp where sal*12 <25000 order by sal asc

156) List the empno,ename,annsal,dailysal  of all the salesmen in the asc ann sal
select *,sal/30 as dailysal,sal*12 as annuvalsal from emp where job='salesman' order by sal*12 asc
           
157) List the empno,ename,hiredate,current date & exp in the ascending order of the exp.
select hiredate,empno,ename,getdate() as todaydate,datediff(yy,hiredate,getdate()) as years from emp order by
datediff(yy,hiredate,getdate()) asc

158) List the emps whose exp is more than 10 years.
select * from emp where datediff(yy,hiredate,getdate())>10

159) List the emps who are working as managers.
select * from emp where job='manager'

160) List the emps who are either clerks or managers.
select * from emp where job in('clerk','manager')

161) List the emps who have joined on the following dates 1 may 81,17 nov 81,30 dec 81
select * from emp where hiredate in ('05/01/1981','11/17/81','12/30/81')

162) List the emps who have joined in the year 1981.
select * from emp where datepart(yyyy,hiredate)='1981'

163) List the emps whose annual sal ranging from 23000 to 40000.
select sal*12,* from emp where sal*12 between 23000 and 40000

164) List the emps working under the mgrs 7369,7890,7654,7900.
select * from emp where mgr in('7369','7890','7654','7900')

165)  List all the 4char emps.
select * from emp where len(ename)=4

166) List the emp names starting with ‘M’ with 5 chars.
select * from emp where ename like'm%' and len(ename)=5

167) List the emps end with ‘H’ all together 5 chars.
select * from emp where ename like'%h' and len(ename)=5

168) List names start with ‘M’.
select * from emp where ename like'm%'

169) List the emps who joined in the year 81.
select * from emp where datepart(yyyy,hiredate)='1981'

170) List the emps whose sal is ending with 00.
select * from emp where sal like''

171) List the emp who joined in the month of JAN.
select * from emp where datepart(mm,hiredate)='01'

172) Who joined in the month having char ‘a’.
select * from emp where hiredate like'%a%'
           
173) Who joined in the month having second char ‘a’
select * from emp where hiredate like'_a%'

174)  List the emps whose salary is 4 digit number.
select * from emp where len (sal)=4

175) List the emp who joined in 80’s.
select * from emp where datepart(yyyy,hiredate)='1980'

176) List the emp who are clerks who have exp more than 8ys.
select * from emp where job='clerk' and datediff(yyyy,hiredate,getdate())>8

178) List the mgrs of dept 10 or 20.
select * from emp where deptno in(10,20) and job='manager'

179) List the emps joined in jan with salary ranging from 1500 to 4000.
select * from emp where sal between 1500 and 4000 and datepart(mm,hiredate)='01'

180) List the unique jobs of dept 20 and 30 in desc order.
select distinct(job) from emp where deptno in(20,30) order by job desc

181) List the emps along with exp of those working under the mgr whose number is starting with
7 but should not have a 9 joined before 1983.
select * from emp where (mgr like '7%' and mgr not like '%9%')  and datediff(yyyy,hiredate,getdate())<1983

182) List the emps who are working as either mgr or analyst with the salary ranging from 2000 to 5000
and with out comm.
select * from emp where job in('manager','analyst') and sal between 2000 and 5000 and comm is null

183) List the empno,ename,sal,job of the emps with /ann sal <34000 but receiving some comm.
Which should not be>sal and desg should be sales man working for dept 30.
select empno,ename,sal,job,deptno,sal*12 as annauvalsal from emp where sal*12<34000
and comm is not null and comm>sal  and job='salesman'and deptno=30
207) List the emps who are working for dept 10 or 20 with desgs as clerk or analyst with a sal is either
3 or 4 digits with an exp>8ys but does not belong to mons of mar,apr,sep and working for mgrs &no  is not ending with 88 and 56.
select * from emp where deptno in(10,20) and job in('clerk','analyst') and len (sal)in(3,4)
and datediff(yyyy,hiredate,getdate())>8 and datepart(mm,hiredate)in('03','04','09') and mgr not like
('%88')and mgr not like ('%56')

184) List the empno,ename,sal,job,deptno&exp of all the emps belongs to dept 10 or20 with an exp 6 to 10 y working under the same mgr with out comm. With a job not ending irrespective of the position with comm.>200 with exp>=7y and sal<2500 but not belongs to the month sep or nov working under the mgr whose no is not having digits either 9 or 0 in the asc dept& desc dept
select empno,ename,sal,job,deptno,datediff(yyyy,hiredate,getdate()) as exp from emp where deptno in(10,20)
and datediff(yyyy,hiredate,getdate()) between 6 and 10                      --wrong

185) List the details of the emps working at Chicago.
select * from emp where deptno in(select deptno from dept where dept.loc='chicago')

186) List the empno,ename,deptno,loc of all the emps.
select e.empno,e.ename,e.deptno,d.loc from emp e ,dept d where e.deptno = d.deptno

187) List the empno,ename,loc,dname of all the depts 10 and 20.
select * from emp e,dept d where e.deptno=d.deptno and e.deptno in(10,20)

188) List the empno, ename, sal, loc of the emps working at Chicago dallas with an exp>6ys.
select * from emp e,dept d where e.deptno=d.deptno and d.loc in('chicago','dallas') and
datediff(yyyy,hiredate,getdate()) >6

189) List the emps along with loc of those who belongs to dallas ,
newyork with sal ranging from 2000 to 5000 joined in 81.
select * from emp e,dept d where e.deptno=d.deptno and d.loc in('dallas','newyork') and
e.sal between 2000 and 5000 and datepart(yyyy,hiredate)='1981'

190) List the empno,ename,sal,grade of all emps.
select * from emp e,salgrade s where e.sal between s.losal and s.hisal

191) List the grade 2 and 3 emp of Chicago.
select * from emp e,dept d,salgrade s where e.deptno=d.deptno and
e.sal between s.losal and s.hisal and d.loc='chicago' and s.GRADE IN(2,3)

192) List the emps with loc and grade of  accounting dept or the locs dallas or
Chicago with the grades 3 to 5 &exp >6y
select e.deptno,e.empno,e.ename,e.sal,d.dname,d.loc,s.grade from emp e,salgrade s,dept d
where e.deptno = d.deptno and e.sal between s.losal and s.hisal
and s.grade in (3,5)
and datediff(yyyy,hiredate,getdate()) >6
and ( d.dname = 'ACCOUNTING' or D.loc in ('DALLAS','CHICAGO'))

193) List the grades 3 emps of research and operations depts joined after 1987 and
whose names should not be either miller or allen.
select e.ename from emp e ,dept d,salgrade s where e.deptno = d.deptno and
d.dname in ('OPERATIONS','RESEARCH') and e.sal between s.losal and s.hisal
and e.ename not in ('MILLER','ALLEN')
and datediff(yyyy,hiredate,getdate()) >'1987'

194) List the emps whose job is same as smith.
select * from EMP where JOB=(select JOB from EMP where ENAME='smith')

195)  List the emps who are senior to miller
select * from emp where HIREDATE<(select HIREDATE from EMP where ENAME='miller')

196) List the emps whose job is same as either allen or sal>allen.
select * from EMP where JOB=(select JOB from EMP where ENAME='allen') OR
SAL>(select sal from emp where ename='allen')

197) List the emps who are senior to their own manager.
 select * from emp w,emp m where w.mgr = m.empno and
w.hiredate < m.hiredate

198) List the emps whose sal greater than blakes sal.
select * from emp where sal<(select sal from EMP where ENAME='blake')

199) List the dept 10 emps whose sal>allen sal.
select * from emp where deptno=10 and sal>(select sal from EMP where ENAME='allen')

200) List the mgrs who are senior to king and who are junior to smith.
SELECT * FROM EMP WHERE EMPNO IN(select mgr from emp  where hiredate<(select hiredate from
emp where ename = 'KING' )  and hiredate > (select hiredate from emp
where ename = 'SMITH')) and mgr is not null

201) List the empno,ename,loc,sal,dname,loc of the all the emps belonging to king dept.
 select e.*,d.* from emp e,dept d  where e.deptno=d.deptno
and e.deptno in  (select deptno from emp where ename = 'KING'and emp.empno <> e.empno)

202) List the emps whose salgrade are greater than the grade of miller.
select * from emp e,salgrade s
where e.sal between s.losal and s.hisal and s.grade >
(select s.grade from emp e,salgrade s where e.sal between s.losal and s.hisal and e.ename = 'MILLER')

203) List the emps whose sal is same as ford or blake.
select * from emp where sal in (select sal from emp e where e.ename in
('FORD','BLAKE')and emp.empno <> e.empno)

204) List the emps whose sal is same as any one of the following.
select * from emp where sal in
(select sal from emp e where emp.empno <> e.empno)

205) Sal of any clerk of emp1 table.
select * from EMP where JOB='clerk'

206) Any emp of emp2 joined before 82.
select * from EMP where DATEpart(yyyy,hiredate)='1982'

207) The total remuneration (sal+comm.) of all sales person of Sales dept belonging to emp3 table.
select * from emp e
where (sal+isnull(comm,0)) in
(select sal+isnull(comm,0) from emp e,dept d where e.deptno=d.deptno
and d.dname = 'SALES'and e.job = 'SALESMAN')

208) ggGrade 4 emps Sal of emp 4 table.
select * from EMP e,SALGRADE s where e.SAL between s.LOSAL and s.HISAL and s.GRADE=4

209) Any emp Sal of emp5 table.
select * from emp

210) List the highest paid emp.
select * from emp where sal in (select max(sal) from emp);

211) List the details of most recently hired emp of dept 30.
 select * from emp where hiredate in  (select max(hiredate) from emp where deptno = 30)
           
212) List the highest paid emp of Chicago joined before the most  recently hired emp of grade 2.
select * from emp where sal = ( select max(sal) from emp e,dept d where e.deptno = d.deptno
and d.loc = 'CHICAGO' and hiredate <(select max(hiredate) from emp e ,salgrade s where
e.sal between s.losal and s.hisal and s.grade = 2))

213) List the highest paid emp working under king.
select * from emp where sal in  (select max(sal) from emp where mgr in

 (select empno from emp where ename = 'KING'))

1 comment: