-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueries.sql
120 lines (94 loc) · 2.57 KB
/
queries.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
CREATE TABLE student (
id SERIAL PRIMARY KEY,
first_name TEXT,
last_name TEXT
);
-- One to One --
CREATE TABLE contact_detail (
id INTEGER REFERENCES student(id) UNIQUE,
tel TEXT,
address TEXT
);
-- Data --
INSERT INTO student (first_name, last_name)
VALUES ('Angela', 'Yu');
INSERT INTO contact_detail (id, tel, address)
VALUES (1, '+123456789', '123 App Brewery Road');
-- Join --
SELECT *
FROM student
JOIN contact_detail
ON student.id = contact_detail.id
-- Many to One --
CREATE TABLE homework_submission (
id SERIAL PRIMARY KEY,
mark INTEGER,
student_id INTEGER REFERENCES student(id)
);
-- Data --
INSERT INTO homework_submission (mark, student_id)
VALUES (98, 1), (87, 1), (88, 1)
-- Join --
SELECT *
FROM student
JOIN homework_submission
ON student.id = student_id
SELECT student.id, first_name, last_name, mark
FROM student
JOIN homework_submission
ON student.id = student_id
-- Many to Many --
CREATE TABLE class (
id SERIAL PRIMARY KEY,
title VARCHAR(45)
);
CREATE TABLE enrollment (
student_id INTEGER REFERENCES student(id),
class_id INTEGER REFERENCES class(id),
PRIMARY KEY (student_id, class_id)
);
-- Data --
INSERT INTO student (first_name, last_name)
VALUES ('Jack', 'Bauer');
INSERT INTO class (title)
VALUES ('English Literature'), ('Maths'), ('Physics');
INSERT INTO enrollment (student_id, class_id ) VALUES (1, 1), (1, 2);
INSERT INTO enrollment (student_id ,class_id) VALUES (2, 2), (2, 3);
-- Join --
SELECT *
FROM enrollment
JOIN student ON student.id = enrollment.student_id
JOIN class ON class.id = enrollment.class_id;
SELECT student.id AS id, first_name, last_name, title
FROM enrollment
JOIN student ON student.id = enrollment.student_id
JOIN class ON class.id = enrollment.class_id;
-- ALIAS --
SELECT s.id AS id, first_name, last_name, title
FROM enrollment AS e
JOIN student AS s ON s.id = e.student_id
JOIN class AS c ON c.id = e.class_id;
SELECT s.id AS id, first_name, last_name, title
FROM enrollment e
JOIN student s ON s.id = e.student_id
JOIN class c ON c.id = e.class_id;
-- EXERCISE SOLUTION AND SETUP --
DROP TABLE IF EXISTS visited_countries, users;
CREATE TABLE users(
id SERIAL PRIMARY KEY,
name VARCHAR(15) UNIQUE NOT NULL,
color VARCHAR(15)
);
CREATE TABLE visited_countries(
id SERIAL PRIMARY KEY,
country_code CHAR(2) NOT NULL,
user_id INTEGER REFERENCES users(id)
);
INSERT INTO users (name, color)
VALUES ('Aditya', 'teal'), ('Pradnya', 'powderblue');
INSERT INTO visited_countries (country_code, user_id)
VALUES ('IN', 1), ('US', 1), ('CA', 2), ('IN', 2 );
SELECT *
FROM visited_countries
JOIN users
ON users.id = user_id;