Salary Difference

Description


There are many employees at your company, with a wide range of salaries. You’ve decided to find out the difference in salary between the employees who make the most and the employees who make the least.

You store information about your employees in the table employees, which has the structure:

  • id: the unique employee ID;
  • name: the employee’s name;
  • salary: the employee’s salary - a positive integer.

The difference between the sum of the highest salaries and the sum of lowest salaries will give you the information you want. So, given the table employees, build the resulting table as follows: The table should contain only one column difference and only one row, which will contain the difference between sum of the highest and the sum of lowest salaries. If employees table is empty, the difference should be equal 0.

Example

For the following tables employees

id name salary
1 John 1200
2 Bill 1000
3 Mike 1300
4 Katy 1200
5 Brendon 1300
6 Amanda 900

the output should be

difference
1700

There are two highest salaries (1300) and one lowest salary (900). 1300 _ 2 - 900 _ 1 = 1700.

  • [execution time limit] 10 seconds (mysql)

Solution


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/*Please add ; after each select statement*/
CREATE PROCEDURE salaryDifference()
BEGIN


SELECT IFNULL(maxd - mind,0) AS difference FROM 
(SELECT mins*COUNT(*) AS mind FROM employees e 
    JOIN (SELECT MIN(Salary) AS mins FROM employees LIMIT 1) tmin
    ON e.salary = mins) t1,

(SELECT maxs*COUNT(*) AS maxd FROM employees e 
    JOIN (SELECT MAX(Salary) AS maxs FROM employees LIMIT 1) tmAx
    ON e.salary = maxs) t2;



END