List the employees with same salary - mysql

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;

Related

How to get the top n entries for each column grouped by another column? [duplicate]

This question already has answers here:
Get top n records for each group of grouped results
(12 answers)
Closed 1 year ago.
Let's say I have this table called Employees:
Id
Name
Salary
DepartmentId
1
Joe
85000
1
2
Henry
80000
2
3
Sam
60000
2
4
Max
90000
1
5
Janet
69000
1
6
Randy
85000
1
7
Will
70000
1
And for each department id, I want the top three salaries. So the result should be:
DepartmentId
Name
Salary
1
Max
90000
1
Joe
85000
1
Randy
85000
1
Will
70000
2
Henry
80000
2
Sam
60000
So for each department id, the top three salaries are returned, and if there are duplicate salaries in the top three, the duplicates are returned too and the limiting factor is top three unique salaries. How to implement this?
You would use dense_rank():
select e.*
from (select e.*,
dense_rank() over (partition by departmentid order by salary desc) as seqnum
from employees e
) e
where seqnum <= 3;
WITH cte AS (
SELECT DepartmentId,
Salary,
ROW_NUMBER() OVER (PARTITION BY DepartmentId ORDER BY Salary DESC) rn
FROM Employees
)
SELECT *
FROM Employees
WHERE Salary >= ( SELECT MIN(Salary)
FROM cte
WHERE Employees.DepartmentId = cte.DepartmentId
AND rn <= 3 )
it is well described in manuals and references, but you haven't even linked any of the manuals or provided any information at all in your answer. – Martin
MySQL 8.0 Reference Manual / ... / WITH (Common Table Expressions)
MySQL 8.0 Reference Manual / ... / Window Function Descriptions # ROW_NUMBER() function
MySQL 8.0 Reference Manual / ... / Subqueries # Correlated Subqueries

How to get the count of the people have a better salary than the current tuple

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

Select max of a column based on another column 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

SQL Server Rules of Precedence

What is the difference between two queries :
Query #1:
select *
from Employee
where Job_id = 'SA_REP' OR Job_id = 'AD_PRES' AND salary > 15000
Output:
EmpId Last_name Job_ID Salary
1 King AD_PRES 24000
2 Abel SA_REP 11000
3 Taylor SA_REP 8600
4 Grant SA_REP 7000
Query #2:
select *
from Employee
where (Job_id = 'SA_REP' OR Job_id = 'AD_PRES') AND salary > 15000
Output:
EmpId Last_name Job_ID Salary
1 King AD_PRES 24000
In the first query you will get all Employees with a Job_ID of 'SA-REP' no matter what their salary is and Employees with a Job_ID of 'AD_PRES' and salary > 15000. In the second query you will get Employees whose salary > 15000 and also have a Job_ID of 'SA_REP' or 'AD_PRES'.

SELECT MAX() FROM TABLE using GROUP BY

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);