I am a beginner of SQL, having this table instructor:
ID name dept_name salary
001 A d01 1000
002 B d02 2000
003 C d01 3000
...
I am writing a code to find people who have highest salary in each department like:
name dept_name
C d01
B d02
I do know how to find maximum value
but I have no idea how to use it by according dept_name for all each department.
This will ensure that only records which are the highest salary for each department are returned to the result set.
Using
SELECT name, MAX(salary)like other answerers have used won’t work. UsingMAX()will return the highest salary for each department, but the name will not necessarily be related to that salary value.For example,
SELECT MIN(salary), MAX(salary)is most likely going to pull values from different records. That’s how aggregate functions work.