SQLNotes: Salaries Differences

XuanKhanh Nguyen
Nothingaholic
Published in
2 min readJan 23, 2022

--

Problem

Write a query that calculates the difference between the highest salaries found in the marketing and engineering departments. Output just the absolute difference in salaries.

Tables: db_employee, db_deptdb_employee
+--------------+---------+
| id | int |
+--------------+---------+
| first_name | varchar |
+--------------+---------+
| last_name | varchar |
+--------------+---------+
| salary | int |
+--------------+---------+
| department_id| int |
+--------------+---------+
db_dept
+--------------+---------+
| id | int |
+--------------+---------+
| department | varchar |
+--------------+---------+

Solution

Algorithm

Solution #1: From the db_employee table, we can get the MAX(salary) , and from the db_depttable, we know that the id for the marketing department is 4 and the id for the engineering department is 1. We can find the absolute value for the difference between the highest salaries using abs().

--

--

Nothingaholic
Nothingaholic

Published in Nothingaholic

We love what we do. The moment when we realize we’ve learned something new makes every meeting or change worth it. Learn on!

XuanKhanh Nguyen
XuanKhanh Nguyen

Written by XuanKhanh Nguyen

Interests: Data Science, Machine Learning, AI, Stats, Python | Minimalist | A fan of odd things.