MySQL query with avg and count greater than a value - mysql

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;

Related

I am trying to get latest month salary from salary table, where multiple entry of employeeID in salary table for each month

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

Selecting with the HAVING clause

I wrote a quick query
select dno from Employee having max(salary)
In my table, the maximum salary in 96000 and is associated with dno 8. But for some reason, that select statement gives dno 6.
http://hastebin.com/wewahiloki.vhdl
I feel like this is really obvious but I can't figure it out.
having don't work properly without group by
If you want all the dno with max salary you can use this
select dno from Employee where salary = (select max(salary) from Employee);
If you one to get the "dno" that have the highest salary you can use the following query:
SELECT dno from employee ORDER BY Salary DESC LIMIT 1
The reason is that "having" is used when your are using aggregation operations. See: https://stackoverflow.com/a/2905312/1641558

why query to find the 2nd highest salary of the employee is not working in mysql 5.1?

I am trying to find the salary of the second-highest paid employee.
My expected output is 9000 but it prints 14000. I am unable to find my mistake. Kindly help me
Try this query
select max(salary) from table_name
where salary< (select max(salary) from table_name)
Fiddle
You could use this query also ,
SELECT MAX(salary) FROM employees WHERE salary < (SELECT MAX(salary) FROM employees)
Simply use sub query to get max salary from the employees table and check against same to get second highest.
Select * from table order by salary desc limit 2,1

Find Total salary using distinct and sum?

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;

MYSQL find max value from each department

I have a table like this
employeeid | departmentid | bossid | name | salary
I need to find the max salary, but for each of the different departmentid's.
Use GROUP BY with the aggregate function MAX():
SELECT MAX(salary), departmentid FROM your_table GROUP BY departmentid;
You need to use MAX and Group By:
SELECT DepartmentId, MAX(SALARY) AS Salary
FROM EmployeesTable
GROUP BY DepartmentId
select departmentid, max(salary) as salary
from yourtab
group by departmentid
You can use the group by clause to build a group around departmentid. For that group you can use aggregate functions to run operations on the data of each group, like max()
select departmentid,
max(salary) as max_sal
from employees
group by departmentid