Skip to content
Advertisement

Simple SQL query returning null value

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:

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

Advertisement

Answer

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:

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:

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:

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement