forked from MrLRTripp/HackerRank-LeetCode-StrataScratch-SQL
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode 608 Tree Node.sql
More file actions
39 lines (30 loc) · 1.09 KB
/
LeetCode 608 Tree Node.sql
File metadata and controls
39 lines (30 loc) · 1.09 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
32
33
34
35
36
37
38
set search_path to data_sci
-- What makes this a little tricky is that any comparison (=, <>) to NULL is always False
-- ALL will return NULL if there are no matches and there is a NULL in the result set.
-- In this case, there is a NULL in parent_department_id.
-- Therefore, you need a WHERE clause to exclude parent_department_id is NULL.
-- The situation is taken care of in the first case statement.
-- If not a Root or Leaf, the ELSE will label as Inner
select id,
-- root_leaf_inner
case when parent_department_id is NULL then 'Root'
when id <> ALL (select parent_department_id
from org_structure
where parent_department_id is not NULL) then 'Leaf'
else 'Inner'
END
from org_structure
order by id ASC
-- This is better since you don't have to deal with the funny way ALL handles NULL
SELECT
id,
CASE
WHEN parent_department_id IS NULL THEN 'Root'
WHEN id IN (SELECT parent_department_id FROM org_structure) THEN 'Inner'
ELSE 'Leaf'
END as Type
FROM
org_structure
order by id ASC
select * from org_structure
where parent_department_id <> NULL