Reputation: 7
Calculate the minimum salary for exempt employees and the average salary for non-exempt employees in a SINGLE SQL statement. Use subqueries to incorporate both elements in the query. It should look something like:
Min Exempt Salary Average Non-Exempt Salary
47,000 35,271
I know how to do it separate but cannot figure how to do it as it stated above , this is the statments I have.
SELECT jobs1.exempt_nonexempt_status,
Min (employees.salary)AS Minimal_Exempt_Salary
FROM employees
LEFT JOIN jobs1
ON employees.job_title = jobs1.job_title
WHERE jobs1.exempt_nonexempt_status = 'Exempt'
GROUP BY jobs1.exempt_nonexempt_status
SELECT jobs1.exempt_nonexempt_status,
Avg (employees.salary)AS Average_Non_Exempt_Salary
FROM employees
LEFT JOIN jobs1
ON employees.job_title = jobs1.job_title
WHERE jobs1.exempt_nonexempt_status = 'Non-exempt'
GROUP BY jobs1.exempt_nonexempt_status
Upvotes: 0
Views: 306
Reputation: 238276
A normal way to do this would be:
select j.exempt_nonexempt_status
, avg(case when j.exempt_nonexempt_status='Non-exempt' then e.salary end)
as Average_Non_Exempt_Salary
, min(case when j.exempt_nonexempt_status='Exempt' then e.salary end)
as Minimal_Exempt_Salary
from Employees e
left join
jobs1 j
on e.job_title = j.job_title
group by
j.exempt_nonexempt_status
But that doesn't use subqueries. Another way is to wrap your two queries in an outer select:
select (
select MIN (employees.salary)
from Employees left join jobs1
on employees.job_title = jobs1.job_title
where jobs1.exempt_nonexempt_status='Exempt'
group by jobs1.exempt_nonexempt_status
) as MinimalExemptSalary
, (
select avg (employees.salary)
from Employees left join jobs1
on employees.job_title = jobs1.job_title
where jobs1.exempt_nonexempt_status='Non-exempt'
group by jobs1.exempt_nonexempt_status
) as AverageNonExemptSalary
Upvotes: 1
Reputation: 22955
Try this:
SELECT
MIN(CASE WHEN J.exempt_nonexempt_status = 'Exempt'
THEN E.Salary
END) AS Minimal_Exempt_Salary,
SUM(CASE WHEN J.exempt_nonexempt_status = 'Non-exempt'
THEN E.Salary
END) AS Average_Non_Exempt_Salary
FROM Employees E
LEFT JOIN JOBS1 J ON J.job_title = E.job_title
WHERE J.exempt_nonexempt_status IN ('Exempt', 'Non-exempt')
Upvotes: 2