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
Related
For an Employee table with columns EmpId, Name and Salary Table with columns Id, EmpId, Salary, where EmpId of salary table is Foreign Key of Employee Table.
Is it possible to write a SQL query so that the result contains each employee's name and the salary of all employees provided by a employee name?
Sample Data
Employee Table
EmpId Name
1 Alice
2 Bob
3 Charlie
4 Doug
Salary Table
Sample Data
Id EmpId Salary
1 1 1000
2 2 2000
3 3 3000
4 4 4000
Query I tried, but could not see, how I can write employee name say, Bob, where I want to fetch employee who have higher salary than Bob. Below query gives result with salary perspective, but how to fetch by employee name?
--Updated with Order By Clause, Thanks #Strawberry
SELECT s.salary
, e.empname
FROM
test.employee as e, test.salary as s
WHERE s.empid = e.empid AND s.salary > 2000
ORDER BY s.Salary DESC
LIMIT 3;
Here is the query with which you can fetch the salary of Bob:
SELECT Salary
FROM salary
WHERE EmpId = 2;
Now you want to fetch all names and salaries of employees who have higher salary than what you fount with the query above. Therefore you will use the above query as a nested one in WHERE. Here you go:
SELECT employee.EmpId, employee.Name, salary.Salary
FROM employee
JOIN salary ON employee.EmpId = salary.EmpId
WHERE salary > (
SELECT Salary
FROM salary
WHERE EmpId = 2
);
Edit - below in the comments you said that you want to filter by Name, not EmpId. Therefore you probably want to filtler with WHERE Name="Bob". The problem is that usually names are not unique. Therefore you may have two people named "Bob". If that is the case, you must figure out a solution which salary to get - they may be different.
Here is an example query that will get the salary of person named "Bob" who have the highest salary among all Bobs, then filter the other employees to have higher salary than whatever is fount:
SELECT employee.EmpId, employee.Name, salary.Salary
FROM employee
JOIN salary ON employee.EmpId = salary.EmpId
WHERE salary > (
SELECT MAX(Salary)
FROM salary
WHERE EmpId IN (
SELECT EmpId
FROM employee
WHERE Name="Bob"
)
);
Eventual issue may come if there is NO person named "Bob". The above query will return an empty set. If you want to print all people instead, you can do something like this:
SELECT employee.EmpId, employee.Name, salary.Salary
FROM employee
JOIN salary ON employee.EmpId = salary.EmpId
WHERE salary > (
SELECT IF(MAX(Salary) IS NULL, 0, MAX(Salary))
FROM salary
WHERE EmpId IN (
SELECT EmpId
FROM employee
WHERE Name="Bob"
)
);
Now if there is no "Bob", it will still print all people with salaries above 0.
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;
I need to display the code of the Department in the School table with the highest total Salary of any department.
So I tried this:
SELECT MAX(Total), dept
FROM (SELECT SUM(salary) AS Total, dept from school group by dept) AS Temp;
Which gives me the correct result; however it shows the field MAX(Total) and I just want the code of the department to show. What should I change?
You can do
SELECT a.dept FROM (
SELECT MAX(Total), dept
FROM (SELECT SUM(salary) AS Total, dept from school group by dept) AS Temp
) AS a;
If you need department code with max salary fund:
SELECT a.dept
FROM (SELECT SUM(salary) AS Total, dept from school group by dept) AS a
ORDER BY a.Total DESC
LIMIT 1;
there is two table
1 Employee
2 salary
Employee : eId, ename, salaryId
Salary : salaryId, eId, salary, date
salary table contains monthly record of employee salary,
like:
eId date salary
1 2015-jan-01 10000
1 2015-feb-01 10000
1 2015-mar-01 10000
1 2014-jan-01 10000
1 2014-feb-01 10000
1 2014-mar-01 10000
2 2015-jan-01 10000
2 2015-feb-01 10000
2 2015-mar-01 10000
2 2014-jan-01 10000
2 2014-feb-01 10000
2 2014-mar-01 20000
so the query is to give me highest paid employee in specific year for ex: 2014
so here using group by with date and sum of salary output is :
empid - empname - sum(salary)
2 - xyz - 40000
Try it with something like this ... i just took a glance to point you toward right direction, this query might need some fixing
Select TOP 1 emp.eid, emp.ename, sal.salary
from Employee emp
join Salary sal on emp.salaryID = sal.salaryID
where DATEPART(yy,sal.date) = 2014
order by sal.salary desc
Good luck
Something like this could work
with salaries as (
select to_char(date,'yyyy') y, eld, sum(salary) s_sal
from salary
group by to_char(date,'yyyy'), eld)
select eld
from salaries s
where s_sal = (select max(s_sal) from salaries where y=s.y)
and s.y='2014';
But you didn't specify DB, so there is Oracle syntax.
And I didn't join with Employee table, but believe it isn't hard to add it.
If the requirement is to get the list of highest paid employee in each year then this can be achieved by a "Ranking" logic.
Something like this:
SELECT
Year(SalaryMonthDate) AS [Payroll Year],
EID,
Sum(Amout) AS [Annual Salary],
RANK() OVER(PARTITION BY Year(SalaryMonthDate) ORDER BY Sum(Amout) DESC) [Rank]
FROM tblSalary
GROUP BY Year(SalaryMonthDate), EID
Hope this helps.
MySQL Code,
SELECT Top 1 E.ename EmployeeName,
MAX(salary) AS EmployeeSalary
FROM Employee E
INNER JOIN Salary S
ON E.salaryID = S.salaryID
WHERE YEAR(S.Date) = 2014
GROUP BY
E.eId,
E.ename
#highest paid emp in specific year
SELECT e.empId,e.empName,SUM(s.salary),s.salDate
FROM emp e INNER JOIN salary s ON e.empId = s.empId
WHERE YEAR(s.salDate)=2009 GROUP BY s.empId ORDER BY SUM(s.salary) DESC LIMIT 1;
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)