Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The SQL statements comprising the DDL for the COMPANY Database are given to you.

ID: 3912574 • Letter: T

Question

The SQL statements comprising the DDL for the COMPANY Database are given to you. For that database, answer the following queries. Create the files Q1 to Q10 in PostgreSQL. Do follow the restrictions stated for individual queries. When a query asks for "name" of employees, retrieve both, the first and the last names.

7. For each department whose average employee salary is more than $30,000, retrieve the department name and the number of male employees working for that department.

https://users.cs.fiu.edu/~navlakha/4710/CreateCompany.sql

Explanation / Answer


NOTE: since Chegg prohibits use of certain words in our answer, I had to modify the column name *ex (representing the gender) to Pex. Please change it to correct column name when you run the query... You will get error since Pex is not the correct column name....

You will need to change near the condition e.Pex = 'M' in the query


SELECT d.dname as Department, count(*) as Males
FROM department d, employee e
WHERE d.dnumber = e.dno AND e.Pex = 'M'AND d.dnumber in ((SELECT dno from employee GROUP BY dno HAVING AVG(salary) > 30000))
GROUP BY d.dname

Detailed explanation
--------------

First let us write a query to get department numbers where average salary is more than 30000. We will use this query to get the final query

SELECT dno
FROM employee
GROUP BY dno
HAVING AVG(salary) > 30000


The above query gets department no. where avg. salary is more than 30000. It goes through all records in employee table and groups them based on dno and then gets average for each group.

Now we use the above query to get out final query i.e names of department and no. of males in those departments where average salary is more than 30000.


SELECT d.dname as Department, count(*) as Males
FROM department d, employee e
WHERE d.dnumber = e.dno AND e.Pex = 'M'AND d.dnumber in ((SELECT dno from employee GROUP BY dno HAVING AVG(salary) > 30000))
GROUP BY d.dname

This query uses the previous query to get all dno where average is more than 30000 and then fetches the department name from department table using the common column dnumber and also counting only those records which are males

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote