Using MAX aggregate between two tables - sql

I have two tables, employer and position:
Employer
eID
eName
Position
eID
salary
I need to match my eID between the two tables, determine what the max salary is, and print only the eName. Any suggestions as to how I can do this? I have tried multiple ways, but nothing seems to work.
I am not sure where to put in the max(salary) function:
select eName
from employer, position
where employer.eID = position.eID

To get the name(s) of the people with the highest salary...
Using a JOIN:
SELECT e.name
FROM EMPLOYER e
JOIN POSITION x ON x.eid = e.eid
JOIN (SELECT MAX(salary) AS max_salary
FROM POSITION) y ON y.max_salary = x.salary
Using a subquery:
SELECT e.name
FROM EMPLOYER e
JOIN POSITION p ON p.eid = e.eid
WHERE p.salary = (SELECT MAX(salary)
FROM POSITION)

select e.ename,p.salary
from employer e ,position p
where p.salary=(select max(salary) from position)
and e.eid=p.eid

Join the tables, sort, and get the first one:
select top 1 e.eName, p.salary
from Employer e
inner join Position p on p.eID = e.eID
order by p.salary desc
(This returns the salary also, but you can of course remove it if you really don't want it.)

Related

Implementing Join in this query

I currently have a query to find if one employee is working on more than one assignment, and returns the names of the employees.
SELECT Eid, Count(Eid),
(SELECT Ename from Employee where Employee.Eid = Assignment.Eid) as EName
FROM Assignment
GROUP BY Eid HAVING Count(Eid) > 1;
Employee table has EID, and EName field. Assignment has EID and AssignmentNum.
Currently it works, but I think it can be written better with Inner Join, but I can't seem to compose a query with join that works. Any help would be appreciated.
You would just do:
SELECT a.Eid, Count(*), e.EName
FROM Assignment a JOIN
Employee e
ON e.Eid = a.Eid
GROUP BY a.Eid, e.name
HAVING Count(*) > 1;
SELECT e.ename, a.Eid, Count(a.Eid),
from Employee
Inner join assignment as a on e.Eid = a.Eid
GROUP BY a.Eid
HAVING Count(a.Eid) > 1;
You had the necessary join logic within the subquery.

TROUBLE IN MULTIPLE SUBSELECT QUERY

I need to find the name, function, officename and the salary of all employee with the same function as PETER or a salary greater or equal than the salary of SANDERS. order by function and salary.
There are two tables: office and employee
table office contains:
officenumber
name
city
table employee contains:
employeenumber
name
function
manager
sal
officenumber
this is my current SQL query:
SELECT NAME,
FUNCTION,
SAL
FROM EMPLOYEE
WHERE FUNCTIE = (SELECT FUNCTION
FROM EMPLOYEE
WHERE NAME = 'PIETERS')
I'm stuck with the query.
Assuming this is SQL Server (you never specified), something like this should work.
SELECT
e.name,
e.function,
e.sal,
o.name AS officename
FROM employee e
JOIN office o ON e.officenumber = o.officenumber
WHERE
e.function = (SELECT function FROM employee WHERE name = 'PIETERS') OR
e.sal >= (SELECT sal FROM employee WHERE name = 'SANDERS')
ORDER BY e.function, e.salary
You'll have to tweak this a bit if you're working with MySQL or something else.
Three things you need to do here:
1. join the two tables, since you need results from both tables
2. filter the results according to the two criterias
3. order the results:
The first part is easy, just need to join them according to the officenumber:
select e.name, e.function, o.name as officeName, e.salary from
employee e inner join office o
on e.officenumber = o.officenumber
second part, simple where clause:
where e.function = (select function from employee where name = 'PETER')
or e.salary >= (select salary from employee where name = 'SANDERS')
and the last, ordering:
order by e.function, e.salary
Putting it all together:
select e.name, e.function, o.name as officeName, e.salary from
employee e inner join office o
on e.officenumber = o.officenumber
where e.function = (select function from employee where name = 'PETER')
or e.salary >= (select salary from employee where name = 'SANDERS')
order by e.function, e.salary

Employees with largest salary in department

I found a couple of SQL tasks on Hacker News today, however I am stuck on solving the second task in Postgres, which I'll describe here:
You have the following, simple table structure:
List the employees who have the biggest salary in their respective departments.
I set up an SQL Fiddle here for you to play with. It should return Terry Robinson, Laura White. Along with their names it should have their salary and department name.
Furthermore, I'd be curious to know of a query which would return Terry Robinsons (maximum salary from the Sales department) and Laura White (maximum salary in the Marketing department) and an empty row for the IT department, with null as the employee; explicitly stating that there are no employees (thus nobody with the highest salary) in that department.
Return one employee with the highest salary per dept.
Use DISTINCT ON for a much simpler and faster query that does all you are asking for:
SELECT DISTINCT ON (d.id)
d.id AS department_id, d.name AS department
,e.id AS employee_id, e.name AS employee, e.salary
FROM departments d
LEFT JOIN employees e ON e.department_id = d.id
ORDER BY d.id, e.salary DESC;
->SQLfiddle (for Postgres).
Also note the LEFT [OUTER] JOIN that keeps departments with no employees in the result.
This picks only one employee per department. If there are multiple sharing the highest salary, you can add more ORDER BY items to pick one in particular. Else, an arbitrary one is picked from peers.
If there are no employees, the department is still listed, with NULL values for employee columns.
You can simply add any columns you need in the SELECT list.
Find a detailed explanation, links and a benchmark for the technique in this related answer:
Select first row in each GROUP BY group?
Aside: It is an anti-pattern to use non-descriptive column names like name or id. Should be employee_id, employee etc.
Return all employees with the highest salary per dept.
Use the window function rank() (like #Scotch already posted, just simpler and faster):
SELECT d.name AS department, e.employee, e.salary
FROM departments d
LEFT JOIN (
SELECT name AS employee, salary, department_id
,rank() OVER (PARTITION BY department_id ORDER BY salary DESC) AS rnk
FROM employees e
) e ON e.department_id = d.department_id AND e.rnk = 1;
Same result as with the above query with your example (which has no ties), just a bit slower.
This is with reference to your fiddle:
SELECT * -- or whatever is your columns list.
FROM employees e JOIN departments d ON e.Department_ID = d.id
WHERE (e.Department_ID, e.Salary) IN (SELECT Department_ID, MAX(Salary)
FROM employees
GROUP BY Department_ID)
EDIT :
As mentioned in a comment below, if you want to see the IT department also, with all NULL for the employee records, you can use the RIGHT JOIN and put the filter condition in the joining clause itself as follows:
SELECT e.name, e.salary, d.name -- or whatever is your columns list.
FROM employees e RIGHT JOIN departments d ON e.Department_ID = d.id
AND (e.Department_ID, e.Salary) IN (SELECT Department_ID, MAX(Salary)
FROM employees
GROUP BY Department_ID)
This is basically what you want. Rank() Over
SELECT ename ,
departments.name
FROM ( SELECT ename ,
dname
FROM ( SELECT employees.name as ename ,
departments.name as dname ,
rank() over (
PARTITION BY employees.department_id
ORDER BY employees.salary DESC
)
FROM Employees
JOIN Departments on employees.department_id = departments.id
) t
WHERE rank = 1
) s
RIGHT JOIN departments on s.dname = departments.name
Good old classic sql:
select e1.name, e1.salary, e1.department_id
from employees e1
where e1.salary=
(select maxsalary=max(e.salary) --, e. department_id
from employees e
where e.department_id = e1.department_id
group by e.department_id
)
Table1 is emp - empno, ename, sal, deptno
Table2 is dept - deptno, dname.
Query could be (includes ties & runs on 11.2g):
select e1.empno, e1.ename, e1.sal, e1.deptno as department
from emp e1
where e1.sal in
(SELECT max(sal) from emp e, dept d where e.deptno = d.deptno group by d.dname)
order by e1.deptno asc;
SELECT
e.first_name, d.department_name, e.salary
FROM
employees e
JOIN
departments d
ON
(e.department_id = d.department_id)
WHERE
e.first_name
IN
(SELECT TOP 2
first_name
FROM
employees
WHERE
department_id = d.department_id);
`select d.Name, e.Name, e.Salary from Employees e, Departments d,
(select DepartmentId as DeptId, max(Salary) as Salary
from Employees e
group by DepartmentId) m
where m.Salary = e.Salary
and m.DeptId = e.DepartmentId
and e.DepartmentId = d.DepartmentId`
The max salary of each department is computed in inner query using GROUP BY. And then select employees who satisfy those constraints.
Assuming Postgres
Return highest salary with employee details, assuming table name emp having employees department with dept_id
select e1.* from emp e1 inner join (select max(sal) avg_sal,dept_id from emp group by dept_id) as e2 on e1.dept_id=e2.dept_id and e1.sal=e2.avg_sal
Returns one or more people for each department with the highest salary:
SELECT result.Name Department, Employee2.Name Employee, result.salary Salary
FROM ( SELECT dept.name, dept.department_id, max(Employee1.salary) salary
FROM Departments dept
JOIN Employees Employee1 ON Employee1.department_id = dept.department_id
GROUP BY dept.name, dept.department_id ) result
JOIN Employees Employee2 ON Employee2.department_id = result.department_id
WHERE Employee2.salary = result.salary
SQL query:
select d.name,e.name,e.salary
from employees e, depts d
where e.dept_id = d.id
and (d.id,e.salary) in
(select dept_id,max(salary) from employees group by dept_id);
Take look at this solution
SELECT
MAX(E.SALARY),
E.NAME,
D.NAME as Department
FROM employees E
INNER JOIN DEPARTMENTS D ON D.ID = E.DEPARTMENT_ID
GROUP BY D.NAME

Using AVG() function between two tables

I have two tables, and I need to determine the company that offers the highest average salary for any position. My tables are as follows:
employer
eID (primary key), eName, location
position
eID (primary key), pName (primary key), salary)
The code I wrote determines all avg salaries that are higher than one, but I need to find only the highest average salary over all
Here is my code so far:
SQL> select eName
2 from Employer E inner join position P on E.eID = P.eID
3 where salary > (select avg(salary) from position);
This outputs all salaries that are higher than the lowest average, but I need only the highest average. I tried using avg(salary) > (select avg(salary) from position) but I received the error that group function is not allowed.
Any help or suggestions would be greatly appreciated!
Use:
SELECT x.eid,
x.ename,
x.avg_salary
FROM (SELECT e.eid,
e.ename,
AVG(p.salary) AS avg_salary,
ROWNUM
FROM EMPLOYER e
JOIN POSTITION p ON p.eid = e.eid
GROUP BY e.eid, e.ename
ORDER BY avg_salary) x
WHERE x.rownum = 1
Oracle 9i+:
SELECT x.eid,
x.ename,
x.avg_salary
FROM (SELECT e.eid,
e.ename,
AVG(p.salary) AS avg_salary,
ROW_NUMBER() OVER(PARTITION BY e.eid
ORDER BY AVG(p.salary) DESC) AS rank
FROM EMPLOYER e
JOIN POSTITION p ON p.eid = e.eid
GROUP BY e.eid, e.ename) x
WHERE x.rank = 1
Previously, because the question was tagged "mysql":
SELECT e.eid,
e.ename,
AVG(p.salary) AS avg_salary
FROM EMPLOYER e
JOIN POSTITION p ON p.eid = e.eid
GROUP BY e.eid, e.ename
ORDER BY avg_salary
LIMIT 1
select a.eid,
a.ename,
b.avg_salary
FROM EMPLOYER a
JOIN POSTITION b ON a.eid = b.eid
WHERE b.avg_salary =(SELECT max(x.avg_salary)
FROM (SELECT e.eid,
e.ename,
AVG(p.salary) AS avg_salary,
FROM EMPLOYER e
JOIN POSTITION p ON p.eid = e.eid
GROUP BY e.eid, e.ename) x
) y

Extra Fields with SQL MIN() & GROUP BY

When using the SQL MIN() function, along with GROUP BY, will any additional columns (not the MIN column, or one of the GROUP BY columns) match the data in the matching MIN row?
For example, given a table with department names, employee names, and salary:
SELECT MIN(e.salary), e.* FROM employee e GROUP BY department
Obviously I'll get two good columns, the minimum salary and the department. Will the employee name (and any other employee fields) be from the same row? Namely the row with the MIN(salary)?
I know there could very possibly be two employees with the same (and lowest) salary, but all I'm concerned with (now) is getting all the information on the (or a single) cheapest employee.
Would this select the cheapest salesman?
SELECT min(salary), e.* FROM employee e WHERE department = 'sales'
Essentially, can I be sure that the data returned along with the MIN() function will matches the (or a single) record with that minimum value?
If the database matters, I'm working with MySql.
If you wanted to get the "cheapest" employee in each department you would have two choices off the top of my head:
SELECT
E.* -- Don't actually use *, list out all of your columns
FROM
Employees E
INNER JOIN
(
SELECT
department,
MIN(salary) AS min_salary
FROM
Employees
GROUP BY
department
) AS SQ ON
SQ.department = E.department AND
SQ.min_salary = E.salary
Or you can use:
SELECT
E.*
FROM
Employees E1
LEFT OUTER JOIN Employees E2 ON
E2.department = E1.department AND
E2.salary < E1.salary
WHERE
E2.employee_id IS NULL -- You can use any NOT NULL column here
The second statement works by effectively saying, show me all employees where you can't find another employee in the same department with a lower salary.
In both cases, if two or more employees have equal salaries that are the minimum you will get them both (all).
SELECT e.*
FROM employee e
WHERE e.id =
(
SELECT id
FROM employee ei
WHERE ei.department = 'sales'
ORDER BY
e.salary
LIMIT 1
)
To get values for each department, use:
SELECT e.*
FROM department d
LEFT JOIN
employee e
ON e.id =
(
SELECT id
FROM employee ei
WHERE ei.department = d.id
ORDER BY
e.salary
LIMIT 1
)
To get values only for those departments that have employees, use:
SELECT e.*
FROM (
SELECT DISTINCT eo.department
FROM employee eo
) d
JOIN
employee e
ON e.id =
(
SELECT id
FROM employee ei
WHERE ei.department = d.department
ORDER BY
e.salary
LIMIT 1
)
Of course, having an index on (department, salary) will greatly improve all three queries.
The fastest solution:
SET #dep := '';
SELECT * FROM (
SELECT * FROM `employee` ORDER BY `department`, `salary`
) AS t WHERE IF ( #dep = t.`department`, FALSE, ( #dep := t.`department` ) OR TRUE );
Another approach can be using Analytical functions. Here is the query using analytical and ROW_NUM functions
select first_name, salary from (select first_name,salary, Row_NUMBER() over (PARTITION BY DEPARTMENT_ID ORDER BY salary ASC) as row_count from employees) where row_count=1;