ASSIGNMENT 3 Sol
ASSIGNMENT 3 Sol
Q3. Write a query to display the name of the department of EMP table,
which has maximum types of jobs.
Q4. Write a query to display the name of the department from DEPT table,
which has maximum number of employees, as shown in EMP table.
Q5. Write a query to display the name of all the employees of EMP table,
who are getting salary (SAL + COMM) less than the average salary of
all the SALESMAN.
Table: EMP
+-------+--------+-----------+------+------------+---------+------+--------+
| EMPNO | ENAME | JOB | MGR | DOJ | SAL | COMM | DEPTNO |
+-------+--------+-----------+------+------------+---------+------+--------+
| 7369 | SMITH | CLERK | 7902 | 1990-12-17 | 800.00 | NULL | 20 |
| 7499 | ALLEN | SALESMAN | 7698 | 1991-02-20 | 1600.00 | 300 | 30 |
| 7521 | WARD | SALESMAN | 7698 | 1991-02-22 | 1250.00 | 500 | 30 |
| 7566 | JONES | MANAGER | 7839 | 1991-04-02 | 2975.00 | NULL | 20 |
| 7654 | Martin | SALESMAN | 7698 | 1991-09-28 | 1250.00 | 1400 | 30 |
| 7698 | BLAKE | MANAGER | 7839 | 1991-05-01 | 2850.00 | NULL | 30 |
| 7782 | CLARK | MANAGER | 7839 | 1991-06-09 | 2450.00 | NULL | 10 |
| 7788 | SCOTT | ANALYST | 7566 | 1997-04-19 | 3000.00 | NULL | 20 |
| 7839 | KING | PRESIDENT | NULL | 1991-11-17 | 5000.00 | NULL | 10 |
| 7844 | TURNER | SALESMAN | 7698 | 1991-09-08 | 1500.00 | 0 | 30 |
| 7876 | ADAMS | CLERK | 7788 | 1997-05-23 | 1100.00 | NULL | 20 |
| 7900 | JAMES | CLERK | 7698 | 1991-12-03 | 950.00 | NULL | 30 |
| 7902 | FORD | ANALYST | 7566 | 1991-12-03 | 300.00 | NULL | 20 |
| 7934 | MILLER | CLERK | 7782 | 1992-01-23 | 1300.00 | NULL | 10 |
+-------+--------+-----------+------+------------+---------+------+--------+
14 rows in set (0.00 sec)
Table: DEPT
+--------+----------+------+----------+
| DEPTNO | DEPTNAME | MGR | LOCATION |
+--------+----------+------+----------+
| 10 | SALES | 8566 | MUMBAI |
| 20 | PERSONEL | 9698 | DELHI |
| 30 | ACCOUNTS | 4578 | CHINNAI |
| 40 | RESEARCH | 8839 | BANGLORE |
+--------+----------+------+----------+
4 rows in set (0.00 sec)
Q1. Write a query to display the highest salary of each department of EMP table.
Q2. Write a query to display the highest salary of each department of EMP table,
along with employee name.
Explanation
The query seems to be correct, until we updated the salary of JONES from 2975 to 2850, as:
UPDATE EMP SET SAL = 2850 WHERE ENAME = 'JONES';
After this update, the above query produced the following output:
+--------+-------+----------------+
| DEPTNO | ENAME | HIGHEST SALARY |
+--------+-------+----------------+
| 20 | JONES | 2850.00 |
| 30 | BLAKE | 2850.00 |
| 20 | SCOTT | 3000.00 |
| 10 | KING | 5000.00 |
+--------+-------+----------------+
4 rows in set (0.00 sec)
Q3. Write a query to display the name of the department of EMP table, which has
maximum types of jobs.
SELECT DEPTNO, COUNT(DISTINCT JOB) "Maximum Types of JOBs"
FROM EMP
GROUP BY DEPTNO
HAVING COUNT(DISTINCT JOB) >= ALL(SELECT COUNT(DISTINCT JOB)
FROM EMP
GROUP BY DEPTNO
);
+--------+-----------------------+
| DEPTNO | Maximum Types of JOBs |
+--------+-----------------------+
| 10 | 3 |
| 20 | 3 |
| 30 | 3 |
+--------+-----------------------+
3 rows in set (0.00 sec)
Now, we modify the above query to display DEPTNAME (name of DEPTNO), which is in other
table DEPT, as shown:
To recheck the query, we updated DEPTNO of KING from 10 to 20, so that DEPTNO 10 now has
one JOB (PRESIDENT) less and type of jobs in DEPTNO 20 now is increased by 1 and the output
was as expected.
+--------+----------+-------------------+
| DEPTNO | DEPTNAME | Max Types of JOBs |
+--------+----------+-------------------+
| 20 | PERSONEL | 4 |
+--------+----------+-------------------+
1 row in set (0.00 sec)
Q4. Write a query to display the name of the department from DEPT table, which
has maximum number of employees, as shown in EMP table.
After inspecting the table EMP, we noticed that, in EMP table, the number of employees in
DEPTNO 10, 20, 30 is respectively 3, 5, and 6
+--------+------------+
| ENAME | SALARY |
+--------+------------+ This output and query
| ALLEN | 1900.00 | both are wrong.
| WARD | 1750.00 |
| TURNER | 1500.00 |
+--------+------------+
3 rows in set (0.00 sec)
+--------+----------+
| ENAME | Tot. Sal | IFNULL(COMM, 0) means:
+--------+----------+ While computing, replace all the NULL
| SMITH | 800.00 | values in the column COMM with zero(0).
| ALLEN | 1900.00 | Note: This will not make any changes in
| WARD | 1750.00 | the table.
| TURNER | 1500.00 |
SELECT AVG(SAL + COMM) FROM EMP;
| ADAMS | 1100.00 | +-----------------+
| JAMES | 950.00 | | AVG(SAL + COMM) |
| FORD | 300.00 | +-----------------+
| MILLER | 1300.00 | | 1950.000000 |
+--------+----------+ +-----------------+
8 rows in set (0.00 sec)
SELECT AVG(SAL + IFNULL(COMM, 0)) FROM EMP;
+----------------------------+
| AVG(SAL + IFNULL(COMM, 0)) |
+----------------------------+
| 2037.500000 |
+----------------------------+