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)
Related
I am not getting the logic of how to do Grouping by the Department and Finding the Average of the Salary of the Department and then filtering all the rows of the table by the values that is greater than Average salary of that department in SQL
Department
Salary
A
100
B
200
A
200
B
50
So avg of group A is 150
and avg of grp B is 125
My query should return :-
Department
Salary
B
200
A
200
You should please have a look how grouping works in SQL. This query will find the department and its average salary:
SELECT department, AVG(salary) salary FROM yourtable
GROUP BY department;
In order to find the departments having a higher salary, you can just join the whole table and this "average result" and choose those entries only that have a higher salary:
SELECT y.department, y.salary FROM yourtable y
JOIN (SELECT department, AVG(salary) salary FROM yourtable
GROUP BY department) average
ON y.department = average.department
WHERE y.salary > average.salary
ORDER BY y.department;
The order clause let department A appear before department B. In your description, it's sorted the other way. If you want to change this, you can write ORDER BY y.department DESC;
A last note: If there are NULL values in the salary table, they will note be considered by the average function. So if you have 10 null values, one row with a salary of 100 and one with a salary of 50, the average will be 75 and "ignore" the NULL values. If you don't want this, you need to replace the NULL values by the value you want. As example, you could write COALESCE(salary,0) within your query if you want to replace all NULL values by zero when calculating your average salary.
I am using a nested SQL query for finding the third lowest salary but the query doesn't show the correct name of the employee instead it shows gives the highest-paid employee name with the third-lowest salary. This is my query.
SELECT first_name, MIN(salary)
FROM employee
WHERE salary > (SELECT min(salary)
FROM employee
WHERE salary > (SELECT min(salary)
FROM employee)
);
You can use OFFSET/LIMIT to get the third lowest:
SELECT first_name, salary
FROM employee
WHERE salary
ORDER BY salary
LIMIT 1 OFFSET 2
The reason why your original query didn't work, is firstly, your select is:
SELECT first_name, MIN(salary)
which means that there is an implicit "group everything" here, and MySQL interprets the first_name as ANY(first_name).
Furthermore, it's extremely inefficient to do it that way.
SELECT firstname, salary
FROM employees
ORDER BY salary ASC
OFFSET 2 LIMIT 1;
select the data and order by salary ascending order. The minimum salary will be the first row, so limit the result to just one row, that is the person with the minimum salary!
SELECT first_name, salary
FROM employee
ORDER BY salery
LIMIT 1;
Here is the screenshot of the table and my attempt of this query
here's the query that I am running
SELECT department
FROM employees
where ( select sum(salary)
group
by department
having sum(salary) >=10000);
It returns 0 rows whereas it should return 'COE' as the sum of its employees is 14000
I think you just want this:
select department
from employees
group by department
having sum(salary) >= 10000;
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 am a beginner at SQL. I am trying to write a query which "provides the Total salary drawn by all people for each department if the total salary is greater than 300,000".
I have written some of it, but can't figure it out completely.
USE EMP_DB_01;
SELECT DEPTNAME, SUM(SALARY) AS 'Total Salary'
FROM DEPT, EMP
WHERE (SALARY > 300000) AND (DEPT.DEPTNO = EMP.DEPTNO)
GROUP BY DEPTNAME
Table is here
enter image description here
You should use a join between the DEPT and EMP could be based on DEPT.DEPTNO = EMP.DEPTNO ..and for total salary is greater than 300,000 you should use having and not where
Having filter the result of aggregated result .. where filter the rows values
this return the dept and the the related Total Salary when the sum is > 300.00
USE EMP_DB_01;
SELECT DEPTNAME, SUM(SALARY) AS 'Total Salary'
FROM DEPT
INNER JOIN EMP ON DEPT.DEPTNO = EMP.DEPTNO
HAVING SUM(SALARY) > 300000
GROUP BY DEPTNAME