-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathSecond_High_Salary_In_Each_Dept.sql
More file actions
31 lines (27 loc) · 1.27 KB
/
Second_High_Salary_In_Each_Dept.sql
File metadata and controls
31 lines (27 loc) · 1.27 KB
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
/*
CoderPad provides a basic SQL sandbox with the following schema.
You can also use commands like `show tables` and `desc employees`
employees projects
+---------------+---------+ +---------------+---------+
| id | int |<----+ +->| id | int |
| first_name | varchar | | | | title | varchar |
| last_name | varchar | | | | start_date | date |
| salary | int | | | | end_date | date |
| department_id | int |--+ | | | budget | int |
+---------------+---------+ | | | +---------------+---------+
| | |
departments | | | employees_projects
+---------------+---------+ | | | +---------------+---------+
| id | int |<-+ | +--| project_id | int |
| name | varchar | +-----| employee_id | int |
+---------------+---------+ +---------------+---------+
*/
SELECT tb1.*
FROM employees tb1
JOIN departments tb2
ON tb1.department_id=tb2.id
WHERE (select count(distinct(tb3.salary))
from employees tb3
where tb3.salary > tb1.salary
and tb1.department_id = tb3.department_id
) =1;