Setup an Environment
Let’s imagine we have a table named Employees with the following schema:
CREATE TABLE Employees (
EmployeeID INT PRIMARY KEY,
FirstName VARCHAR(50),
LastName VARCHAR(50),
Department VARCHAR(50),
Salary DECIMAL(10, 2)
);
INSERT INTO Employees (EmployeeID, FirstName, LastName, Department, Salary)
VALUES
(1, 'John', 'Doe', 'Engineering', 65000.00),
(2, 'Jane', 'Smith', 'Marketing', 55000.00),
(3, 'Michael', 'Johnson', 'Sales', 62000.00),
(4, 'Emily', 'Brown', 'Engineering', 63000.00),
(5, 'Chris', 'Wilson', 'Marketing', 58000.00),
(6, 'Jessica', 'Lee', 'HR', 54000.00),
(7, 'David', 'Anderson', 'Finance', 67000.00),
(8, 'Emma', 'Martinez', 'Sales', 59000.00),
(9, 'James', 'Taylor', 'HR', 56000.00),
(10, 'Olivia', 'Hernandez', 'Finance', 65000.00);
Output:
| EmployeeID | FirstName | LastName | Department | Salary |
|------------|-----------|-----------|-----------------|------------|
| 1 | John | Doe | Engineering | 65000.00 |
| 2 | Jane | Smith | Marketing | 55000.00 |
| 3 | Michael | Johnson | Sales | 62000.00 |
| 4 | Emily | Brown | Engineering | 63000.00 |
| 5 | Chris | Wilson | Marketing | 58000.00 |
| 6 | Jessica | Lee | HR | 54000.00 |
| 7 | David | Anderson | Finance | 67000.00 |
| 8 | Emma | Martinez | Sales | 59000.00 |
| 9 | James | Taylor | HR | 56000.00 |
| 10 | Olivia | Hernandez | Finance | 65000.00 |
How to Find Employees with Salaries Higher Than Their Departmental Average in MySQL
In businesses and institutions, comparisons of employees’ salaries to their colleagues within the same department is a major value concern. Suppose we are an employee in a large organization where designed teams work differently: Engineering, Marketing, or Sales.
For example, suppose there are cases in which one person receives a larger salary than another who worked in the same department and that’s precisely what we are going to discuss further on.
In this article, We’ll find how we discover the salary of each worker in every department which is higher than the average salary provided in that department.