how to retrieve the employee who got maximum bonus? - sql

I have to display the employee detail who has got the maximum bonus(one with employee details and another table with bonus details). Here I have created a 'performance bonus' column to sum up the multiple bonuses. How to retrieve the employee from that column?
select e.Employee_id,
e.First_name,
e.Department,
e.Salary,
coalesce((select sum(b.Bonus_Amount)
as Bonus-- Let's sum up all Employee's the bonuses
from Employee_Bonus_Table b
where b.Employee_ref_id = e.Employee_Id), 0) [Performance_bonus]
from Employee_Table e

If I understood the task correctly, then the bonuses can be repeated, is that right?
then we must first sum up all the bonuses by employee, then sort from largest to smallest and get the first one from the list
--- for examples:
with Employee_Bonus_Table as(
select Bonus_Amount = 1 ,Employee_ref_id = 1
union select Bonus_Amount = 1000 ,Employee_ref_id = 2
union select Bonus_Amount = 2000 ,Employee_ref_id = 2
),Employee_Table as (
select Employee_id=1
,First_name='First_name'
,Department= 'Department'
,Salary = 1000
UNION select Employee_id=2
,First_name='First_name2'
,Department= 'Department2'
,Salary = 2000
)
--reslut query:
select top 1
e.Employee_id,
e.First_name,
e.Department,
e.Salary,
b.sumBonus_Amount
from Employee_Table e
join (select sumBonus_Amount = sum(Bonus_Amount), Employee_ref_id
from Employee_Bonus_Table
group by Employee_ref_id
) b on b.Employee_ref_id = e.Employee_Id
order by b.sumBonus_Amount desc

Related

DISTINCT not giving expected result

ID FirstName LastName Gender Salary
1 Ben Hoskins Male 70000
2 Mark Hastings Male 60000
4 Ben Hoskins Male 70000
8 John Stanmore Male 80000
While running the query:
select *
from Employees
where Salary > (SELECT AVG(distinct SALARY) FROM employees)
It is displaying 2 records for 7000 it should display one. Why is it showing 2 records if I have used distinct?
You have used distinct the subquery, not in the outer query, so the outer query still sees the data with the duplicates.
You could solve this using:
select e.FirstName, e.LastName, e.Gender, e.Salary
from Employees e
where e.Salary > (SELECT AVG(distinct e2.SALARY) FROM employees e2);
However, something seems seriously wrong with the data model if you have rows with such duplicates. The data should be fixed.
In the meantime, you can work around the problem. You can phrase your query using a CTE that eliminates the duplicates:
with e as (
select e.*
from (select e.*,
row_number(). over (partition by firstname, lastname, gender order by id desc) as seqnum
from employees e
)
where seqnum = 1
)
select e.*
from e
where e.salary > (select avg(salary) from e)
It looks like you have duplicate rows in the table (that only differ by their id), so I am going to assume that you want distinct over the name, gender and salary rather than the salary only.
You seem to want distinct both in the outer and inner query:
select distinct firstname, lastname, gender, salary
from employees
where salary > (
select avg(salary)
from (
select distinct firstname, lastname, gender, salary
from employees
) e
)
If your database supports window functions, this can be shortened:
select *
from (
select e.*, avg(salary) over() as avg_salary
from (
select distinct firstname, lastname, gender, salary
from employees
) e
) e
where salary > avg_salary

How to find dept name that has the highest average salary within two tables

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

When can i use having count or just Greater than or Less than

i'm just curios why i can't use greater than in the following exercise
Write a query in SQL to display the full name (first and last name) of manager who is supervising 4 or more employees
The right code for my sub-query is the following ( by using having count )
SELECT e.first_name , e.last_name ,e.department_id
FROM employees e
WHERE e.employee_id IN
(SELECT e.manager_id
FROM employees e
GROUP BY e.manager_id
HAVING COUNT(*)>=4);
This code returns 15 rows
And the wrong code with greater than
SELECT e.first_name , e.last_name ,e.department_id
FROM employees e
WHERE e.employee_id IN
(SELECT e.manager_id
FROM employees e where e.manager_id>=4);
This code returns 18 rows
You query to get the desired output is incorrect.
In where clause you are checking the manager id >=4 or not instead of count.
For your OP this is one of my solution to get a clear understanding
SELECT E.FIRST_NAME, E.LAST_NAME,E.DEPARTMENT_ID
FROM EMPLOYEES E
WHERE E.EMPLOYEE_ID IN(
SELECT MANAGER_ID FROM
(
SELECT M.MANAGER_ID,COUNT(*) CNT FROM EMPLOYEE M
GROUP BY M.MANAGER_ID
)
WHERE CNT >= 4
)
The where clause will return all the managers that has manager_id>4.
Whereas the Having clause will return the total count or count greater than 4

SQL - Using Sum command multiplies the salary for the same person

I'm trying to increase the salary for those employees who treat at least 2 patients by 10%. My problem is that the salary first multiplies by 2 for every patient they treat and then multiplies by 10% at the end. For exampel if the employee earns 25.000 and treats 3 people the new salary becomes 82.500.
select distinct t.empNbr, e.Salary, sum(e.Salary*1.1) as NewSalary from Treats t
inner join Employee e
on e.empNbr=t.empNbr
WHERE t.empNbr IN
(
SELECT empNbr
FROM Treats
GROUP BY empNbr
HAVING COUNT(*) >= 2)
group by t.empNbr, e.Salary
CROSS APPLY should help:
SELECT e.empNbr,
e.Salary,
e.Salary*1.1 as NewSalary
FROM Employee e
CROSS APPLY (
SELECT empNbr
FROM Treats
WHERE e.empNbr = empNbr
GROUP BY empNbr
HAVING COUNT(*) > 1
) as t
The t part gets empNbr we need. Then we select empNbr and salary from Employee table and do math :)
One more way:
SELECT TOP 1 WITH TIES
e.empNbr,
e.Salary,
e.Salary*1.1 as NewSalary
FROM Employee e
INNER JOIN Treats t
ON e.empNbr = t.empNbr
ORDER BY
CASE WHEN COUNT(t.empNbr) OVER (PARTITION BY t.empNbr ORDER BY t.empNbr) > 1 THEN 1 ELSE 0 END DESC,
ROW_NUMBER() OVER (PARTITION BY t.empNbr ORDER BY t.empNbr)
This should be the right query. Let me know if this works
select empNbr, Salary, sum(Salary*1.1) as NewSalary
from employee
where empNbr in (select empNbr
from Treats
group by empNbr
having count(*) >=2) ----- Ordered as a code
Christopher, You can use below query to get the result,
SELECT t.empNbr,
e.Salary,
(e.Salary * e.count +(e.Salary/10) ) as NewSalary
from Treats t
INNER JOIN
(SELECT empNbr, COUNT(*) AS count Employee GROUP BY empNbr) e
ON e.empNbr=t.empNbr
AND e.count >=2
Explination
1. We can calculate result without having clause2.In the inner join, empNbr and the count of that employee in derived3.Using this count in the select query, the current salary is multiplied and 10% is added with the salary
Hope this is what you need. Any issues, feel free to ask

Select all the people that have the same salary

I have problems finding a solution to an SQL query. This is probably a very obvious beginner question but I can't seem to get the results that I'm after. I have a table that looks something like the following:
|Name |Station|Salary|
|Bob |1 |2000 |
|Steve|2 |1750 |
|Mark |3 |2050 |
|Lisa |4 |2200 |
|Hans |5 |2000 |
I would like to select the names of the people in this table that have the same salary. The result should of course be Bob and Hans.
If you join the table against itself on Salary but where the names are separate then this should give you any matching salaried people:
SELECT s1.Name, s1.Station, s1.Salary
FROM Staff s1
INNER JOIN Staff s2 ON s1.Salary = s2.Salary AND s1.Name <> s2.Name
Here's a SQLFiddle to show it in action
SELECT Name
FROM table1
WHERE Salary IN (
SELECT Salary
FROM table1
GROUP BY Salary
HAVING COUNT(*) > 1
)
If you determine the salary bands which have more than one employee, you can then join back to it as a derived table:
SELECT e.Name, e.Station
FROM Employee e
INNER JOIN
(
SELECT Salary
FROM Employee
GROUP BY Salary
HAVING COUNT(*) > 1
) grp ON e.Salary = grp.Salary;
SqlFiddle here
Most databases support window functions. The simple method to do this is to count the number of people that have a given salary. Then choose those people. Here is an easy method:
select t.*
from (select t.*, count(*) over (partition by salary) as salarycnt
from table t
) t
where salarycnt > 1
order by salary;
TRY THIS:
SELECT E1.Name, E1.Salary
FROM Employee E1, Employee E2
WHERE E1.Salary = E2.Salary
AND E1.Name <> E2.Name
http://sqlfiddle.com/#!2/1e34b
Use below Query:
with datatab as
(
select 'Bob' Name, 1 Station, 2000 Salary from dual union
select 'Steve' Name, 2 Station, 1750 Salary from dual union
select 'Mark' Name, 3 Station, 2050 Salary from dual union
select 'Lisa' Name, 4 Station, 2200 Salary from dual union
select 'Hans' Name, 5 Station, 2000 Salary from dual union
select 'Test' Name, 6 Station, 1750 Salary from dual
)
SELECT NAME, sTATION, SALARY FROM DATATAB
WHERE SALARY IN
(
SELECT Salary
FROM datatab
GROUP BY Salary
HAVING COUNT(1) > 1
);
As someone suggested in Edits, The Query would be:
SELECT NAME, sTATION, SALARY FROM TABLE_NAME
WHERE SALARY IN
(
SELECT Salary
FROM TABLE_NAME
GROUP BY Salary
HAVING COUNT(1) > 1
)
Select a.name, a.salary from
(
Select count(salary) as cnt, salary from staff group by salary having count(salary) > 1
)as X
Inner join staff a on a.Salary=x.salary
order by a.salary:
Use aliases when you have to put one and the same table two or more times:
select t1.Name,
t2.Name,
t1.Salary
from MyTable t1,
MyTable t2
where t1.Station > t2.Station and -- if Station is id
-- t1.Name > t2.Name -- if Name is in fact an id
t1.Salary = t2.Salary
Try this:
select COUNT(*) as NumberOfPerson,salary from tblEmployee group by salary having COUNT(*)>1
For MySQL:
Select * from worker
where salary in (select salary from worker
group by salary
having count(1)>1);
lets say table name is: employee
select distinct e.name, e.salary from employee e, employee e1 where
e.salary = e1.salary and e.name != e1.name;
First Solution :
Select e1.name from employee e1
inner join employee e2 on e1.salary=e2.salary AND e1.name <> e2.name
Second easy solution:
Select name from employee
where salary In (Select salary from employee
group by salary
having count(*)>1)
SUPPOSE WE HAVE TABLE NAMELY AS EMP3 WHICH ONTEIN COLUMNS AS FIRST_NAME,LAST_NAME,SALRY,DEPARTMENT_ID,COMMISSION ETC.
NOW WE HAVE TO SELECT RECORDS OF EMPLOYEES WHO HAVE THE SAME SALARY.
LET'S GET OUTPUT WITH THE HELP OF SELF JOIN.
HERE WE ARE JOINING THE SAME TABLE WITH ITSELFMTO GET RECORDS WHO HAVE THE SAME SALARY.
SELECT E1.FIRST_NAME AS FN,E1.SALARY FROM EMPLOYEES E1 INNER JOIN EMPLOYEES E2 ON E1.SALARY=E2.SALARY;
Treating same table as a separate two table.
SELECT DISTINCT T.username, T.salary
FROM account T, account T
WHERE T.salary = T.salary
AND T.username <> T.username
ORDER BY salary;
Do you want the quantity of people with the same salary.
SELECT count(*) FROM table GROUP BY salary