Question: sql query to get:
The names of the departments with the highest average salary of their employees.
If you using t-sql (MS-SQL)
SELECT AVG(salary) AS 'avgSalary' , dept
FROM emp
GROUP BY dept
Order BY avgSalary DESC
Related
I have a SQL server problem where i have to get the average of the maximum salaries in each department. For example I have a department with id 1 that has an employee with a maximum salary of 50000, and a department with id 2 that has an employee with a maximum salary of 30000, what i have to do is calculate the average between these 2.
What I tried:
SELECT AVG(MAX(salary))
FROM employees
GROUP BY department_id
most simple think you can do is (if in the employee table, you have a department_id) :
SELECT AVG(p.maximum)
FROM (SELECT department_id, MAX(salary) AS maximum
FROM employees
GROUP BY department_id) p
I try to learn mysql oracle database, I want to show average salary from table employee group by department_id and where average salary is like '%0'.
here is my query
select department_id, AVG(salary) as "Pay Salary per dept"
from employees
where department_id is not null and AVG(salary) like '%0'
group by department_id order by department_id
what's right query to replace
AVG(salary) like '%0' ?
Thanks
Use MOD function instead of LIKE.
where MOD(salary,10)=0
query
select t.dept_id,t.Pay_Salary_per_dept
from
(
select dept_id, AVG(salary) as Pay_Salary_per_dept
from employee
where dept_id is not null
group by dept_id
)t
where mod(t.Pay_Salary_per_dept,10)=0
order by t.dept_id;
Fiddle demo here
To filter the result of aggregated functions there is the HAVING clause in SQL.
You may write it direct without a subquery as follows:
select dept_id, AVG(salary) as Pay_Salary_per_dept
from employee
where dept_id is not null
group by dept_id
having mod(AVG(salary),10)=0
;
This question already has answers here:
SELECT every employee that has a higher salary than the AVERAGE of his department
(8 answers)
Closed 8 years ago.
i have a table called employees which i have name, department_id and salary in it. I want to find the employees whose salary is greater than the average of their department and see their names, department_id, salary and the average salary of their department. I have written this code but it does not work. How can we fix this? Thanks in advance.
SELECT name, department_id, salary, avg(salary)
FROM employees
GROUP BY name, department_id, salary
HAVING salary > (select avg(salary) from employees group by department_id)
I have updated my code as you said like:
SELECT department_id, salary, avg(salary), count(*)
FROM employees e
GROUP BY department_id, salary
HAVING salary > (select avg(salary) from employees e2 where e2.department_id=e.department_id)
But when i run this i get this result:
You can see that salary and the averages are the same and there are 2 department 80's, i need 1 of all the existing departments. How can we fix this. I am using the Oracle database if that's any important. Thanks.
Your code is quite close. But, instead of a group by in the subquery, it needs to be correlated to the outer query. And, you don't need an outer aggregation, just a where clause:
SELECT name, department_id, salary
FROM employees e
WHERE salary > (select avg(salary) from employees e2 where e2.department_id = e.department_id);
However, you are presumably learning Oracle. You can also write this query using analytic functions:
select e.*
from (select e.*, avg(salary) over (partition by department) as avgsalary
from employees e
) e
where e.salary > e.avgsalary;
Although this is probably a bit advanced for what you are learning, I encourage you to understand both queries.
I am stuck with a query, I can't get it to work correctly.
This is what I need to do:
Write a query that will return both the maximum and minimum average
salary grouped by department from the employees table.
This is what I made:
SELECT
(SELECT AVG(MIN(salary)) FROM employees GROUP BY department_id) As "Minimum Average salary",
(SELECT AVG(MAX(salary)) FROM employees GROUP BY department_id) As "Maximum Average Salary"
FROM EMPLOYEES
but it keeps returning me more than 1 row result. I can't use LIMIT 2, I'm getting an error if I query with LIMIT.
I also tried with the following query, but I'm getting error: missing expression.
SELECT
AVG(SELECT MIN(salary) FROM employees GROUP BY department_id)) As "Minimum Average salary",
AVG((SELECT MAX(salary) FROM employees GROUP BY department_id)) As "Maximum Average Salary"
FROM EMPLOYEES
No need for an inline view -- this works:
select
min(avg(salary)),
max(avg(salary))
from
employees
group by
dept_id;
http://sqlfiddle.com/#!4/eff67/3
First of all, it's simple why it returns as many as all records in EMPLOYEES table; because the group by is in your inner sub-query which is not applied to your main query. actually your sub-queries are executed per each row in the table and the result is returned per row.
I think this is the right query that you need:
SELECT AVG(min_salary) As "Minimum Average salary", AVG(max_salary) As "Maximum Average Salary"
FROM
(SELECT MIN(salary) As min_salary, MAX(salary) AS max_salary
FROM EMPLOYEES
GROUP BY department_id
)
Use a subquery to retrieve all the average salaries. The outer query can then pick up the minimum and the maximum:
select min(AvgSalary)
, max(AvgSalary)
from (
select avg(salary) as AvgSalary
from employees
group by
department_id
) SubQueryAlias
select MAX(salary),min(salary), avg(salary), department_id from employees group by department_id
O community, do you know how I could select the department_ID, and lowest salary of the department with the highest average salary? Or how to eliminate the'ORA-00934: group function not allowed here' issue? Would I need to use two subqueries?
So far, this is what I've come up with, trying to get the department_ID of the highest paid department:
SELECT department_ID, MIN(salary
FROM employees
WHERE department_ID = (SELECT department_ID
FROM employees WHERE salary = MAX(salary));
Thank you, your assistance is greatly appreciated.
I can't test this, but it should work:
;WITH DepartmentsSalary AS
(
SELECT department_ID, AVG(Salary) AvgSalary, MIN(Salary) MinSalary
FROM employees
GROUP BY department_ID
)
SELECT department_ID, MinSalary
FROM ( SELECT department_ID, AvgSalary, MAX(AvgSalary) OVER() MaxSalary, MinSalary
FROM DepartmentsSalary) D
WHERE MaxSalary = AvgSalary
You can use join (then you have just one sub query)
select e1.department_ID, min(e1.salary)
from employees e1
join (
select avg_query.department_ID, max(avg_query.avg_value)
from (
select department_ID, avg(salary) as avg_value
from employees
group by department_ID
) avg_query
) e2 on e2.department_ID = e1.department_ID
;
First sub-query returned average salary for all departments
Next sub-query based on first sub-query returned highest average
salary and related department_ID
Main query returned min salary for department_ID with highest average
salary