-
Notifications
You must be signed in to change notification settings - Fork 0
/
Famous People.sql
317 lines (265 loc) · 9.49 KB
/
Famous People.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/* Create a table named person */
CREATE TABLE persons
(id SERIAL PRIMARY KEY NOT NULL,
full_name VARCHAR (20) NOT NULL,
age INTEGER NOt NULL);
/* Insert values into the table */
INSERT INTO persons (full_name, age)
VALUES ('Bobby McBobbyFace', 12);
INSERT INTO persons (full_name, age)
VALUES ('Lucy BoBucie', 25);
INSERT INTO persons (full_name, age)
VALUES ('Banana FoFanna', 14);
INSERT INTO persons (full_name, age)
VALUES ('Shish Kabob', 20);
INSERT INTO persons (full_name, age)
VALUES ('Fluffy Sparkles', 8);
INSERT INTO persons (full_name, age)
VALUES ('Jerry Jay', 9);
/* Create a table named hobbies */
CREATE TABLE hobbies
(id SERIAL PRIMARY KEY NOT NULL,
person_id INTEGER NOT NULL,
game VARCHAR (20) NOT NULL);
/* Insert values into the table */
INSERT INTO hobbies (person_id, game)
VALUES (1, 'drawing');
INSERT INTO hobbies (person_id, game)
VALUES (1, 'coding');
INSERT INTO hobbies (person_id, game)
VALUES (2, 'dancing');
INSERT INTO hobbies (person_id, game)
VALUES (2, 'coding');
INSERT INTO hobbies (person_id, game)
VALUES (3, 'skating');
INSERT INTO hobbies (person_id, game)
VALUES (3, 'rowing');
INSERT INTO hobbies (person_id, game)
VALUES (3, 'drawing');
INSERT INTO hobbies (person_id, game)
VALUES (4, 'coding');
INSERT INTO hobbies (person_id, game)
VALUES (4, 'dilly-dallying');
INSERT INTO hobbies (person_id, game)
VALUES (4, 'meowing');
INSERT INTO hobbies (person_id, game)
VALUES (5, 'hunting');
/* Create table named person */
CREATE table friends
(id SERIAL PRIMARY KEY NOT NULL,
person1_id INTEGER,
person2_id INTEGER);
/* Insert values into the table */
INSERT INTO friends (person1_id, person2_id)
VALUES (1, 4);
INSERT INTO friends (person1_id, person2_id)
VALUES (2, 3);
/*
● We've created a database of people and hobbies, and each row in hobbies is related to a
row in persons via the person_id column. In this first step, insert one more row in persons
and then one more row in hobbies that is related to the newly inserted person.
● Now, select the 3 tables with a join so that you can see each person's name next to their
hobby.
● Now, add an additional query that shows only the name and hobbies of 'Bobby
McBobbyFace', using JOIN combined with WHERE.
*/
/* Query all the tables */
SELECT *
FROM persons;
SELECT *
FROM hobbies;
SELECT *
FROM friends;
/* Inserting additional row to persons and hobbies tables respectively */
INSERT INTO persons (full_name, age)
VALUES ('Dominic Appiah', 21);
INSERT INTO hobbies (person_id, game)
VALUES (7, 'coding');
/* Joining peoples name with thier hobbies */
SELECT persons.full_name, hobbies.game
FROM persons
JOIN hobbies
ON persons.id = hobbies.person_id;
/* Query the name and hobbies of 'Bobby McBobbyFace' */
SELECT persons.full_name, hobbies.game
FROM persons
JOIN hobbies
ON persons.id = hobbies.person_id
WHERE persons.age = 12;
/* Joining names to each per friend */
SELECT p1.full_name, p2.full_name
FROM friends
JOIN persons p1
ON p1.id = friends.person1_id
JOIN persons p2
ON p2.id = friends.person2_id;
/* Create table named customers */
CREATE TABLE customers
(id SERIAL PRIMARY KEY NOT NULL,
name VARCHAR (20) NOT NULL,
email TEXT);
/* Insert valuse intp the table */
INSERT INTO customers (name, email)
VALUES ('Doctor Who', '[email protected]');
INSERT INTO customers (name, email)
VALUES ('Harry Potter', '[email protected]');
INSERT INTO customers (name, email)
VALUES ('Captain Awesome', '[email protected]');
/* Create a table named order */
CREATE TABLE orders
(id SERIAL PRIMARY KEY NOT NULL,
customer_id INTEGER NOT NULL,
item TEXT NOT NULL,
price REAL NOT NULL);
/* Inter values into the table */
INSERT INTO orders (customer_id, item, price)
VALUES (1, 'Sonic Screwdriver', 1000.00);
INSERT INTO orders (customer_id, item, price)
VALUES (2, 'High Quality Broomstick', 40.00);
INSERT INTO orders (customer_id, item, price)
VALUES (1, 'TARDIS', 1000000.00);
/*
We've created a database for customers and their orders. Not all of the customers have
made orders, however. Come up with a query that lists the name and email of every
customer followed by the item and price of orders they've made. Use a LEFT OUTER JOIN
so that a customer is listed even if they've made no orders, and don't add any ORDER BY.
Now, create another query that will result in one row per each customer, with their name, email,
and total amount of money they've spent on orders. Sort the rows according to the total money
spent, from the most spent to the least spent.
(Tip: You should always GROUP BY on the column that is most likely to be unique in a row.)
*/
/* Query all the tables */
SELECT *
FROM customers;
SELECT *
FROM orders;
/* Query the lists of name and email of every
customer followed by the item and price of orders they've made. */
SELECT customers.name, email, orders.item, orders.price
FROM customers
LEFT OUTER JOIN orders
ON customers.id = orders.customer_id;
/* Query a row per each customer, with their name, email,
and total amount of money they've spent on orders. */
SELECT customers.name, email, SUM(price) AS total_amount
FROM customers
LEFT OUTER JOIN orders
ON orders.customer_id = customers.id
GROUP BY customers.name, email;
/* Create a table movies named */
CREATE TABLE movies
(id SERIAL PRIMARY KEY NOT NULL,
title TEXT NOT NULL,
released INTEGER NOT NULL,
sequel_id INTEGER);
/* Insert values into the tables */
INSERT INTO movies
VALUES (1, 'Harry Potter and the Philosopher Stone', 2001, 2);
INSERT INTO movies
VALUES (2, 'Harry Potter and the Chamber of Secrets', 2002, 3);
INSERT INTO movies
VALUES (3, 'Harry Potter and the Prisoner of Azkaban', 2004, 4);
INSERT INTO movies
VALUES (4, 'Harry Potter and the Goblet of Fire', 2005, 5);
INSERT INTO movies
VALUES (5, 'Harry Potter and the Order of the Phoenix', 2007, 6);
INSERT INTO movies
VALUES (6, 'Harry Potter and the Half-Blood Prince', 2009, 7);
INSERT INTO movies
VALUES (7, 'Harry Potter and the Deathly Hallows – Part 1', 2010, 8);
INSERT INTO movies
VALUES (8, 'Harry Potter and the Deathly Hallows – Part 2', 2011, NULL);
/*
We've created a table with all the 'Harry Potter' movies, with a sequel_id column that
matches the id of the sequel for each movie. Issue a SELECT that will show the title of each
movie next to its sequel's title (or NULL if it doesn't have a sequel).
*/
/* Query movies table */
SELECT *
FROM movies;
/* Query the title of each movie next to its sequel's title
(or NULL if it doesn't have a sequel) */
SELECT movies.title, m1.title
FROM movies
JOIN movies m1
ON m1.sequel_id = movies.id;
/*
We've created a database for a documents app, with rows for each document with it's title,
content, and author. In this first step, use UPDATE to change the author to 'Jackie Draper' for all
rows where it's currently 'Jackie Paper'. Then re-select all the rows to make sure the table
changed like you expected.
Step 2
Now you'll delete a row, being very careful not to delete all the rows. Only delete the row where
the title is 'Things I'm Afraid Of'. Then re-select all the rows to make sure the table changed like
you expected.
*/
/* Create a table named documents */
CREATE TABLE documents
(id SERIAL PRIMARY KEY NOT NULL,
title TEXT NOT NULL,
content TEXT NOT NULL,
author TEXT NOT NULL);
/* Insert values into the table */
INSERT INTO documents (title, content, author)
VALUES ('Rich Dad Poor Dad', 'Financial Education', 'Robert Kiyosaki'),
('Cashflow Quadrant', 'Guide to financial freedom', 'Robert Kiyosaki'),
('Coding for Life', 'Learning to code within 100 days', 'Jackie Draper'),
('Time Management', 'Things I am Afraid Of', 'Paul Asamoah'),
('Python for Backend', 'How to use Python for backend', 'Jackie Draper');
/* Query the document table */
SELECT *
FROM documents;
/* Changing the author name "Jackie Draper" to "Jackie Paper" */
UPDATE documents
SET author = 'Jackie Paper'
WHERE id = 5;
UPDATE documents
SET author = 'Jackie Paper'
WHERE id = 3;
/* Delete row that contains "Things I'm Afraid Of" */
DELETE FROM documents
WHERE id = 4;
/*
We've created a database of clothes, and decided we need a price column. Use ALTER to add a
'price' column to the table. Then select all the columns in each row to see what your table looks
like now.
Step 2
Now assign each item a price, using UPDATE - item 1 should be 10 dollars, item 2 should be 20
dollars, item 3 should be 30 dollars. When you're done, do another SELECT of all the rows to
check that it worked as expected.
Step 3
Now insert a new item into the table that has all three attributes filled in, including 'price'. Do one
final SELECT of all the rows to check it worked.
*/
/* Create a table named clothes */
CREATE TABLE clothes
(id SERIAL PRIMARY KEY NOT NULL,
type TEXT NOT NULL,
design TEXT NOT NULL);
/* Insert values into the table */
INSERT INTO clothes (type, design)
VALUES ('Jacket', 'Burberry'),
('Sleeve', 'Sam Sheikh'),
('Dress', 'Oscar de la');
/* Query clothes table */
SELECT *
FROM clothes;
/* Adding a price column to the clothes table */
ALTER TABLE clothes
ADD price INT;
/* Assigning price to each item in the price column */
UPDATE clothes
SET price = 10
WHERE id = 1;
UPDATE clothes
SET price = 20
WHERE id = 2;
UPDATE clothes
SET price = 30
WHERE id = 3;
/* Inserting new item into the table */
INSERT INTO clothes (type, design, price)
VALUES ('Pyjamas', 'Olivia von Halle', 40);
/* Query clothes table */
SELECT *
FROM clothes;