How to get the sum of distinct IDs and their respective salaries?
ID Salary
1 1000
2 2000
1 1000
2 2000
In above I want to get the out like this
Total salary
3000
I tried and made the output like this
ID Salary
1 1000
2 2000
select distinct(id), sum (salary) from employee group by id
Main issue is that I am not able to total after applying distinct to the ID.
Want to remove the duplicate entries of id and sum of there distinct ID?
Check this Query is,
Query
SELECT SUM (
DISTINCT salary
) FROM employee
Result
SUM(DISTINCT SALARY)
--------------------
3000
Check Demo SQLFiddle
You can do also by this way,
SELECT SUM(salary)
FROM (SELECT DISTINCT id, salary
FROM employee
GROUP BY id,salary
) AS emp
Check this Demo SQLfiddle
Try this 100% work
SELECT SUM(salary) as "Total salary"
FROM(SELECT DISTINCT id, salary FROM
employee GROUP BY id,salary ) as e
Try Like This
select sum(salary) from( select distinct id, salary from
employee) as t
Simplest way would be, remove duplicate records and write simple query. Second way is this query, in which I have taken MAX(Highest) salary for each employee in sub query, and then applied SUM() from calculating total salary.
SELECT SUM (sal) from
(
SELECT MAX (salary) as sal
FROM employee GROUP BY id
) as tbl
Try this:
It will work definitely
It will sum of salary with same id only.
Select SUM(salary) as total from employee group by id;
Related
I am trying to solve below leet code problem.
https://leetcode.com/problems/second-highest-salary
What is wrong with this answer? Below answer is not accepted :(
select t.salary as SecondHighestSalary from
(
select salary
from employee
order by salary desc
limit 1 offset 1
) as t
Both the below answers are accepted
We can use subquery as shown below:
SELECT MAX(salary) AS secondhighestsalary
FROM employee
WHERE salary < (SELECT MAX(salary)
FROM employee);
or alternatively you can use temporary table:
with temp as
(
SELECT MAX(salary) as salary FROM employee
)
select max(salary) as secondhighestsalary from employee where salary <(select salary from temp);
This is Accepted.
select salary as SecondHighestSalary
from employee a
where 1 = (select count(1) from employee b where b.salary < a.salary )
Your query will not work if the lowest salary appears twice in the table.
Above query uses co-related subquery, means, for each row in outer table the subquery (or inner query) will be executed once.
Moreover, this will work for Nth highest salary. If 10th highest salary would have been asked then just replace the 1 in the above query with 9.
i have 3 table,
- Employee (idemployee, iddivision, firstname, lastname)
- Salary (idsalary, idemployee, dateadded, amount)
- division (iddivision, divisionname)
i want to display the first name that have the highest amount between january and april
so far i tried
SELECT firstname, MAX(Total) FROM (
SELECT firstname,SUM(amount) AS Total
FROM salary
JOIN employee ON employee.idemployee=salary.idemployee
WHERE dateadded BETWEEN "2019-1-1" AND "2019-4-1"
GROUP BY employee.idemployee) as t
but the employeeid sql show is wrong. why?
SELECT employee.firstname, SUM(salary.amount) AS Total
FROM salary
JOIN employee
ON employee.idemployee=salary.idemployee
WHERE salary.dateadded BETWEEN "2019-01-01" AND "2019-04-01"
GROUP BY employee.firstname
ORDER BY 2 DESC
LIMIT 1
You are filtering by the range you want, then you sum the amounts in that range, grouping by the employee. If you order by that sum and get just the first row, it must he one what you are looking for.
Even better, if your employees just have a name in the firstname attribute, you have the risk to group by the same name wrongly. So, to identify better the employee, I would add the idemployee to the group by sentence. Like this:
SELECT employee.idemployee, employee.firstname, SUM(salary.amount) AS Total
FROM salary
JOIN employee
ON employee.idemployee=salary.idemployee
WHERE salary.dateadded BETWEEN "2019-01-01" AND "2019-04-01"
GROUP BY employee.idemployee,employee.firstname
ORDER BY 3 DESC
LIMIT 1
Do you mean you want it to be ordered from greatest to least?
SELECT firstname, Total FROM (
SELECT firstname,SUM(amount) AS Total
FROM salary
JOIN employee ON employee.idemployee=salary.idemployee
WHERE dateadded BETWEEN "2019-1-1" AND "2019-4-1"
GROUP BY employee.idemployee) as t
order by desc Total
Employee table"
empno,
ename,
sal_id,
emp_id,
salary,
month
SALARY table:
sal_id,
emp_id,
salary,
month
I am trying to make query for getting all employee from employee table +
In salary table, there are multiple entry (may be not) of emp_id.
I want employee list with their latest salary( or last month salary)
My current query is :
SELECT * FROM emp LEFT JOIN salary ON emp.empno = salary.emp_id GROUP BY empno ORDER BY salary.sal_id DESC
But I am getting emp list with first salary, I want with latest salary.
Help me :(
Emploee table
Salary table
You are ordering by sal_id instead you might want to order by a field in salary which represents its month.
SELECT emp.empno, emp.name, (
SELECT sal
FROM salary
WHERE salary.empno = emp.empno
ORDER BY salary.sal_id DESC LIMIT 1
)
FROM emp
I have come with solution after a lot R&D and googling .
It is not possible in single query, we have to apply 2 query
SELECT * FROM emp LEFT JOIN salary ON salary.emp_id=emp.empno WHERE salary.sal_id IN ( SELECT MAX(salary.sal_id) FROM salary GROUP BY salary.emp_id ) GROUP BY salary.sal_id ORDER BY salary.sal_id DESC
I have table tbl_emply containing fields Salary, DNO, and EID for each employ. I need to find average Salary of each DNO that has more than two employees.
I have tried queries like
select avg(salary),DNO
from tbl_emply
where count(select * from tbl_emply group by(DNO)>2);
select avg(salary),DNO
from tbl_emply
group by(DNO);
But these all gave me invalid use of group by. How to get the result?
Use HAVING
SELECT AVG(salary), DNO
FROM tbl_emply
GROUP BY DNO
HAVING COUNT(*) > 2
try this,
select avg(Salary),DNo from tbl_emply group by DNo having count(*)>2;
I'm a student and am working on a MySql assignment given to us for the holidays.
I have a table that looks something like this :
name dept salary
A Sales 100
B Marketing 200
C Sales 800
(Sorry I'm new to stackexchange so I don't know how to display a table.)
The question for the query is : find the dept that is being paid the max salary.
I entered the following query :
SELECT dept
, SUM(salary)
FROM emp
GROUP BY dept
HAVING MAX(SUM(salary));
But I am getting the following error:
'Invalid use of group function error'.
In case salary sum is unique, you can calculate sum for each dept then order records in descending order by aggregated salary and fetch the first record (with the greatest salary):
select dept
, sum(salary) as salary
from tbl
group by dept
order by salary desc
limit 1
In case salary sum can be the same for multiple depts you can calculate salary sum for each dept , then find maximum salary sum the same way as explained above and using a having clause validate if salary sum for each group is equal to the maximum salary sum:
select dept
from tbl
group by dept
having sum(salary) = ( select sum(salary) as salary
from tbl
group by dept
order by salary desc
limit 1)