-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSession_9_Advance_Joins_SQL.sql
More file actions
149 lines (117 loc) · 3.54 KB
/
Copy pathSession_9_Advance_Joins_SQL.sql
File metadata and controls
149 lines (117 loc) · 3.54 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
CREATE DATABASE Session_9;
USE Session_9;
------- 1. Perform Full Outer Join ------
CREATE TABLE influencers (
influencer_id INT AUTO_INCREMENT,
influencer_name VARCHAR(50),
city VARCHAR(50),
PRIMARY KEY (influencer_id)
);
INSERT INTO influencers (influencer_name,city)
VALUES ('Aman Verma', 'Mumbai'),
('Riya Sharma', 'Delhi'),
('Neha Patel', 'Ahmedabad');
CREATE TABLE brands (
brand_id INT AUTO_INCREMENT,
brand_name VARCHAR(50),
city VARCHAR(50),
influencer_id INT,
PRIMARY KEY (brand_id),
FOREIGN KEY (influencer_id)
REFERENCES influencers(influencer_id)
);
INSERT INTO brands (brand_name, city, influencer_id)
VALUES ('TechWorld', 'Mumbai',1),
('FashionHub', 'Bangalore',1),
('FoodieKing', 'Ahmedabad',3),
('NewAge', 'Delhi',2);
-- FULL OUTER JOIN is not directly supported in MySQL.
-- It can be simulated by combining LEFT JOIN and RIGHT JOIN using UNION.
-- This returns:
-- 1. All matching records from both tables.
-- 2. Unmatched records from the left table.
-- 3. Unmatched records from the right table.
SELECT i.influencer_name, b.brand_name
FROM influencers AS i
LEFT JOIN brands AS b
ON i.city = b.city
UNION
SELECT i.influencer_name, b.brand_name
FROM influencers AS i
RIGHT JOIN brands AS b
ON i.city = b.city;
-------- 2. Self Join --------
CREATE TABLE playlists (
id INT AUTO_INCREMENT,
playlist_name VARCHAR(50),
parent_playlist_id INT,
PRIMARY KEY (id),
FOREIGN KEY (parent_playlist_id)
REFERENCES playlists(id)
);
INSERT INTO playlists (playlist_name, parent_playlist_id)
VALUES ('Workout Mix', NULL),
('Cardio Songs', 1),
('Gym Motivation', 1),
('Relaxing Music', NULL),
('Sleep Playlist', 4);
-- SELF JOIN is used when a table is related to itself.
-- Here, each playlist can have a parent playlist.
-- Similar to Spotify where a playlist category can contain sub-playlists.
SELECT child.playlist_name AS Playlist_Name, parent.playlist_name AS Parent_Playlist
FROM playlists AS child
JOIN playlists AS parent
ON child.parent_playlist_id = parent.id;
-------- 3. Cross Join --------
CREATE TABLE users (
user_id INT AUTO_INCREMENT,
user_name VARCHAR(50),
PRIMARY KEY (user_id)
);
INSERT INTO users (user_name)
VALUES ('Rahul'),
('Priya'),
('Karan');
CREATE TABLE offers (
offer_id INT AUTO_INCREMENT,
offer_title VARCHAR(100),
user_id INT,
PRIMARY KEY (offer_id)
);
INSERT INTO offers (offer_title,user_id)
VALUES ('10% Off Electronics',1),
('Buy 1 Get 1 Free',2),
('Free Delivery',3);
-- CROSS JOIN creates a Cartesian Product.
-- Every user is paired with every offer.
-- Similar to a Flipkart campaign where all offers are generated for all users before applying personalization filters.
SELECT U.user_name, O.offer_title
FROM users AS U
CROSS JOIN offers AS O;
-------- 4. Self Join (Employees & Managers) --------
CREATE TABLE managers (
manager_id INT AUTO_INCREMENT,
name VARCHAR(50),
PRIMARY KEY (manager_id)
);
INSERT INTO managers (name)
VALUES ('Alok Singh'),
('Jignesh Patel');
CREATE TABLE employees (
id INT AUTO_INCREMENT,
name VARCHAR(50),
manager_id INT,
PRIMARY KEY (id),
FOREIGN KEY (manager_id)
REFERENCES managers(manager_id)
);
INSERT INTO employees (name, manager_id)
VALUES ('Rajesh', NULL),
('Amit', 1),
('Sneha', 1),
('Pooja', 2),
('Vikas', 2);
SELECT e.name AS employee_name,m.name AS manager_name
FROM employees AS e
LEFT JOIN managers AS m
ON e.manager_id = m.manager_id;