sql Subquery
sql Subquery
SELECT
EMPNO 'EMPLOYEE NUMBER',
ENAME 'EMPLOYEE NAME',
DEPTNO 'DEPARTMENT NUMBER',
SAL 'SALARY',
(SELECT AVG(SAL) FROM EMP WHERE DEPTNO = E.DEPTNO) 'AVERAGE SALARY',
(SELECT MAX(SAL) FROM EMP WHERE DEPTNO = E.DEPTNO) 'MAXIMUM SALARY',
(SELECT MIN(SAL) FROM EMP WHERE DEPTNO = E.DEPTNO) 'MINIMUM SALARY'
FROM EMP E;
+-----------------+---------------+-------------------+---------+----------------
+----------------+----------------+
| EMPLOYEE NUMBER | EMPLOYEE NAME | DEPARTMENT NUMBER | SALARY | AVERAGE SALARY |
MAXIMUM SALARY | MINIMUM SALARY |
+-----------------+---------------+-------------------+---------+----------------
+----------------+----------------+
| 7369 | SMITH | 20 | 800.00 | 2175.000000 |
3000.00 | 800.00 |
| 7499 | ALLEN | 30 | 1600.00 | 1566.666667 |
2850.00 | 950.00 |
| 7521 | WARD | 30 | 1250.00 | 1566.666667 |
2850.00 | 950.00 |
| 7566 | JONES | 20 | 2975.00 | 2175.000000 |
3000.00 | 800.00 |
| 7654 | MARTIN | 30 | 1250.00 | 1566.666667 |
2850.00 | 950.00 |
| 7698 | BLAKE | 30 | 2850.00 | 1566.666667 |
2850.00 | 950.00 |
| 7782 | CLARK | 10 | 2450.00 | 2916.666667 |
5000.00 | 1300.00 |
| 7788 | SCOTT | 20 | 3000.00 | 2175.000000 |
3000.00 | 800.00 |
| 7839 | KING | 10 | 5000.00 | 2916.666667 |
5000.00 | 1300.00 |
| 7844 | TURNER | 30 | 1500.00 | 1566.666667 |
2850.00 | 950.00 |
| 7876 | ADAMS | 20 | 1100.00 | 2175.000000 |
3000.00 | 800.00 |
| 7900 | JAMES | 30 | 950.00 | 1566.666667 |
2850.00 | 950.00 |
| 7902 | FORD | 20 | 3000.00 | 2175.000000 |
3000.00 | 800.00 |
| 7934 | MILLER | 10 | 1300.00 | 2916.666667 |
5000.00 | 1300.00 |
+-----------------+---------------+-------------------+---------+----------------
+----------------+----------------+
[2]
Write a query to display maximum salary, minimum salary and average sal along with
emp name and deptno for all departments.
[3]
Find sum of salary for all employees whose sal > 1000. Query should display
ename,sal,Sum of salary, should be displayed according to the job.
[4]
Find number of clerks working in each department. Display ename,job and count of
that job.
[5]
Find how many employees are working under same manager display mgr and the count .
[6]
Calculate how many employees earn sal greater than 1500 in each dept. Query should
display ename, sal, count of employees earning sal >1500.
[7]
Calculate the difference between max salary and minimum salary for depatno 20.
Query should display ename,sal, diff between max sal and employee's sal, diff
between min sal and max sal of the dept.