-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjoins_practice2.sql
More file actions
177 lines (145 loc) · 5.97 KB
/
joins_practice2.sql
File metadata and controls
177 lines (145 loc) · 5.97 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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
drop table employee;
create table 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');
insert into employee values
('M1', 'Prem', 115000, 'D3', 'M4'),
('M2', 'Shripadh', 125000, 'D4', 'M4'),
('M3', 'Nick', 150000, 'D1', 'M4'),
('M4', 'Cory', 220000, 'D1', '');
drop table department;
create table department
(
dept_id varchar(20),
dept_name varchar(50)
);
insert into department values
('D1', 'IT'),
('D2', 'HR'),
('D3', 'Finance'),
('D4', 'Admin');
drop table company;
create table company
(
company_id varchar(10),
company_name varchar(50),
location varchar(20)
);
insert into company values
('C001', 'techTFQ Solutions', 'Kuala Lumpur');
insert into company values
('C002', 'techTFQ Media Group', 'Singapore');
DROP TABLE family;
CREATE TABLE family
(
member_id VARCHAR(10),
name VARCHAR(50),
age INT,
parent_id VARCHAR(10)
);
insert into family values
('F1', 'David', 4, 'F5'),
('F2', 'Carol', 10, 'F5'),
('F3', 'Michael', 12, 'F5'),
('F4', 'Johnson', 36, ''),
('F5', 'Maryam', 40, 'F6'),
('F6', 'Stewart', 70, ''),
('F7', 'Rohan', 6, 'F4'),
('F8', 'Asha', 8, 'F4');
select * from employee; -- D1, D2, D10
select * from department; -- D1, D2, D3, D4
select * from company;
-- INNER JOIN can also be represented as "JOIN"
-- INNER Join = Fetches only matching records in both tables based on the JOIN condition.
-- Write a query to fetch the employee name and their corresponding department name.
SELECT e.emp_name, d.dept_name
FROM employee e
INNER JOIN department d ON e.dept_id = d.dept_id;
-- LEFT JOIN can also be represented as "LEFT OUTER JOIN"
-- LEFT Join = INNER Join + all remaining records from Left Table (returns null value for any columns fetched from right table)
-- Write a query to fetch ALL the employee name and their corresponding department name.
SELECT e.emp_name, d.dept_name
FROM employee e
LEFT JOIN department d ON e.dept_id = d.dept_id;
-- RIGHT JOIN can also be represented as "RIGHT OUTER JOIN"
-- RIGHT Join = INNER Join + all remaining records from Right Table (returns null value for any columns fetched from left table)
-- Write a query to fetch ALL the department and the employees under these departments.
SELECT e.emp_name, d.dept_name
FROM employee e
RIGHT JOIN department d ON e.dept_id = d.dept_id;
-- FULL JOIN can also be represented as "FULL OUTER JOIN"
-- FULL Join = INNER Join
-- + all remaining records from Left Table (returns null value for any columns fetched from right table)
-- + all remaining records from Right Table (returns null value for any columns fetched from left table)
-- Write a query to fetch the employee name and their corresponding department name.
-- Make sure to include all the employees and the departments.
SELECT e.emp_name, d.dept_name
FROM employee e
FULL JOIN department d ON e.dept_id = d.dept_id;
-- CROSS JOIN
-- CROSS JOIN returns cartesian product.
-- Meaning it will match every record from the left table with every record from the right table hence it will return records from both table.
-- No join condition is required to be specified.
SELECT e.emp_name, d.dept_name
FROM employee e
CROSS JOIN department d;
-- Write a query to fetch the employee name and their corresponding department name.
-- Also make sure to display the company name and the company location correspodning to each employee.
-- NATURAL JOIN - SQL will naturally choose the column on which join should happen based on the column name.
-- Natural join will perform inner join operation if there are columns with same name in both table. If there are more than 1 column with same name then join will happen based on all these columns.
-- If there are no columns with same name in both table then it performs cross join
-- If you specify * in select list then it displays the join columns in the beginning and does not repeat it.
-- No join condition is required to be specified.
SELECT *
FROM employee e
NATURAL JOIN department d;
-- Altering the dept_id column name to see how Natural Join acts when there are no common column names in both tables.
--alter table department rename column dept_id to department_id
--alter table department rename column department_id to dept_id;
-- SELF JOIN - When you join a table to itself, this is called as SELF Join.
-- There is no keyword like SELF JOIN but we just use the regular JOIN keyword to make the self join.
-- Write a query to fetch the child name and their age correspodning to their parent name and parent age.
select child.name as child_name
, child.age as child_age
, parent.name as parent_name
, parent.age as parent_age
from family as child
join family as parent on parent.member_id = child.parent_id;
select child.name as child_name
, child.age as child_age
, parent.name as parent_name
, parent.age as parent_age
from family as child
left join family as parent on parent.member_id = child.parent_id;
-- ANSI JOIN - Uses the JOIN clause and mentions join condition under the ON clause and filter conditions in the WHERE clause.
SELECT e.emp_name, d.dept_name
FROM employee e
JOIN department d ON e.dept_id = d.dept_id
WHERE e.salary > 15000;
SELECT e.emp_name, d.dept_name
FROM employee e
LEFT JOIN department d ON e.dept_id = d.dept_id
WHERE e.salary > 15000;
-- Non ANSI JOIN - Uses comma to seperate multiple tables and then use WHERE clause to mention both the join and filter conditions.
SELECT e.emp_name, d.dept_name
FROM employee e
, department d
WHERE e.salary > 15000
AND e.dept_id = d.dept_id;
SELECT e.emp_name, d.dept_name
FROM employee e
, department d
WHERE e.salary > 15000
AND e.dept_id = d.dept_id (+); -- This is unsupported in PostgreSQL but works in Oracle. Other RDBMS may have alternative symbols to performs same thing.