This is my database:
Table Country: id integer (pk), varchar name,
Table Employe: id integer (pk), countryid integer(fk), varchar name, integer salary.
I want to display employees with the highest salary in every country, but I can display only one employer with the highest salary.
This is my SQL query:
Select *
from Employe
order by Employe.salary
desc LIMIT 1;
In Postgres you can use the comfortable distinct on:
select distinct on (c.name) c.name country, e.name employe, salary
from employe e
join countries c on c.id = e.country_id
order by c.name, salary desc
Try this query
SELECT Employe.countryid , Max(Employe.salary) from Employe GROUP BY Employe.countryid
You could use the rank window function to get the "first" employee[s] (salary-wise) per country:
SELECT id, c.name, e.name, salary
FROM (SELECT id, countryid, name, salary,
RANK() OVER (PARTITION BY countryid ORDER BY salary DESC) rk
FROM employee) e
JOIN country c ON c.id = e.countryid
WHERE rk = 1
Related
i have two tables
employee :
employee_number employee_name salary divison_id
division :
division_id division_name
How to Show division id, division name, number of employees working in each division that has the highest number of employees.
I want to have an outcome like this :
division_id division_name total_employee
Z-100 | finance | 3
SELECT *
FROM (SELECT e.division_id,
d.division_name,
Count(1) total_employee
FROM employee e
JOIN division d
ON e.division_id = d.division_id
GROUP BY e.division_id
ORDER BY Count(1) DESC)a
LIMIT 1
If you want all the department with most employees you should consider that more than one department can have that. So I am revising my answer and instead of top I am using Rank() with CTE to get all the divisions with most number of employees.
with DivisionWithMostEmployee as
(
select Rank()over(order by count(*) desc) RowNumber, d.division_id, d.division_name, count(*) TotalEmployee
from division d, employee e where d.division_id=e.division_id
group by d.division_id,d.division_name
)
select Division_id,Division_name,TotalEmployee from DivisionWithMostEmployee where RowNumber=1
I am trying to compile a query which gives me the highest salary per each department and for each unique employee. The complexity is that 1 employee can be part of multiple departments.
In case the same employee has the highest salary in several departments, only the department with a lower salary should show. This is my start but I am not sure how to continue from here:
select max(salary) as salary, dd.dept_name,d.emp_no
from salaries s
inner join dept_emp d on
s.emp_no=d.emp_no
inner join departments dd on
d.dept_no=dd.dept_no
group by 2,3;
My output is:
What should I modify from here?
For an employee, you seem to only want to include the department with the smallest salary. I would recommend using window functions:
select s.*
from (select s.*,
rank() over (partition by dept_name order by salary desc) as seqnum_d
from (select s.*, d.dept_name,
rank() over (partition by dept_name order by salary) as seqnum_ed
from salaries s join
dept_emp d
on s.emp_no = d.emp_no join
departments dd
d.dept_no = dd.dept_no
) s
where seqnum_ed = 1
) s
where seqnum_d = 1;
Something like this?
select m.salary, m.emp_no, salary.dept_name from salary,
(select emp_no, min(salary) salary from salary group by emp_no) m
where
m.emp_no=salary.emp_no and m.salary=salary.salary;
Having two tables
Employee
Id
Name
Salary
DepartmentId
and
Departament
Id
Name
How can I get the highest average salary within two tables
like
Joe and Max belong to dept 1 so, avg is (70K+90K)/2
= 80K
and
Henry and Sam belog to dept 2, avg is (80K + 60K)/2=70k
so How to select the greatest avg salary by depto?, in this case
IT 80K
i have been trying:
'group the salary by each department and use the Max function to obtain the highest one.
select
Department.Name as Department,
T.M as Salary
from
Employee,
Department,
(select DepartmentId as ID, Max(Salary) as M from Employee group by DepartmentId) as T
where
Employee.Salary = T.M and
Department.Id = T.ID and
Employee.DepartmentId = Department.Id
enter image description here
If multiple department having same maximum avg salary then this solution will return multiple rows.
SELECT *
FROM(
SELECT d.Id, d.Name, AVG(e.Salary) avg_salary, RANK() OVER(ORDER BY AVG(e.Salary) DESC) AS rank_
FROM Employee e
INNER JOIN Departament d ON e.DepartmentId = d.Id
GROUP BY d.Id, d.Name
)T
WHERE rank_ = 1
If you want to get the average just for the department, you can use in this way.
select DepartmentId as ID, de.name as Deptname, Avg(Salary) as M from Employee em1
join Department de on de.departmentID = em1.DepartmentId
group by DepartmentId, de.name
If you want employee name along with highest average then you can use this approach as well.
select
Deptname as Department,
e.Name as Employeename,
z.M as Salary
from
Employee e
join
( select DepartmentId,Deptname, M, row_number() (order by m desc) rownum from ( select DepartmentId as ID, de.name as Deptname, Avg(Salary) as M from Employee em1
join Department de on de.departmentID = em1.DepartmentId
group by DepartmentId, de.name) as T) z
on
e.DepartmentId = T.DepartmentId and z.rownum = 1
If you want a full answer, you should provide DDL, sample data and desired result.
If I understand you correctly, you are looking for something like:
SELECT DepartmentID, AVG(Salary) AS AverageSalaryForDept
FROM Employee
GROUP BY DepartmentID
ORDER BY AverageSalaryForDept DESC;
This will give you all the averages, ordered from the highest to the lowest. Now if you want just the top one, add a FETCH clause:
SELECT DepartmentID, AVG(Salary) AS AverageSalaryForDept
FROM Employee
GROUP BY DepartmentID
ORDER BY AverageSalaryForDept DESC
OFFSET 0 ROWS FETCH NEXT 1 ROW ONLY;
HTH
Schema for EMPLOYEE
(ID, EMPLOYEENAME, SALARY, ORGANIZATIONID)
Query to Solve: Find employee Names in each organization with Maximum Salary without a Join.
SELECT E.*
FROM EMPLOYEE E,
(SELECT EMP.ORGANIZATIONID, MAX(EMP.SALARY)
FROM EMPLOYEE EMP
GROUP BY EMP.ORGANIZATIONID) MAXSALARY
WHERE MAXSALARY.SALARY =E.SALARY
AND E.ORGANIZATIONID=EMP.ORGANIZATIONID ;
Is there a way to avoid the join? I am using Spark SQL API and joins cause an extra shuffle operation which is expensive. Is there a way to get the employee name while getting the max salary?
Assume you have a single employee in each organization having the max salary
You can use PARTITION BY with Spark SQL as shown below (Although it will require a subquery)
SELECT E.*
FROM
(SELECT EMP.EMPLOYEENAME, EMP.ORGANIZATIONID, EMP.SALARY,
row_number() OVER (PARTITION BY ORGANIZATIONID ORDER BY SALARY DESC) as rank
FROM EMPLOYEE EMP
) AS E
WHERE E.rank=1
Try this:
SELECT P.ORGANIZATIONID, P.EMPLOYEENAME
FROM EMPLOYEE P
WHERE P.SALARY = (SELECT MAX(E.SALARY) FROM EMPLOYEE E WHERE P.ORGANIZATIONID = E.ORGANIZATIONID)
GROUP BY P.ORGANIZATIONID, P.EMPLOYEENAME
Try this:
SELECT EMPLOYEENAME FROM EMPLOYEE
WHERE SALARY IN (SELECT MAX(SALARY) FROM EMPLOYEE GROUP BY ORGANIZATIONID)
How can I get id of department in which employees receive the maximum salary:
Employee table: Empl (ID, FirstName, LastName, Salary, DeptId)
Departments table: Dept (ID, City)
rus (Вывести “id” подразделения, в котором сотрудники получают максимальную заработную плату.)
EDIT: Changed SUM(Salary) to AVG(Salary) based on comments on the question.
SELECT TOP 1 DeptId
FROM Employees
GROUP BY DeptId
ORDER BY AVG(Salary) DESC
SELECT TOP 1 B.*
FROM (SELECT DeptId, AVG(Salary) AvgSalary
FROM Empl
GROUP BY DeptId) A
INNER JOIN Dept B
ON A.DeptId = B.Id
ORDER BY AvgSalary DESC
To get the one single Department's ID where the highest single salary is paid:
SELECT TOP 1 DeptID
FROM dbo.Empl
ORDER BY Salary DESC
Or are you looking for something else?
I would assume you mean max average salary of a department and not the single highest salary across all departments.
However it seems all you would have to do is use the following SQL functions
MAX function
AVG function
group by department ID and viola.
Thought I agree with the comments above, I will assume you are doing this for research ;-)
select id
from dept
where id = ( select deptid
from ( select max(avg_salary), deptid
from ( select deptid, avg(salary) as avg_salary
from empl
group by deptid )
group by deptid )
)
:-)
SELECT DepartmentId
FROM Employee
WHERE Salary = (SELECT MAX(Salary) FROM Employee)