SQL Query to Find the Highest Salary of Each Department



SQL (Structured Query Language) is a programming language used to manage and interact with databases. In this case, the goal is to find the highest salary in each department from a table that contains employee data, including their salaries and the departments they work in.

We'll write an SQL query to find the highest salary for each department, which is useful for analyzing salary trends, setting pay standards, and making informed business decisions.

Finding the Highest Salary of Each Department

To find the highest salary in each department, use SQL's GROUP BY and MAX() functions. The GROUP BY groups the data by department, and MAX() finds the highest salary in each group.

Here's how to write the SQL query:

Step 1: Creating the Database

The first step is to create a database that will store our salary table. We will name the database tutorialspoint.

CREATE DATABASE tutorialspoint;

Output

Output Image

Step 2: Selecting the Database

Once the database is created, we need to select it for use. This ensures that any table we create or query we execute will be applied to the correct database.

USE tutorialspoint;

Output

Output Image

Step 3: Creating the Salary Table

Now, we will create a table called salary to store our employee data.

CREATE TABLE salary (
    emp_id INT PRIMARY KEY,           -- Employee ID
    emp_name VARCHAR(100),            -- Employee Name
    dept_id INT,                      -- Department ID
    dept_name VARCHAR(50),            -- Department Name
    salary DECIMAL(10, 2)             -- Employee Salary
);

Output

Output Image

Step 4: Inserting Sample Data

Now that our table is ready, let's insert some sample employee data into the salary table. This includes information like employee IDs, names, departments, and salaries.

INSERT INTO salary (emp_id, emp_name, dept_id, dept_name, salary)
VALUES
    (1, 'John Doe', 101, 'Sales', 55000),
    (2, 'Jane Smith', 102, 'Marketing', 60000),
    (3, 'Michael Brown', 101, 'Sales', 40000),
    (4, 'Sarah Johnson', 103, 'HR', 75000),
    (5, 'David Clark', 102, 'Marketing', 50000),
    (6, 'Emily White', 101, 'Sales', 45000),
    (7, 'Chris Green', 103, 'HR', 35000),
    (8, 'Linda Blue', 104, 'IT', 65000);

Output

Output Image

The table is created. You can check it by running:

SELECT * FROM salary;
Output Image

Step 5: Retrieving the Highest Salary in Each Department

Now, we are ready to write the SQL query that will retrieve the highest salary in each department. We'll use the MAX() function and the GROUP BY clause.

SELECT dept_name, MAX(salary) AS highest_salary
FROM salary
GROUP BY dept_name;

Output

Output Image

Step 6: Retrieving Employee Details with Highest Salary

If you want to see the details of the employee who has the highest salary in each department, you can use the following query:

SELECT E.emp_id, E.emp_name, E.dept_name, E.salary
FROM salary E
WHERE E.salary = (
    SELECT MAX(salary)
    FROM salary
    WHERE dept_name = E.dept_name
);
Output Image

Conclusion

In this article, we created a database and a table to store employee salary data. We then added sample data and wrote SQL queries to find the highest salary in each department. With these steps, you can easily check salary details and make more informed decisions about employee compensation.

Updated on: 2025-01-17T12:08:08+05:30

124 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements