1

I just started studying SQL queries. I am practicing on this site: https://www.techonthenet.com/sql/joins_try_sql.php

I want to find:

"the name of the employee with the highest salary for every department".

My query is:

SELECT first_name, max(salary) FROM employees, departments 
WHERE departments.dept_id=employees.dept_id
GROUP BY employees.dept_id

And I get null value for first_name : query result I understand that the problem is due to the group by expressions. But how can I solve this?

Tables: query result

7
  • 1
    Do you have a first_name set in the employees table for all the records? Commented Nov 22, 2019 at 15:59
  • We need to see the structure of these two tables in order to help further. Commented Nov 22, 2019 at 15:59
  • Yes I have. You can check the tables data in the first link if you want. Or I can post them for you Commented Nov 22, 2019 at 16:00
  • Ok I post the two tables. Thanks Commented Nov 22, 2019 at 16:00
  • I posted the tables. Commented Nov 22, 2019 at 16:04

4 Answers 4

1

You can alternatively use row_number() like below, you don't need to join to departments table unless you need to show the name of the department:

Select e.*
from employees e
INNER JOIN
(
   SELECT e.id, e.dept_id. e.first_name, 
          rn=row_number() over (partition by e.dept_id order by e.salary desc)
   FROM employees e 
) x ON x.id = e.id
where x.rn = 1

EDIT

(Since OP does not want to use row_number() function amd it turned out the query will be used in mysql instead of sql server) -> Can you please try this:

select em.*
from employees em, (
    Select dept_id, max(salary) salary
    from employees e
    group by dept_id
) x on x.dept_id=em.dept_id and x.salary = em.salary

That should work but the online compiler does not accept join with a sub-query as far as I understand. Easiest solution I can think of, in this case:

select em.*
from employees em
where salary = (select max(salary) from employees em2 where em.dept_id = em2.dept_id)
Sign up to request clarification or add additional context in comments.

3 Comments

Sorry, I haven't written in the post: I only want to use SELECT, WHERE, JOIN , GROUP BY, HAVING... Thanks!
Updated my answer. @l000000l
I tried this also, as a mysql query, but it does not work. I'm practicing on this site techonthenet.com/sql/joins_try_sql.php , i think i need simpler expressions to work.
1
SELECT top 2 first_name,max(salary) FROM employees, departments
WHERE departments.dept_id=employees.dept_id

GROUP BY first_name
order by max(salary) desc

2 Comments

Can you provide more information instead of a code-only answer?
@chevybow first_name is selected but not included in the Group by clause so i added it in group by and ordered them by max salary. I think somehow the online compiler rather than showing the error it is showing null in the place of that.
1

try this:

SELECT e.first_name, e.salary FROM employees as e
INNER JOIN departments as d ON d.dept_id=e.dept_id
WHERE e.salary IN (SELECT max(salary) FROM employees GROUP BY dept_id)

Comments

0

Try this:

select first_name, b.dept_id, salary  from employees a,
(
SELECT employees.dept_id, max(salary) as salary FROM employees, departments 
WHERE departments.dept_id=employees.dept_id
GROUP BY employees.dept_id
 )b where a.salary = b.salary and a.dept_id= b.dept_id

2 Comments

Its the max salary for EVERY department ... This only returns the max salary
@zip what happens if different people in different departments have same amount of salary?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.