How to get the top 1 row of each group in SQL
We can retrieve the top 1 row of each group in SQL by using a subquery with a JOIN. We can do it by following the steps given below:
Create a table
First, we will create a table. We make a table called employees with the appropriate fields (department_id, employee_id, employee_name, and salary) with the help of the query given below:
CREATE TABLE employees (department_id INT,employee_id INT,employee_name VARCHAR(50),salary DECIMAL(10, 2),PRIMARY KEY (department_id, employee_id));
Insert values in a table
Now, we will insert data in the table created above. We insert data using the SQL statements given below:
-- Inserting data into the employees tableINSERT INTO employees (department_id, employee_id, employee_name, salary)VALUES(1001, 4001, 'Alice', 60000.00),(1001, 4002, 'Bob', 75000.00),(1002, 4003, 'Carol', 80000.00),(1002, 4004, 'David', 72000.00),(1003, 4005, 'Emily', 90000.00),(1003, 4006, 'Frank', 95000.00);
Retrieve the top 1 row of each group
We consider the department a group according to the inserted data above. We can use the SQL statement below to get the top 1 (highest-paid) employee for each department:
SELECT e.department_id AS Dep_ID, e.employee_id AS Emp_ID, e.employee_name AS Name, e.salary AS SalaryFROM employees eJOIN (SELECT department_id, MAX(salary) AS max_salaryFROM employeesGROUP BY department_id) max_salariesON e.department_id = max_salaries.department_id AND e.salary = max_salaries.max_salary;
Try it out
The output of the code given above can be observed below after clicking the “Run” button.
CREATE TABLE employees (department_id INT,employee_id INT,employee_name VARCHAR(50),salary DECIMAL(10, 2),PRIMARY KEY (department_id, employee_id));INSERT INTO employees (department_id, employee_id, employee_name, salary)VALUES(1001, 4001, 'Alice', 60000.00),(1001, 4002, 'Bob', 75000.00),(1002, 4003, 'Carol', 80000.00),(1002, 4004, 'David', 72000.00),(1003, 4005, 'Emily', 90000.00),(1003, 4006, 'Frank', 95000.00);SELECT e.department_id AS Dep_ID, e.employee_id AS Emp_ID, e.employee_name AS Name, e.salary AS SalaryFROM employees eJOIN (SELECT department_id, MAX(salary) AS max_salaryFROM employeesGROUP BY department_id) max_salariesON e.department_id = max_salaries.department_id AND e.salary = max_salaries.max_salary;
Code explanation
-
Lines 1–7: We create a table called
employeescomprising four columns:department_idas an integer for department identification,employee_idas an integer for employee identification,employee_nameas a variable character column with a maximum 50-character capacity for holding employee names, andsalaryas a decimal column with a precision of 10 digits and two decimal places for storing salaries. The table enforces data integrity by applying aPRIMARY KEYconstraint on the composite ofdepartment_idandemployee_id, ensuring the uniqueness of department-employee pairs. -
Lines 9–16: The provided code populates the
employeestable by inserting data. EachVALUESclause corresponds to a distinct set of values representing a single row to be added to the table. -
Lines 18–25: The provided code retrieves data from the
employeestable using theSELECTstatement to fetch specific columns, employing aliases to rename the result set columns. The main query involves joining theemployeestable (e) with a subquery (max_salaries) based on department and salary. The subquery employs theMAX()aggregate function to determine the maximum salary for each department, grouped accordingly. The output presents employees with the highest salary in their respective departments, utilizing aliases to rename columns:Dep_IDfordepartment_id,Emp_IDforemployee_id,Nameforemployee_name, andSalaryforsalary.
Free Resources