PostgreSQL Aggregate Functions and Group By: Get the difference between the highest and lowest salaries
8. Write a query to get the difference between the highest and lowest salaries.
Sample Solution:
Code:
-- This SQL query calculates the salary difference between the highest and lowest salaries among employees.
SELECT MAX(salary) - MIN(salary) DIFFERENCE -- Calculates the difference between the maximum and minimum salaries from the employees table and labels the result column as "DIFFERENCE"
FROM employees; -- Specifies the table from which to retrieve data, in this case, the employees table
Explanation:
- The SQL query computes the salary difference between the highest and lowest salaries among employees.
- The SELECT statement is used to specify the calculation of the salary difference.
- MAX(salary) retrieves the highest salary from the salary column.
- MIN(salary) retrieves the lowest salary from the salary column.
- MAX(salary) - MIN(salary) calculates the difference between the highest and lowest salaries.
- The result column is labeled as "DIFFERENCE" using the alias DIFFERENCE.
- The FROM clause specifies the table from which to retrieve the data, which is the employees table in this case.
Sample table: employees
+-------------+-------------+-------------+----------+--------------------+------------+------------+----------+----------------+------------+---------------+ | EMPLOYEE_ID | FIRST_NAME | LAST_NAME | EMAIL | PHONE_NUMBER | HIRE_DATE | JOB_ID | SALARY | COMMISSION_PCT | MANAGER_ID | DEPARTMENT_ID | +-------------+-------------+-------------+----------+--------------------+------------+------------+----------+----------------+------------+---------------+ | 100 | Steven | King | SKING | 515.123.4567 | 1987-06-17 | AD_PRES | 24000.00 | 0.00 | 0 | 90 | | 101 | Neena | Kochhar | NKOCHHAR | 515.123.4568 | 1987-06-18 | AD_VP | 17000.00 | 0.00 | 100 | 90 | | 102 | Lex | De Haan | LDEHAAN | 515.123.4569 | 1987-06-19 | AD_VP | 17000.00 | 0.00 | 100 | 90 | | 103 | Alexander | Hunold | AHUNOLD | 590.423.4567 | 1987-06-20 | IT_PROG | 9000.00 | 0.00 | 102 | 60 | | 104 | Bruce | Ernst | BERNST | 590.423.4568 | 1987-06-21 | IT_PROG | 6000.00 | 0.00 | 103 | 60 | | 105 | David | Austin | DAUSTIN | 590.423.4569 | 1987-06-22 | IT_PROG | 4800.00 | 0.00 | 103 | 60 | | 106 | Valli | Pataballa | VPATABAL | 590.423.4560 | 1987-06-23 | IT_PROG | 4800.00 | 0.00 | 103 | 60 | | 107 | Diana | Lorentz | DLORENTZ | 590.423.5567 | 1987-06-24 | IT_PROG | 4200.00 | 0.00 | 103 | 60 | | 108 | Nancy | Greenberg | NGREENBE | 515.124.4569 | 1987-06-25 | FI_MGR | 12000.00 | 0.00 | 101 | 100 | .......... | 206 | William | Gietz | WGIETZ | 515.123.8181 | 1987-10-01 | AC_ACCOUNT | 8300.00 | 0.00 | 205 | 110 | +-------------+-------------+-------------+----------+--------------------+------------+------------+----------+----------------+------------+---------------+
Output:
pg_exercises=# SELECT MAX(salary) - MIN(salary) DIFFERENCE pg_exercises-# FROM employees; difference ------------ 21900.00 (1 row)
Go to:
PREV :Write a query to get the number of employees working in each post.
NEXT : Write a query to find the manager ID and the salary of the lowest-paid employee under that manager.
Practice Online
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
What is the difficulty level of this exercise?
