└── README.md
/README.md:
--------------------------------------------------------------------------------
1 |
2 |
3 | # Project Summary
4 |
5 | In this project we will be practicing inserting and querying data using SQL. We'll make use of a handy online tool provided by DevMountain that will allow us to write SQL in your browser. [Click Me](https://postgres.devmountain.com/)
6 |
7 | On the left are the Tables with their fields, the right is where we will be writing our queries, and the bottom is where we will see our results.
8 |
9 | Any new tables or records that we add into the database will be removed after you refresh the page.
10 |
11 | ## Table - person
12 |
13 | ### Instructions
14 | 1. Create a table called person that records a person's id, name, age, height ( in cm ), city, favorite_color.
15 | * id should be an auto-incrementing id/primary key - Use type: SERIAL
16 | 2. Add 5 different people into the person database.
17 | * Remember to not include the person_id because it should auto-increment.
18 | 3. List all the people in the person table by height from tallest to shortest.
19 | 4. List all the people in the person table by height from shortest to tallest.
20 | 5. List all the people in the person table by age from oldest to youngest.
21 | 6. List all the people in the person table older than age 20.
22 | 7. List all the people in the person table that are exactly 18.
23 | 8. List all the people in the person table that are less than 20 and older than 30.
24 | 9. List all the people in the person table that are not 27 (Use not equals).
25 | 10. List all the people in the person table where their favorite color is not red.
26 | 11. List all the people in the person table where their favorite color is not red and is not blue.
27 | 12. List all the people in the person table where their favorite color is orange or green.
28 | 13. List all the people in the person table where their favorite color is orange, green or blue (use IN).
29 | 14. List all the people in the person table where their favorite color is yellow or purple (use IN).
30 |
31 | ### Solution
32 |
33 |
36 |
37 | SQL Solutions
40 |
41 | ```sql
42 | CREATE TABLE person ( person_id SERIAL PRIMARY KEY, name VARCHAR(200), age INTEGER, height INTEGER, city VARCHAR(200), favorite_color VARCHAR(200) );
43 | ```
44 |
45 | #1
50 |
51 | ```sql
52 | INSERT INTO person ( name, age, height, city, favorite_color ) VALUES ( 'First Last', 21, 182, 'City', 'Color' );
53 | ```
54 |
55 | #2
60 |
61 | ```sql
62 | SELECT * FROM person ORDER BY height DESC;
63 | ```
64 |
65 | #3
70 |
71 | ```sql
72 | SELECT * FROM person ORDER BY height ASC;
73 | ```
74 |
75 | #4
80 |
81 | ```sql
82 | SELECT * FROM person ORDER BY age DESC;
83 | ```
84 |
85 | #5
90 |
91 | ```sql
92 | SELECT * FROM person WHERE age > 20;
93 | ```
94 |
95 | #6
100 |
101 | ```sql
102 | SELECT * FROM person WHERE age = 18;
103 | ```
104 |
105 | #7
110 |
111 | ```sql
112 | SELECT * FROM person WHERE age < 20 OR age > 30;
113 | ```
114 |
115 | #8
120 |
121 | ```sql
122 | SELECT * FROM person WHERE age != 27;
123 | ```
124 |
125 | #9
130 |
131 | ```sql
132 | SELECT * FROM person WHERE favorite_color != 'red';
133 | ```
134 |
135 | #10
140 |
141 | ```sql
142 | SELECT * FROM person WHERE favorite_color != 'red' AND favorite_color != 'blue';
143 | ```
144 |
145 | #11
150 |
151 | ```sql
152 | SELECT * FROM person WHERE favorite_color = 'orange' OR favorite_color = 'green';
153 | ```
154 |
155 | #12
160 |
161 | ```sql
162 | SELECT * FROM person WHERE favorite_color IN ( 'orange', 'green', 'blue' );
163 | ```
164 |
165 | #13
170 |
171 | ```sql
172 | SELECT * FROM person WHERE favorite_color IN ( 'yellow', 'purple' )
173 | ```
174 |
175 | #14
197 |
198 | SQL Solutions
201 |
202 | ```sql
203 | CREATE TABLE orders ( order_id SERIAL PRIMARY KEY, person_id INTEGER, product_name VARCHAR(200), product_price NUMERIC, quantity INTEGER );
204 | ```
205 |
206 | #1
211 |
212 | ```sql
213 | INSERT INTO orders ( person_id, product_name, product_price, quantity ) VALUES ( 0, 'Product', 12.50, 2 );
214 | ```
215 |
216 | #2
221 |
222 | ```sql
223 | SELECT * FROM orders;
224 | ```
225 |
226 | #3
231 |
232 | ```sql
233 | SELECT SUM(quantity) FROM orders;
234 | ```
235 |
236 | #4
241 |
242 | ```sql
243 | SELECT SUM(product_price * quantity) FROM orders;
244 | ```
245 |
246 | #5
251 |
252 | ```sql
253 | /* The value of person_id depends on what IDs you used. Use a valid ID from your table */
254 | SELECT SUM(product_price * quantity) FROM orders WHERE person_id = 0;
255 | ```
256 |
257 | #6
276 |
277 | SQL Solutions
280 |
281 | ```sql
282 | INSERT INTO artist ( name ) VALUES ( 'artist name' );
283 | ```
284 |
285 | #1
290 |
291 | ```sql
292 | SELECT * FROM artist ORDER BY name DESC LIMIT 10;
293 | ```
294 |
295 | #2
300 |
301 | ```sql
302 | SELECT * FROM artist ORDER BY name ASC LIMIT 5;
303 | ```
304 |
305 | #3
310 |
311 | ```sql
312 | SELECT * FROM artist WHERE name LIKE 'Black%';
313 | ```
314 |
315 | #4
320 |
321 | ```sql
322 | SELECT * FROM artist WHERE name LIKE '%Black%';
323 | ```
324 |
325 | #5
345 |
346 | SQL Solutions
349 |
350 | ```sql
351 | SELECT first_name, last_name FROM employee WHERE city = 'Calgary';
352 | ```
353 |
354 | #1
359 |
360 | ```sql
361 | SELECT MAX(birth_date) from employee;
362 | ```
363 |
364 | #2
369 |
370 | ```sql
371 | SELECT MIN(birth_date) from employee;
372 | ```
373 |
374 | #3
379 |
380 | ```sql
381 | SELECT * FROM employee WHERE reports_to = 2;
382 | ```
383 |
384 | #4
389 |
390 | ```sql
391 | SELECT COUNT(*) FROM employee WHERE city = 'Lethbridge';
392 | ```
393 |
394 | #5
416 |
417 | SQL Solutions
420 |
421 | ```sql
422 | SELECT COUNT(*) FROM invoice WHERE billing_country = 'USA';
423 | ```
424 |
425 | #1
430 |
431 | ```sql
432 | SELECT MAX(total) FROM invoice;
433 | ```
434 |
435 | #2
440 |
441 | ```sql
442 | SELECT MIN(total) FROM invoice;
443 | ```
444 |
445 | #3
450 |
451 | ```sql
452 | SELECT * FROM invoice WHERE total > 5;
453 | ```
454 |
455 | #4
460 |
461 | ```sql
462 | SELECT COUNT(*) FROM invoice WHERE total < 5;
463 | ```
464 |
465 | #5
470 |
471 | ```sql
472 | SELECT COUNT(*) FROM invoice WHERE billing_state in ('CA', 'TX', 'AZ');
473 | ```
474 |
475 | #6
480 |
481 | ```sql
482 | SELECT AVG(total) FROM invoice;
483 | ```
484 |
485 | #7
490 |
491 | ```sql
492 | SELECT SUM(total) FROM invoice;
493 | ```
494 |
495 | #8
505 |
506 | * [SQL Teaching](http://www.sqlteaching.com/)
507 | * [SQL Bolt](http://sqlbolt.com/)
508 |
509 | SQL
520 |
521 |