-
Notifications
You must be signed in to change notification settings - Fork 0
/
06_joins_subqueries.sql
203 lines (162 loc) · 3.81 KB
/
06_joins_subqueries.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
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
201
202
203
SELECT * FROM employees;
SELECT * FROM addresses;
SELECT employee_id, first_name, addresses.address_id, address_text
FROM employees
INNER JOIN addresses;
SELECT first_name, last_name, address_text
FROM employees
INNER JOIN addresses
ON employees.address_id = addresses.address_id;
SELECT
e.employee_id,
e.first_name,
e.last_name,
a.address_id,
a.address_text
FROM employees AS e
INNER JOIN addresses AS a
ON e.address_id = a.address_id;
SELECT
e.employee_id,
e.first_name,
e.last_name,
a.address_id,
a.address_text
FROM employees e
INNER JOIN addresses a
ON e.address_id = a.address_id;
SELECT * FROM employees;
SELECT
e.employee_id,
e.first_name,
e.last_name,
e.manager_id,
m.employee_id,
m.first_name,
m.last_name
FROM employees e
INNER JOIN employees m
ON e.manager_id = m.employee_id;
SELECT * FROM employees;
SELECT e.employee_id, e.first_name, a.address_id, a.address_text
FROM employees e, addresses a
WHERE e.address_id = a.address_id
ORDER BY e.employee_id;
SELECT COUNT(*) FROM departments;
SELECT COUNT(DISTINCT department_id)
FROM employees;
INSERT INTO departments(name, manager_id)
VALUES("Empty Dept", 1);
SELECT * FROM departments;
SELECT
e.employee_id,
e.first_name,
e.department_id,
d.department_id,
d.name
FROM employees e
RIGHT JOIN departments d
ON e.department_id = d.department_id
ORDER BY d.department_id DESC;
SELECT employee_id, last_name
FROM employees
WHERE first_name LIKE 'z%'
UNION
SELECT name, department_id
FROM departments
WHERE name LIKE 'r%';
INSERT INTO towns(name) VALUES('Empty Dept');
SELECT COUNT(*) FROM towns;
SELECT name
FROM departments
WHERE name LIKE 'e%'
UNION
SELECT name
FROM towns
WHERE name LIKE 'e%';
SELECT e.employee_id, a.address_id
FROM employees e
CROSS JOIN addresses a;
SELECT e.employee_id, a.address_id
FROM employees e
JOIN addresses a;
SELECT e.employee_id, a.address_id
FROM employees e, addresses a;
SELECT * FROM employees;
SELECT
e.employee_id,
CONCAT_WS(' ', first_name, last_name) AS 'full_name',
d.department_id,
d.name AS 'department_name'
FROM employees e
JOIN departments d
ON e.employee_id = d.manager_id
ORDER BY employee_id
LIMIT 5;
SELECT
e.employee_id,
e.first_name,
m.employee_id,
m.first_name
FROM employees e
RIGHT JOIN employees m
ON e.manager_id = m.employee_id
ORDER BY m.employee_id;
SELECT employee_id, first_name FROM employees
WHERE first_name LIKE 'z%';
SELECT * FROM employees
WHERE manager_id IN (90, 25);
SELECT * FROM employees
WHERE manager_id IN (
SELECT employee_id FROM employees
WHERE first_name LIKE 'z%'
);
SELECT employee_id, first_name FROM employees
WHERE first_name LIKE 'za%';
SELECT * FROM employees
WHERE manager_id = (
SELECT employee_id FROM employees
WHERE first_name LIKE 'za%'
LIMIT 1
);
SELECT
ROUND(
(SELECT COUNT(*) FROM employees) /
(SELECT COUNT(*) FROM departments),
2);
SELECT AVG(salary) FROM employees;
SELECT COUNT(*) AS 'count'
FROM employees
WHERE salary > (
SELECT AVG(salary) FROM employees
);
SELECT *
FROM towns
WHERE name IN ('San Francisco', 'Sofia', 'Carnation');
SELECT
t.town_id,
t.name AS 'town_name',
a.address_text
FROM towns t
INNER JOIN addresses a
ON t.town_id = a.town_id
WHERE t.name IN ('San Francisco', 'Sofia', 'Carnation')
ORDER BY t.town_id, a.address_id;
SELECT employee_id, first_name, last_name, department_id, salary
FROM employees
WHERE manager_id IS NULL;
SELECT *
FROM employees e
JOIN towns t
ON e.first_name = t.name;
SELECT e.first_name, t.name
FROM employees e
JOIN addresses a
ON e.address_id = a.address_id
JOIN towns t
ON t.town_id = a.town_id
ORDER BY e.employee_id;
SELECT * FROM employees;
SELECT * FROM addresses WHERE address_id = 166;
SELECT * FROM towns WHERE town_id = 12;
SELECT * FROM projects;