diff --git a/README.md b/README.md index ace1e99..bb732bc 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,7 @@ [![MIT licensed](https://img.shields.io/badge/License-MIT-yellow.svg)](./LICENSE) [![CI](https://github.com/ivanbyone/leetcode-sql/actions/workflows/ci.yml/badge.svg)](https://github.com/Ivanbyone/leetcode-sql//actions) +![Postgres](https://img.shields.io/badge/postgres-%23316192.svg?style=for-the-badge&logo=postgresql&logoColor=white) ## Description @@ -44,6 +45,7 @@ Have a good contributing! - [596. Classes With at Least 5 Students](./leetcode/easy/596.%20Classes%20With%20at%20Least%205%20Students.sql) 2. [Medium](./leetcode/medium/) - [176. Second Highest Salary](./leetcode/medium/176.%20Second%20Highest%20Salary.sql) + - [184. Department Highest Salary](./leetcode/medium/184.%20Department%20Highest%20Salary.sql) ## License diff --git a/leetcode/medium/184. Department Highest Salary.sql b/leetcode/medium/184. Department Highest Salary.sql new file mode 100644 index 0000000..77c6211 --- /dev/null +++ b/leetcode/medium/184. Department Highest Salary.sql @@ -0,0 +1,49 @@ +/* +Question 184. Department Highest Salary +Link: https://leetcode.com/problems/department-highest-salary/description/ + +Table: Employee + ++--------------+---------+ +| Column Name | Type | ++--------------+---------+ +| id | int | +| name | varchar | +| salary | int | +| departmentId | int | ++--------------+---------+ +id is the primary key (column with unique values) for this table. +departmentId is a foreign key (reference columns) of the ID from the Department table. +Each row of this table indicates the ID, name, and salary of an employee. It also contains the ID of their department. + + +Table: Department + ++-------------+---------+ +| Column Name | Type | ++-------------+---------+ +| id | int | +| name | varchar | ++-------------+---------+ +id is the primary key (column with unique values) for this table. It is guaranteed that department name is not NULL. +Each row of this table indicates the ID of a department and its name. + + +Write a solution to find employees who have the highest salary in each of the departments. + +Return the result table in any order. +*/ + +SELECT + d.name AS Department, + e.name AS Employee, + e.salary +FROM Employee AS e +LEFT JOIN + Department AS d + ON e.departmentId = d.id +WHERE e.salary = ( + SELECT MAX(em.salary) + FROM Employee AS em + WHERE em.departmentId = e.departmentId +)