-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataExpert-SQL16.sql
More file actions
44 lines (37 loc) · 1009 Bytes
/
DataExpert-SQL16.sql
File metadata and controls
44 lines (37 loc) · 1009 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
-- Question: Salary Range Difference
-- https://www.dataexpert.io/question/salary-range-difference
-- Approach 1: Using CTEs and Window Functions
WITH sal_agg AS (
SELECT
id, name, salary,
MAX(salary) OVER() AS max,
MIN(salary) OVER() AS min
FROM playground.employees_salary
GROUP BY id, name, salary
), max_sal_sum AS (
SELECT SUM(salary) AS max_sum
FROM sal_agg
WHERE salary = max
), min_sal_sum AS (
SELECT SUM(salary) AS min_sum
FROM sal_agg
WHERE salary = min
)
SELECT COALESCE(max_sum - min_sum, 0) AS difference
FROM max_sal_sum
JOIN min_sal_sum ON 1 = 1;
-- Approach 2: Using CTE with Conditional Aggregation
WITH sal_range AS (
SELECT
salary,
MAX(salary) OVER() AS max_sal,
MIN(salary) OVER() AS min_sal
FROM playground.employees_salary
)
SELECT COALESCE(
SUM(CASE WHEN salary = max_sal THEN salary END), 0
)
- COALESCE(
SUM(CASE WHEN salary = min_sal THEN salary END), 0
) AS difference
FROM sal_range;