How do I get the Average of maximum value? - sql

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

Related

Cannot get results to show when querying salary of department based on average salary of department and twice minimum salary of entire company

I am attempting to display the department and average salary of employees in said department if it meets a condition: average salary of department must be twice the minimum salary of the entire firm.
select dname, avg(salary) from department, employee where dno=dnumber group by dname having avg(salary) > min(salary)*2;
I have tables for employee, department. My query is below, however it results in an empty table.
I believe it is because salary in the halving clause is referencing within department and within employee - or rather it isn't doing this properly.
To just list average salaries by department I used this query which seems to work fine:
select dname, avg(salary) from department, employee where dno=dnumber group by dname;
Any help would be much appreciated.
Thank you!
min(salary) is the minimum of the department, replace it with (select min(salary) from employee)

calculate highest salary department in SQL

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

Sql query unsuccessful

I have employee and department tables.
I need to find the following SQL query:
What is the employee with the lowest salary for each department where the employees salary is greater than the average salary for the department.
I tried this:
select * from [Company Management].[dbo].[Employee]
where Salary in
(select min(Salary) from [Company Management].[dbo].[Employee]
where salary>=All
--average employee salary for department
(select avg(salary) from [Company Management].[dbo].[Employee]
group by DepartmentID)
group by DepartmentID)
What I get in the result is greater than both averages and not department specific.
i.e. if the average for department 1 is 50 and for department 2 is 37 I get correct answer for department 1 but 75 for department two, and department 2 has an employee with 40.
Select avarage salaries per department in employees.
Join with employees and find thus the minimum salary higer than the avarage salary per department.
Join again with employees to get all employees having that salary in their department.
Here is the statement:
select employee.*
from employee
join
(
select employee.departmentid, min(employee.salary) as sal
from employee
join
(
select departmentid, avg(salary)
from employee
group by departmentid
) avg_sals on avg_sals.departmentid = employee.departmentid and avg_sals.salary < employee.salary
group by employee.departmentid
) wanted_sals on wanted_sals.departmentid = employee.departmentid and wanted_sals.sal = employee.salary;

How do I filter a minimum and maximum salary for each seperate department? [duplicate]

This question already has answers here:
How to select the max salary per department, including the employees that earn it [closed]
(2 answers)
Closed 8 years ago.
I am referencing a set of tables in which a department contains multiple employees, each of whom has a salary.
The department_id, employee_id, and salary are all part of the EMPLOYEES table. I would like to determine the minimum and maximum salaries in each department.
My query looks like this:
SELECT employee_id, first_name||' '||last_name AS full_name, SALARY
DECODE(department_id, 50, (min(salary)||' '||max(salary)),
60, (min(salary)||' '||max(salary)),
80, (min(salary)||' '||max(salary)),
90, (min(salary)||' '||max(salary)),
employee_id)
SALARY_RANGE
FROM EMPLOYEES;
Thoughts on how to complete this query? Thanks
If you want the minimum and maximum salaries in each department, why not just do:
select department_id, min(salary)||' '||max(salary) as Salary_Range
from Employees
group by department_id;
You can limit this to particular departments using where:
select department_id, min(salary)||' '||max(salary) as Salary_Range
from Employees
where department_id in (50, 60, 80, 90)
group by department_id;
A simple solution would be to use a group by statement on Department and the min and max salary in your selection. This would give you the min and max per department. You could then Join in additional data points that you would want in your final projection.
example:
SELECT min(salary), max(salary)
FROM Salary
GROUP BY Department

Oracle SQL why query is returning me multiple results?

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