title: SQL Server : Display Employees Having Max & Min Salary Department Wise?(Check Screenshot) Any Better Solutions
body: <p><b>Below is my Solution..Any Better Approach Please Suggest</b><br>
  <b>Below Solution generated from HR Database in SQL Server Migrated from Oracle Database</b></p>
  
  <p>This is Output<br>
  <img src="https://i.stack.imgur.com/T6uF2.jpg" alt="Screenshot"></p>
  
  <p>This is Table Structure
  <img src="https://i.stack.imgur.com/dIasQ.jpg" alt="Table Structure"></p>
  
  <pre><code>SELECT e.DEPARTMENT_ID,d.MaxSalary,es.FIRST_NAME,dm.MinSalary,esd.FIRST_NAME
  FROM 
      EMPLOYEES e
      JOIN (  SELECT department_id, MAX(salary) MaxSalary 
              FROM EMPLOYEES 
              GROUP BY DEPARTMENT_ID) d
      ON e.DEPARTMENT_ID=d.DEPARTMENT_ID
      JOIN (  SELECT first_name,DEPARTMENT_ID 
              FROM EMPLOYEES ess 
              WHERE SALARY IN (SELECT MAX(salary) FROM EMPLOYEES WHERE DEPARTMENT_ID=ess.DEPARTMENT_ID)) es
      ON e.DEPARTMENT_ID=es.DEPARTMENT_ID
      JOIN (  SELECT department_id,min(salary) MinSalary 
              FROM EMPLOYEES group by DEPARTMENT_ID) dm
      ON e.DEPARTMENT_ID=dm.DEPARTMENT_ID
      JOIN (  SELECT first_name,DEPARTMENT_ID
              FROM EMPLOYEES ess 
              WHERE SALARY in (SELECT MIN(salary) FROM EMPLOYEES WHERE DEPARTMENT_ID=ess.DEPARTMENT_ID )) esd
      ON e.DEPARTMENT_ID=esd.DEPARTMENT_ID
  GROUP BY 
      e.DEPARTMENT_ID
      ,d.MaxSalary
      ,es.FIRST_NAME
      ,dm.MinSalary
      ,esd.FIRST_NAME
  </code></pre>