-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path8_recursive_sql_using_cte.sql
89 lines (65 loc) · 1.48 KB
/
8_recursive_sql_using_cte.sql
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
----displaying 1 to 10 using recursive sql
WITH numbers AS
(SELECT 1 AS n--, 1 as level_test
UNION ALL
SELECT n + 1 as n--, level_test+1 as level_test
FROM numbers
WHERE n < 10
)
select * from numbers;
select 1
where 1 = 2
----find ancestors of a person
--select * from parentof
/*
create table parentof
(
parent varchar(255) not null,
child varchar(255) not null
)
*/
/*
insert into parentof(parent,child)
values('alice','carol')
,('bob','carol')
,('carol','dave')
,('carol','george')
,('dave','mary')
,('eve','mary')
,('mary','frank')
*/
with ancestor as
(
select parent from parentof
where child = 'george'
union all
select po.parent from ancestor
inner join --or join
parentof po
on
ancestor.parent = po.child
)
select * from ancestor;
--find the hierarchy of employees under a given manager
/*
CREATE TABLE emp_details(
id int not null identity(1,1),--auto_increment
name varchar(255),
manager_id int,
designation varchar(255)
)
insert into emp_details(name,manager_id,designation)
values('shripadh',NULL,'ceo')
,('satya',5,'software engineer')
,('jia',5,'data analyst')
,('david',5,'data scientist')
,('michael',7,'manager')
,('aravind',7,'architect')
,('asha',1,'cto')
,('mariyam',1,'manager')
,('reshma',8,'business anayst')
,('akshay',8,'java developer')
*/
select * from emp_details
select concat('hello ','->','world') as test_concat
select 'hello '+'->'+' world' as test_concat