0% found this document useful (0 votes)
27 views6 pages

ASSIGNMENT 3 Sol

Uploaded by

arjuntiw890
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views6 pages

ASSIGNMENT 3 Sol

Uploaded by

arjuntiw890
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Unit 2: (Database Query using SQL) Assignment_3

 All Questions carry 4 marks each.


 Total Marks 20
 All Questions are compulsory

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.

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)

1|Page Class 12 (IP)065 [email protected] WhatsApp:9336216924


Unit 2: (Database Query using SQL) Assignment_3
Solution

Q1. Write a query to display the highest salary of each department of EMP table.

SELECT DEPTNO, MAX(SAL) "HIGHEST SALARY"


FROM EMP
GROUP BY DEPTNO;
+--------+----------------+
| DEPTNO | HIGHEST SALARY |
+--------+----------------+
| 10 | 5000.00 |
| 20 | 3000.00 |
| 30 | 2850.00 |
+--------+----------------+
3 rows in set (0.00 sec)

Q2. Write a query to display the highest salary of each department of EMP table,
along with employee name.

SELECT DEPTNO, ENAME, SAL "HIGHEST SALARY"


FROM EMP
WHERE SAL IN (SELECT MAX(SAL) FROM EMP GROUP BY DEPTNO);
+--------+-------+----------------+
| DEPTNO | ENAME | HIGHEST SALARY |
+--------+-------+----------------+
| 30 | BLAKE | 2850.00 |
| 20 | SCOTT | 3000.00 |
| 10 | KING | 5000.00 |
+--------+-------+----------------+
3 rows in set (0.00 sec)

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)

2|Page Class 12 (IP)065 [email protected] WhatsApp:9336216924


Unit 2: (Database Query using SQL) Assignment_3
 This made the query wrong as it is showing two records from DEPTNO 20.
 JONES is not getting maximum salary in DEPTNO 20 (SCOTT is getting).
 But salary of JONES is equal to maximum salary of some employee in other department
(BLAKE in Department 30 in this case) so the name JONES is wrongly displayed.
 Hence, the query is faulty.

So, we have to make some changes in the above Query, as shown:


SELECT DEPTNO, ENAME, SAL "HIGHEST SALARY"
FROM EMP
WHERE (DEPTNO, SAL) IN (SELECT DEPTNO, MAX(SAL)
FROM EMP
GROUP BY DEPTNO
);
This will produce the correct output, by removing JONES of DEPTNO 20.
+--------+-------+----------------+
| DEPTNO | ENAME | HIGHEST SALARY |
+--------+-------+----------------+
| 30 | BLAKE | 2850.00 |
| 20 | SCOTT | 3000.00 |
| 10 | KING | 5000.00 |
+--------+-------+----------------+
3 rows in set (0.07 sec)
Note:
Making changes in salary of JONES (from 2975 to 2850), does not affect the output of query in
Question 1, because that query is using GROUP BY, which picks maximum salary from each group
(DEPTNO). So from DEPTNO 20 the GROUP BY picks only one maximum salary i.e. 3000 of SCOTT.

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)

3|Page Class 12 (IP)065 [email protected] WhatsApp:9336216924


Unit 2: (Database Query using SQL) Assignment_3
Explanation
The output is correct (you may verify from table EMP)
To recheck the query, we updated DEPTNO of KING from 10 to 20, using the query:
UPDATE EMP SET DEPTNO = 20 WHERE EMPNO = 7839;
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 | Maximum Types of JOBs |
+--------+-----------------------+
| 20 | 4 |
+--------+-----------------------+
1 row 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:

SELECT E.DEPTNO, DEPTNAME, COUNT(DISTINCT JOB) "Max Types of JOBs"


FROM EMP E, DEPT D
WHERE E.DEPTNO = D.DEPTNO
GROUP BY E.DEPTNO
HAVING COUNT(DISTINCT JOB) >= ALL (SELECT COUNT(DISTINCT JOB)
FROM EMP
GROUP BY DEPTNO
);
+--------+----------+-------------------+
| DEPTNO | DEPTNAME | Max Types of JOBs |
+--------+----------+-------------------+
| 10 | SALES | 3 |
| 20 | PERSONEL | 3 |
| 30 | ACCOUNTS | 3 |
+--------+----------+-------------------+
3 rows in set (0.01 sec)

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)

4|Page Class 12 (IP)065 [email protected] WhatsApp:9336216924


Unit 2: (Database Query using SQL) Assignment_3

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

SELECT DEPTNO, COUNT(JOB) "maximum number of employees"


FROM EMP
GROUP BY DEPTNO
HAVING COUNT(*) >= ALL (SELECT COUNT(*) FROM EMP
GROUP BY DEPTNO
);
+--------+-----------------------------+
| DEPTNO | maximum number of employees |
+--------+-----------------------------+
| 30 | 6 |
+--------+-----------------------------+
To recheck the query, we updated DEPTNO of KING from 10 to 20, so that DEPTNO 10 now has
one employee less and in DEPTNO 20 no. of employees are increased by 1 and the output was as
expected.
+--------+-----------------------------+
| DEPTNO | maximum number of employees |
+--------+-----------------------------+
| 20 | 6 |
| 30 | 6 |
+--------+-----------------------------+
Now we modify the above query to display DEPTNAME (name of DEPTNO), which is in other table
DEPT, as shown:

SELECT E.DEPTNO, DEPTNAME, COUNT(*) "max no. of employees"


FROM EMP E, DEPT D
WHERE E.DEPTNO = D.DEPTNO
GROUP BY DEPTNO
HAVING COUNT(*) >= ALL (SELECT COUNT(*) FROM EMP
GROUP BY DEPTNO
);
+--------+----------+----------------------+
| DEPTNO | DEPTNAME | max no. of employees |
+--------+----------+----------------------+
| 30 | ACCOUNTS | 6 |
+--------+----------+----------------------+
OR
After updating DEPTNO of KING from 10 to 20,
+--------+----------+----------------------+
| DEPTNO | DEPTNAME | max no. of employees |
+--------+----------+----------------------+
| 20 | PERSONEL | 6 |
| 30 | ACCOUNTS | 6 |
+--------+----------+----------------------+
5|Page Class 12 (IP)065 [email protected] WhatsApp:9336216924
Unit 2: (Database Query using SQL) Assignment_3

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.

SELECT ENAME, SAL + COMM "SALARY"


FROM EMP
WHERE SAL + COMM < (SELECT AVG(SAL + COMM) FROM EMP);

+--------+------------+
| 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)

The correct query is

SELECT ENAME, SAL + IFNULL(COMM, 0) "Tot. Sal"


FROM EMP
WHERE SAL + IFNULL(COMM, 0) < (SELECT AVG( SAL + IFNULL(COMM, 0))
FROM EMP
);

+--------+----------+
| 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 |
+----------------------------+

6|Page Class 12 (IP)065 [email protected] WhatsApp:9336216924

You might also like