-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_5.sql
More file actions
131 lines (99 loc) · 3.25 KB
/
data_5.sql
File metadata and controls
131 lines (99 loc) · 3.25 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
-- Create a new database
CREATE DATABASE company_database;
-- Switch to the newly created database
USE company_database;
-- Create the employee table
CREATE TABLE employee (
employee_id INT PRIMARY KEY,
employee_name VARCHAR(50),
salary DECIMAL(10, 2),
department_id INT,
manager_id INT,
FOREIGN KEY (department_id) REFERENCES department(department_id),
FOREIGN KEY (manager_id) REFERENCES employee(employee_id)
);
INSERT INTO employee (employee_id, employee_name, salary, department_id, manager_id)
VALUES
(1, 'John Doe', 50000.00, 1, 1),
(2, 'Jane Smith', 60000.00, 1, 1),
(3, 'Michael Johnson', 55000.00, 2, 3),
(4, 'Emily Davis', 52000.00, 2, 3),
(5, 'David Wilson', 48000.00, 3, 5),
(6, 'Sarah Brown', 51000.00, 3, 5);
describe employee;
SHOW COLUMNS FROM employee;
select * from employee;
CREATE TABLE department (
department_id INT PRIMARY KEY,
department_name VARCHAR(50) NOT NULL
);
select * from department;
INSERT INTO department (department_id, department_name)
VALUES
(1, 'Engineering'),
(2, 'Marketing'),
(3, 'Finance'),
(4, 'Human Resources');
-- Create the manager table
CREATE TABLE manager (
manager_id INT PRIMARY KEY,
manager_name VARCHAR(50),
department_id INT,
FOREIGN KEY (department_id) REFERENCES department(department_id)
);
-- Insert rows into the manager table
INSERT INTO manager (manager_id, manager_name, department_id)
VALUES
(1, 'John Manager', 1),
(2, 'Jane Manager', 2),
(3, 'Michael Manager', 3),
(4, 'Emily Manager', 4);
select * from manager;
-- Create the projects table
CREATE TABLE projects (
project_id INT PRIMARY KEY,
project_name VARCHAR(50),
team_member_id INT,
FOREIGN KEY (team_member_id) REFERENCES employee(employee_id) ON DELETE CASCADE,
FOREIGN KEY (team_member_id) REFERENCES manager(manager_id) ON DELETE CASCADE
);
-- Insert rows into the projects table
INSERT INTO projects (project_id, project_name, team_member_id)
VALUES
(4, 'Project D', 3), -- Michael Manager
(5, 'Project E', 6), -- Sarah Employee
(6, 'Project F', 1), -- John Manager
(7, 'Project G', 4), -- Emily Employee
(8, 'Project H', 2); -- Jane Manager
select * from employee;
select * from department;
select * from manager;
select * from projects;
-- inner Join
SELECT e.employee_name, d.department_name
FROM employee e
JOIN
department d
ON
e.department_id = d.department_id; -- join condition in which join should happen
-- left join = inner join + any additional records in the left table
SELECT e.employee_name, d.department_name
FROM employee e -- left table
LEFT JOIN
department d -- right table
ON
e.department_id = d.department_id; -- join condition in which join should happen
-- right join = inner join + any additional records in the right table
SELECT e.employee_name, d.department_name
FROM employee e -- left table
RIGHT JOIN
department d -- right table
ON
e.department_id = d.department_id; -- join condition in which join should happen
select e.employee_name, d.department_name, m.manager_name, p.project_name
from employee e LEFT JOIN department d
ON e.department_id = d.department_id
JOIN manager m
ON m.manager_id = e.manager_id
LEFT JOIN projects p
ON p.team_member_id = e.employee_id