I have some table:
name dep_id salary
Vasia 5 1000
Oleg 5 1300
Vitia 4 1000
Sacha 3 1100
Kolia 5 1600
Lesha 2 1400
Sergey 4 1200
Marina 5 1300
Olga 5 1500
Igor 4 1400
Valia 3 1500
Erema 4 1500
I need to get the name of the employees with the maximum salary in his department
i.e I need
Lesha 1400
Valia 1500
Erema 1500
Kolia 1600
I tried:
SELECT name, max(salary) FROM employees GROUP BY dep_id
but this displays incorrect values
how can I do this?
select e1.*
from employees e1
join
(
SELECT dep_id, max(salary) as msal
FROM employees
GROUP BY dep_id
) e2 on e1.dep_id = e2.dep_id
and e1.salary = e2.msal
select t1.name,t2.sallary
from Employees t1 join
(select dep_id,MAX(Sallary) as Sallary
from Employees
group by dep_id) t2 on t1.dep_id=t2.dep_id and t1.sallary=t2.sallary
order by t2.sallary
Result:
name Sallary
---------------
Lesha 1400
Valia 1500
Erema 1500
Kolia 1600
Demo in SQL Fiddle
Try Below Query for the required output :
select name,sallary from employees t1 where (dep_id,sallary) in
(select dep_id,max(sallary) from employees group by dep_id);
Related
I was working on a problem from Leetcode #185
I could understand the solution but I want to know how to write the query that add a column which indicate the count the people have a better salary than the tuple one. I think it is possible in SQL, but i don't know how to make it right, i always get syntax error. :-/
from Employee e1 (Select count(distinct e2.Salary)
from Employee e2
Where e2.Salary > e1.Salary) as c
For exemple I have such a table Employee:
Id - Name - Salary
1 toto 60000
2 tata 50000
3 kiki 90000
4 lily 70000
5 momo 60000
I want to have such a result:
Id - Name - Salary - Head_count_of_higher_salary
1 toto 60000 2
2 tata 50000 4
3 kiki 90000 0
4 lily 70000 1
5 momo 60000 2
Thanks guys
Your subquery is almost correct.
Just remove DISTINCT from COUNT() (although just COUNT(*) would also work) and use it as the new column:
select *,
(
select count(e2.Salary)
from Employee e2
where e2.Salary > e1.Salary
) as Head_count_of_higher_salary
from Employee e1
See the demo.
You can also implement this type of query with a LEFT JOIN on the joined table having a higher salary than the first, and then counting the number of rows in the joined table:
SELECT e1.Id, e1.Name, e1.Salary, COUNT(e2.Id) AS Head_count_of_higher_salary
FROM Employee e1
LEFT JOIN Employee e2 ON e2.Salary > e1.Salary
GROUP BY e1.Id, e1.Name, e1.Salary
Output:
Id Name Salary Head_count_of_higher_salary
1 toto 60000 2
2 tata 50000 4
3 kiki 90000 0
4 lily 70000 1
5 momo 60000 2
Demo on SQLfiddle
If you are running MySQL 8.0: what you ask for is exactly what rank() does.
This would be as simple as:
select e.*, rank() over(order by salary desc) - 1 head_count_of_higher_salary
from employees e
I have a table emp with columns :-
empid empname mgrid doj
1 Steve 2 25-03-2019
2 Winter 3 26-04-2019
3 Summer 1 27-05-2019
4 Autumn 2 28-06-2019
and a table sal with columns :-
empid project salary
1 P1 1000000
2 P1 60000
3 P2 5000
4 P3 1000000
I want to list name of employees with same salary.
Desired result :-
first_employee second_employee salary
Steve Autumn 1000000
What I tried to do is join emp table with sal and before that was trying to self join sal table. How can I achieve the desired results also is there a way to use union in order to get the results.
What if there are more than two employees with the same salary?
I would recommend group_concat():
select salary, group_concat(empname order by doj) as empnames
from emp
group by salary
having count(*) > 1;
I am using Mysql.
I have 2 tables:
table Sales:
S_Id DATE Total_Price Employee_Id
1 1/02/2014 5636 10
2 1/04/2014 148 999
3 1/05/2014 101 30
4 1/06/2014 959 40
5 1/02/2014 3000 10
6 1/04/2014 4992 13
7 1/05/2014 472 11
8 1/06/2014 8483 40
table employees:
Employee_Id First_Name Last_Name Address City
10 brock li a1 berlin
20 dan santiago a2 london
30 alex brookmen a3 rome
40 michael gold a4 amsterdam
50 john bisping a5 barcelona
999 tony oneale a6 munich
the employees table holding only active employees
but still the table Sales holding all sales, including sales of not active employees
my question:
I want to make the top 4 employees who made the max of sum sales per only active employees (by sum Sales.Total_Price) at year 2015
Employee_Id = 999 will include his sum of sales and also the sum of all the Sales price that made by not active employees.
Employee_Id = 999 is an active employee
therefor result should be:
Employee_Id First_Name Last_Name Address City total sales
10 brock li a1 berlin 8636
20 dan santiago a2 london 8483
999 tony oneale a6 munich 5612
40 michael gold a4 amsterdam 959
therefor I tried to make a left join between table Sales to employees and sum all the prices when employees.Employee_Id = null
my code:
SELECT s.employee_id
, e.first_name
, e.last_name
, e.address
, e.city
, CASE WHEN ISNULL(e.employee_id) OR e.employee_id = 999
THEN SUM(s.total_price)
ELSE SUM(s.total_price) END 'total sales'
FROM sales s
LEFT
JOIN employees e
ON s.employee_id = e.employee_id
WHERE YEAR(s.date) = 2015
GROUP
BY s.employee_id
, e.first_name
, e.last_name
ORDER
BY SUM(s.total_price) DESC
LIMIT 4
but it didn't succeed:
it didn't sum all the sales of null values (not active employees) into the row of Employee_Id = 999
it still can be that not active employees will be presented in the top 4 sales when I only want active employees in the list
also, if you know a better way to sum max 4 sales per employee it will be great for me to learn
You have to first sum all the details from sales table and then use left join. Something like below might help you -
SELECT E2.*, Total_Sales
FROM (SELECT emp.employee_id
,SUM(total_price) Total_Sales
FROM (SELECT CASE WHEN e.employee_id IS NULL OR e.employee_id = 999 THEN 999 ELSE s.employee_id END employee_id
, e.first_name
, e.last_name
, e.address
, e.city
, s.total_price
FROM Sales s
LEFT JOIN employees e ON s.employee_id = e.employee_id) emp
GROUP BY emp.employee_id) s2
JOIN employees E2 on s2.employee_id = E2.employee_id
ORDER BY s.total_sales DESC
LIMIT 4
Here is the Db-Fiddle -
https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=748fd944384d80e717c08b3a2057f5c6
thank you Ankit Bajpai, this is the final answer
select employees.*, Total_Sales
from (select emp.Employee_Id,
sum(Total_Price) as 'Total_Sales'
from (select case when employees.employee_id is null or employees.Employee_Id = 999 then 999 else Sales.Employee_Id end Employee_Id,
Sales.Total_Price
from actiview_task.Sales
left join actiview_task.employees
on Sales.Employee_Id = employees.Employee_Id
where year(DATE)= 2015) emp
group by emp.Employee_Id) empGroupBy
join actiview_task.employees on empGroupBy.Employee_Id = employees.Employee_Id
order by Total_Sales desc
limit 4
Suppose we have some employees in each department.we have total 3 departments . Below is the sample source table named 'employee'
emp dept_id salary
A 10 1000
B 10 2000
C 10 3000
D 20 7000
E 20 9000
F 20 8000
G 30 17000
H 30 15000
I 30 30000
j 30 30000
k 30 17000
Here may same salary is exist in same department.
I use Wamp-server which has mysql-5.7.23
And I want to like:
B 10 2000
F 20 8000
G 30 17000
I think there are several way to solve the problem. Following solution from my side and works fine.
SELECT *
From employee e2
WHERE e2.salary = (SELECT distinct salary FROM employee where dept_id=e2.dept_id order by salary desc limit 1,1);
I need only second highest salary value with department wise which is the input array of next operation in my project. Finally I use
SELECT e2.dept_id, max(e2.salary)
From employee e2
WHERE e2.salary = (SELECT distinct salary FROM employee where dept_id=e2.dept_id order by salary desc limit 1,1)
group by e2.dept_id
I am trying to get the maximum salary for each year in a table with the attributes teamID, salary, yearID. Shouldn't be hard right? Here is my query:
SELECT teamID, MAX(tS.teamSalary), yearID
FROM
(SELECT
teamID,
sum(salary) AS teamSalary,
yearID
FROM salaries
GROUP BY teamID, yearID) tS
GROUP BY yearID;
The inner query works just fine, but the outer one is just reporting the first teamID for each group. What am I doing wrong?
Inner query output:
A 1 2000
B 1 2000
C 2 2000
A 2 2001
B 3 2001
A 2 2002
B 2 2002
Full query output:
A 1 2000
A 2 2001
A 2 2002
Desired output:
C 2 2000
B 3 2001
A 2 2002
First you get maxSalary by year, then you get the extra information:
SELECT teamID, salary, yearID
FROM salaries
JOIN
(SELECT MAX(salary) AS maxSalary,
yearID
FROM salaries
GROUP BY yearID) tS
ON ts.yearID = salaries.yearID
AND ts.maxSalary = salaries.salary
EDIT: Not sure if you want the max salary of some year, then to which team it belongs, or if you want the max salary by team and year. The second option is here:
SELECT MAX(salary) AS maxSalary, yearID, teamID
FROM salaries
GROUP BY yearID, teamID