-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjoinsExercise.sql
More file actions
executable file
·200 lines (154 loc) · 4.47 KB
/
joinsExercise.sql
File metadata and controls
executable file
·200 lines (154 loc) · 4.47 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
CREATE DATABASE joinsExercise;
USE joinsExercise;
## table department
CREATE TABLE departments (
dept_id INT PRIMARY KEY,
dept_name VARCHAR(50),
location VARCHAR(50)
);
INSERT INTO departments VALUES
(1, 'IT', 'Mumbai'),
(2, 'HR', 'Delhi'),
(3, 'Finance', 'Pune'),
(4, 'Marketing', 'Bangalore'),
(5, 'Support', 'Chennai');
## table employees
CREATE TABLE employees (
emp_id INT PRIMARY KEY,
name VARCHAR(50),
salary INT,
dept_id INT,
FOREIGN KEY (dept_id) REFERENCES departments(dept_id)
);
INSERT INTO employees VALUES
(101, 'Rohan', 70000, 1),
(102, 'Sneha', 55000, 2),
(103, 'Amit', 60000, 1),
(104, 'Priya', 45000, 3),
(105, 'Karan', 50000, NULL),
(106, 'Neha', 52000, 4),
(107, 'Arjun', 48000, 2);
## table projects
CREATE TABLE projects (
project_id INT PRIMARY KEY,
project_name VARCHAR(50),
emp_id INT,
FOREIGN KEY (emp_id) REFERENCES employees(emp_id)
);
INSERT INTO projects VALUES
(201, 'AI System', 101),
(202, 'HR Portal', 102),
(203, 'Banking App', 104),
(204, 'Website Revamp', 101),
(205, 'Marketing Ads', 106);
## Show employee name and their department name.
SELECT e.name AS emp_name, d.dept_name AS department_name
FROM employees AS e
INNER JOIN departments AS d
ON e.dept_id = d.dept_id;
## Show employees working in IT department along with their salaries.
SELECT e.name AS emp_name, d.dept_name AS department_name, e.salary AS emp_salary
FROM employees e
INNER JOIN departments d
ON e.dept_id = d.dept_id
WHERE dept_name = "IT";
## Show all employees and their department names.
## Employees without a department should still appear.
SELECT e.name AS emp_name, d.dept_name AS department_name
FROM employees AS e
LEFT JOIN departments AS d
ON e.dept_id = d.dept_id;
#############################################################
## departments data
CREATE TABLE Departments_data (
dept_id INT PRIMARY KEY,
dept_name VARCHAR(50),
location VARCHAR(50)
);
INSERT INTO Departments_data VALUES
(1, 'Engineering', 'Pune'),
(2, 'HR', 'Mumbai'),
(3, 'Finance', 'Delhi'),
(4, 'Marketing', 'Bangalore');
## employee_data
CREATE TABLE Employees_data (
emp_id INT PRIMARY KEY,
emp_name VARCHAR(50),
salary INT,
dept_id INT
);
INSERT INTO Employees_data VALUES
(101, 'Amit', 60000, 1),
(102, 'Neha', 50000, 2),
(103, 'Raj', 70000, 1),
(104, 'Sneha', 45000, NULL),
(105, 'Karan', 55000, 4),
(106, 'Priya', 48000, 5);
## projects_data
CREATE TABLE Projects_data (
proj_id INT PRIMARY KEY,
proj_name VARCHAR(50),
dept_id INT
);
INSERT INTO Projects_data VALUES
(201, 'AI System', 1),
(202, 'Recruitment Drive', 2),
(203, 'Budget Planning', 3),
(204, 'Ad Campaign', 4),
(205, 'Cloud Migration', 1);
## employee_project
CREATE TABLE Employee_Projects (
emp_id INT,
proj_id INT
);
INSERT INTO Employee_Projects VALUES
(101, 201),
(101, 205),
(102, 202),
(103, 201),
(105, 204),
(107, 203);
## Show employee names along with their department names.
SELECT d.dept_id, e.emp_name, d.dept_name
FROM Employees_data AS e
JOIN Departments_data AS d
ON e.dept_id = d.dept_id;
## Show all employees and their department names. Employees without a department should still appear
SELECT *
FROM Employees_data AS e
LEFT JOIN Departments_data AS d
ON e.dept_id = d.dept_id;
## Display department names and the projects handled by each department.
SELECT d.dept_name, p.proj_name
FROM Departments_data AS d
LEFT JOIN Projects_data AS p
ON d.dept_id = p.dept_id;
## Show all projects and the department location managing them.
SELECT p.proj_name, d.location
FROM Projects_data AS p
JOIN Departments_data AS d
ON p.dept_id = d.dept_id;
## List employee names and the projects they are working on.
SELECT e.emp_name, p.proj_name
FROM Employees_data AS e
JOIN Projects_data AS p
ON e.dept_id = p.dept_id;
SELECT e.emp_name, p.proj_name
FROM Employees_data e
JOIN Employee_Projects ep ON e.emp_id = ep.emp_id
JOIN Projects_data p ON ep.proj_id = p.proj_id;
## Show all employees along with project names. Include employees who are not working on any project
SELECT e.emp_name, p.proj_name
FROM Employees_data AS e
LEFT JOIN Projects_data AS p
ON e.dept_id=p.dept_id;
## Display all projects along with employee names assigned to them. Include projects that have no employees.
SELECT p.proj_name, e.emp_name
FROM Employees_data AS e
RIGHT JOIN Projects_data AS p
ON e.dept_id=p.dept_id;
## Show employee name, salary, and department location.
SELECT e.emp_name,e.salary,d.location
FROM Employees_data AS e
JOIN Departments_data AS d
ON e.dept_id=d.dept_id;