-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjoin_test_db.sql
More file actions
59 lines (49 loc) · 1.79 KB
/
join_test_db.sql
File metadata and controls
59 lines (49 loc) · 1.79 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
CREATE DATABASE IF NOT EXISTS join_test_db;
USE join_test_db;
CREATE TABLE roles (
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
name VARCHAR(100) NOT NULL,
PRIMARY KEY (id)
);
CREATE TABLE users (
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
name VARCHAR(100) NOT NULL,
email VARCHAR(100) NOT NULL,
role_id INT UNSIGNED DEFAULT NULL,
PRIMARY KEY (id),
FOREIGN KEY (role_id) REFERENCES roles (id)
);
INSERT INTO roles (name) VALUES ('admin');
INSERT INTO roles (name) VALUES ('author');
INSERT INTO roles (name) VALUES ('reviewer');
INSERT INTO roles (name) VALUES ('commenter');
INSERT INTO users (name, email, role_id) VALUES
('bob', 'bob@example.com', 1),
('joe', 'joe@example.com', 2),
('sally', 'sally@example.com', 3),
('adam', 'adam@example.com', 3),
('jane', 'jane@example.com', null),
('mike', 'mike@example.com', null),
('claudia', 'claudia@example.com', 2),
('aimee', 'aimee@example.com', 2),
('arlene', 'arlene@example.com', 2),
('lene', 'lene@example.com', null);
# Join/Inner Join
SELECT users.name as user_name, roles.name as role_name
FROM users
INNER JOIN roles ON users.role_id = roles.id;
# LEFT JOIN
SELECT users.name AS user_name, roles.name AS role_name
FROM users
LEFT JOIN roles ON users.role_id = roles.id;
# RIGHT JOIN
SELECT users.name AS user_name, roles.name AS role_name
FROM users
RIGHT JOIN roles ON users.role_id = roles.id;
# Use COUNT and the appropriate join type to get a list of roles along with the number of users that have a given role. Hint: You will also need to use GROUP BY in the query.
SELECT r.name, COUNT(*) FROM roles AS r RIGHT JOIN users as u on r.id = u.id GROUP BY r.name;
# Other Solution Using Left Join
SELECT roles.name AS 'Role', COUNT(u.role_id) AS 'Count'
FROM roles
LEFT JOIN users u on roles.id = u.role_id
GROUP BY roles.name;