top of page

Leetcode 184. Department Highest Salary

  • crystal0108wong
  • Jun 1, 2017
  • 1 min read

Description

The Employee table holds all employees. Every employee has an Id, and there is also a column for the department Id. The Department table holds all departments of the company. Write a SQL query to find employees who earn the top three salaries in each of the department. For the above tables, your SQL query should return the following rows. [Read Full Description Here]

Solutions

Thought Process

1. First of all, find out the highest salary in each department using 'GROUP BY' command.

2. Join the department information, highest salary within each department with employee to find out which employee in the department has the same salary as the highest salary in that dept.

Key Points

1, It's important to understand that 'GROUP BY' is a aggregation function and will return a scalar result for each group,

2. By simply executing 'GROUP BY' on employee table, it will not return the highest salary and the "corresponding" employee, but the first employee in each group.

Comments


Featured Posts
Tag Cloud
Category

Crystal Wang @ 2017

bottom of page