select max(salary)
from employees
group by department_id
select * from employees
where salary in (select max(salary)
from employees
group by department_id )
I think that you are looking for a correlated subquery:
select e.*
from employees e
where e.salary = (
select max(e1.salary)
from employees e1
where e1.department_id = e.department_id
)
This query gives you employees that have the greatest salary in their department (ties included).
For performance, consider an index on employees(department_id, salary).
You can use the analytical function as follows:
Ties included:
select * from
(select e.*, dense_rank() over (partition by e.department_id order by e.salary) as rn
from employees e)
where rn = 1
Ties excluded (one random record when ties)
select * from
(select e.*, row_number() over (partition by e.department_id order by e.salary) as rn
from employees e)
where rn = 1
Related
SELECT
employee_id,
department_id,
first_name,
last_name,
hire_date,
job_id
FROM employees e
WHERE hire_date IN
(
SELECT max(hire_date)
FROM employees
WHERE e.department_id = department_id
)
ORDER BY hire_date ASC
Result of the query
So this is my query and the result of it. There are two tied results for SA_REP department and I should have only one result - for instance the one with the lower employee_id. I've googled the problem but can't seem to find any related results...
Thanks for any help!
You can use a not exists query which gives you more flexibility:
SELECT *
FROM employees e
WHERE NOT EXISTS ( -- no x exists that...
SELECT *
FROM employees x
WHERE x.department_id = e.department_id -- belongs to same department
AND (
x.hire_date > e.hire_date OR -- but hired later than e
x.hire_date = e.hire_date AND x.employee_id < e.employee_id -- hired on same date but has lesser employee id than e
)
)
You may use the RANK analytic function here:
WITH cte AS (
SELECT e.*, RANK() OVER (PARTITION BY department_id
ORDER BY hire_date DESC, employee_id) rnk
FROM employees e
)
SELECT employee_id, department_id, first_name, last_name, hire_date, job_id
FROM cte
WHERE rnk = 1;
Find third highest salary of employee without order by and limit.
select(SELECT MIN(Salary)
FROM (SELECT * TOP (3) Salary
FROM Employees
)
Not working in the mysql work bench
You can try this -
SELECT salary
FROM Employees e1
WHERE 3-1 = (SELECT COUNT(DISTINCT salary) FROM Employees e2
WHERE e2.salary > e1.salary)
Try this:
WITH emp (
SELECT
ROW_NUMBER() OVER (ORDER BY salary DESC) rowId, *
FROM employee
)
SELECT * FROM emp WHERE rowId = 3;
In the employees table I have id_departament
And I can`t figure it out how to extract the avg salary for every employee
Use window functions if your RDBMS supports them:
select *
from (
select e.*, avg(salary) over(partition by id_department) avg_salary_dept
from employee e
) t
where salary > avg_salary_dept
Alternatively, you can join the table with an aggregate query that computes the average salary per department:
select e.*
from employee e
inner join (
select id_department, avg(salary) avg_salary_dept
from employee
group by id_department
) a on e.id_department = a.id_department and e.salary > a.avg_salary_dept
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;
I have this table. I want to select all employees in each profession with maximum salary.
I tried everything, but nothing seems to be working.
The below query results error.
SELECT * FROM Employee WHERE EmployeeID IN (
SELECT EmployeeID FROM Employee HAVING MAX(Salary) = Salary GROUP BY Profession)
You can use ROW_NUMBER window function
SELECT *
FROM (SELECT *,
Row_number()OVER (PARTITION BY Profession ORDER BY Salary DESC) rn
FROM Employee) a
WHERE rn = 1
Note : When there is a tie in maximum salary for a profession then this will return only one, if you need the tie records then use DENSE_RANK
SELECT *
FROM (SELECT *,
Dense_rank()OVER (PARTITION BY Profession ORDER BY Salary DESC) rn
FROM Employee) a
WHERE rn = 1
in case you want to fix your sub-query then(will return the tie records)
SELECT *
FROM Employee e1
WHERE Salary = (SELECT Max(Salary)
FROM Employee e2
WHERE e1.Profession = e2.Profession)
or
SELECT *
FROM Employee e1
WHERE EXISTS (SELECT 1
FROM Employee e2
WHERE e1.Profession = e2.Profession
HAVING Max(Salary) = e1.Salary)
SELECT *
FROM (
SELECT *
, ROW_NUMBER() OVER (PARTITION BY Profession ORDER BY Salary DESC) rn
FROM Employee
) x
WHERE x.rn = 1
Try this simple query !
SELECT
EmployeeID ,
EmployeeName ,
Profession ,
max(Salary) AS 'Salary'
FROM
Employee
GROUP BY
Profession
SELECT *
FROM employees
WHERE salary IN (SELECT MAX(salary)
FROM Employees
GROUP BY Profession)