-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjoins_practice1.sql
144 lines (118 loc) · 4.16 KB
/
joins_practice1.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
drop table if exists employee;
create table if not exists employee
(
emp_id varchar(20),
emp_name varchar(50),
salary int,
dept_id varchar(20),
manager_id varchar(20)
);
insert into employee values
('E1', 'Rahul', 15000, 'D1', 'M1'),
('E2', 'Manoj', 15000, 'D1', 'M1'),
('E3', 'James', 55000, 'D2', 'M2'),
('E4', 'Michael', 25000, 'D2', 'M2'),
('E5', 'Ali', 20000, 'D10', 'M3'),
('E6', 'Robin', 35000, 'D10', 'M3');
drop table if exists department;
create table if not exists department
(
dept_id varchar(20),
dept_name varchar(50)
);
insert into department values
('D1', 'IT'),
('D2', 'HR'),
('D3', 'Finance'),
('D4', 'Admin');
drop table if exists manager;
create table if not exists manager
(
manager_id varchar(20),
manager_name varchar(50),
dept_id varchar(20)
);
insert into manager values
('M1', 'Prem', 'D3'),
('M2', 'Shripadh', 'D4'),
('M3', 'Nick', 'D1'),
('M4', 'Cory', 'D1');
drop table if exists projects;
create table if not exists projects
(
project_id varchar(20),
project_name varchar(100),
team_member_id varchar(20)
);
insert into projects values
('P1', 'Data Migration', 'E1'),
('P1', 'Data Migration', 'E2'),
('P1', 'Data Migration', 'M3'),
('P2', 'ETL Tool', 'E1'),
('P2', 'ETL Tool', 'M4');
select * from employee;
select * from department;
select * from manager;
select * from projects;
-- Inner Join
select e.emp_name, d.dept_name
from employee e
join department d on e.dept_id = d.dept_id;
-- Left Join
select e.emp_name, d.dept_name
from employee e
left join department d on e.dept_id = d.dept_id;
-- Right Join
select e.emp_name, d.dept_name
from employee e
right join department d on e.dept_id = d.dept_id;
-- Multiple joins 1
select e.emp_name, d.dept_name, m.manager_name
from employee e
right join department d on e.dept_id = d.dept_id -- returns on Manoj, Rahul, Michael and James
join manager m on m.manager_id = e.manager_id; -- Hence returns on the managers of above 4 employees
-- Multiple joins 2
select e.emp_name, d.dept_name, m.manager_name
from employee e
right join department d on e.dept_id = d.dept_id -- returns on Manoj, Rahul, Michael and James
left join manager m on m.manager_id = e.manager_id;
-- Multiple joins 3
select e.emp_name, d.dept_name, m.manager_name
from employee e
right join department d on e.dept_id = d.dept_id -- returns on Manoj, Rahul, Michael and James
right join manager m on m.manager_id = e.manager_id;
-- Multiple joins 4
select e.emp_name, d.dept_name, m.manager_name, p.project_name
from employee e
right join department d on e.dept_id = d.dept_id -- returns on Manoj, Rahul, Michael and James
right join manager m on m.manager_id = e.manager_id
inner join projects p on p.team_member_id = m.manager_id;
--------------------------------------------------------------------
-- Inner join / JOIN
select e.emp_name, d.dept_name
from employee e join department d on e.dept_id = d.dept_id;
-- left join
select e.emp_name, d.dept_name
from employee e left join department d on e.dept_id = d.dept_id;
-- left join = inner join + all records from left table (returns null value for any columns fetched from right table)
-- right join
select e.emp_name, d.dept_name
from employee e right join department d on e.dept_id = d.dept_id;
-- right join = inner join + all records from right table (returns null value for any columns fetched from left table)
-- fetch emp name, their manager who are not working on any project.
select e.emp_name, m.manager_name
from employee e
join manager m on m.manager_id = e.manager_id
where not exists (select 1 from projects p where p.team_member_id = e.emp_id);
-- Fetch details of ALL emp, their manager, their department and the projects they work on.
select e.emp_name, d.dept_name, m.manager_name, p.project_name
from employee e
left join department d on d.dept_id = e.dept_id
join manager m on m.manager_id = e.manager_id
left JOIN projects p on p.team_member_id = e.emp_id;
-- Fetch details of ALL emp and ALL manager, their department and the projects they work on.
select e.emp_name, d.dept_name, m.manager_name, p.project_name
from employee e
left join department d on d.dept_id = e.dept_id
right join manager m on m.manager_id = e.manager_id
left join projects p on p.team_member_id = e.emp_id;