SQL Answers For 50 Questions
SQL Answers For 50 Questions
40. List the no. of emps in each department where the no. is more than 3.
SQL
SELECT deptno, COUNT(*) AS number_of_emp
FROM EMP
GROUP BY deptno
HAVING COUNT(*) > 3;
41. List the names of depts. Where atleast 3 are working in that department.
SQL
SELECT deptno
FROM EMP
GROUP BY deptno
HAVING COUNT(*) >= 3;
42. List the managers whose sal is more than his employess avg salary
SQL
WITH dept_avg_sal AS (
SELECT deptno, AVG(sal) AS avg_sal
FROM EMP
GROUP BY deptno
)
SELECT e.empno, e.ename
FROM EMP e
INNER JOIN dept_avg_sal d ON e.deptno = d.deptno
WHERE e.job = 'MGR' AND e.sal > d.avg_sal;
43. List the emps who are working as Managers
SQL
SELECT * FROM EMP WHERE job = 'MGR';
44. List the emps who joined in the company on the same date
SQL
SELECT * FROM EMP
GROUP BY hiredate
HAVING COUNT(*) > 1;
45. Count the No.of emps who are working as ‘Managers’
SQL
SELECT COUNT(*) AS number_of_mgrs FROM EMP WHERE job = 'MGR';
46. List those Managers who are getting less than his emps Salary.
SQL
WITH dept_avg_sal AS (
SELECT deptno, AVG(sal) AS avg_sal
FROM EMP
GROUP BY deptno
)
SELECT e.empno, e.ename
FROM EMP e
INNER JOIN dept_avg_sal d ON e.deptno = d.deptno
WHERE e.job = 'MGR' AND e.sal < d.avg_sal;
47. Find out how many Managers are their in the company
SQL
SELECT COUNT(*) AS number_of_mgrs FROM EMP WHERE job = 'MGR';
48. Find all the emps who earn the minimum Salary for each job wise in
ascending order.
SQL
WITH min_sal_per_job AS (
SELECT job, MIN(sal) AS min_sal
FROM EMP
GROUP BY job
)
SELECT e.empno, e.ename, e.job, e.sal
FROM EMP e
INNER JOIN min_sal_per_job m ON e.job = m.job AND e.sal = m.min_sal
ORDER BY e.job, e.sal ASC;
49. List the departments having employees in the ‘CLERK’ grade
Assuming a 'grade' column exists in the EMP table
SQL
SELECT deptno
FROM EMP
WHERE grade = 'CLERK'
GROUP BY deptno;
50. Display the details of all employees who have a commission greater than 500
SQL
SELECT * FROM EMP
WHERE comm > 500;