└── 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 |
34 | 35 | SQL Solutions 36 | 37 |
38 | 39 | #1 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 |
46 | 47 |
48 | 49 | #2 50 | 51 | ```sql 52 | INSERT INTO person ( name, age, height, city, favorite_color ) VALUES ( 'First Last', 21, 182, 'City', 'Color' ); 53 | ``` 54 | 55 |
56 | 57 |
58 | 59 | #3 60 | 61 | ```sql 62 | SELECT * FROM person ORDER BY height DESC; 63 | ``` 64 | 65 |
66 | 67 |
68 | 69 | #4 70 | 71 | ```sql 72 | SELECT * FROM person ORDER BY height ASC; 73 | ``` 74 | 75 |
76 | 77 |
78 | 79 | #5 80 | 81 | ```sql 82 | SELECT * FROM person ORDER BY age DESC; 83 | ``` 84 | 85 |
86 | 87 |
88 | 89 | #6 90 | 91 | ```sql 92 | SELECT * FROM person WHERE age > 20; 93 | ``` 94 | 95 |
96 | 97 |
98 | 99 | #7 100 | 101 | ```sql 102 | SELECT * FROM person WHERE age = 18; 103 | ``` 104 | 105 |
106 | 107 |
108 | 109 | #8 110 | 111 | ```sql 112 | SELECT * FROM person WHERE age < 20 OR age > 30; 113 | ``` 114 | 115 |
116 | 117 |
118 | 119 | #9 120 | 121 | ```sql 122 | SELECT * FROM person WHERE age != 27; 123 | ``` 124 | 125 |
126 | 127 |
128 | 129 | #10 130 | 131 | ```sql 132 | SELECT * FROM person WHERE favorite_color != 'red'; 133 | ``` 134 | 135 |
136 | 137 |
138 | 139 | #11 140 | 141 | ```sql 142 | SELECT * FROM person WHERE favorite_color != 'red' AND favorite_color != 'blue'; 143 | ``` 144 | 145 |
146 | 147 |
148 | 149 | #12 150 | 151 | ```sql 152 | SELECT * FROM person WHERE favorite_color = 'orange' OR favorite_color = 'green'; 153 | ``` 154 | 155 |
156 | 157 |
158 | 159 | #13 160 | 161 | ```sql 162 | SELECT * FROM person WHERE favorite_color IN ( 'orange', 'green', 'blue' ); 163 | ``` 164 | 165 |
166 | 167 |
168 | 169 | #14 170 | 171 | ```sql 172 | SELECT * FROM person WHERE favorite_color IN ( 'yellow', 'purple' ) 173 | ``` 174 | 175 |
176 | 177 |
178 | 179 | ## Table - orders 180 | 181 | ### Instructions 182 | 183 | 1. Create a table called orders that records: order_id, person_id, product_name, product_price, quantity. 184 | 2. Add 5 orders to the orders table. 185 | * Make orders for at least two different people. 186 | * person_id should be different for different people. 187 | 3. Select all the records from the orders table. 188 | 4. Calculate the total number of products ordered. 189 | 5. Calculate the total order price. 190 | 6. Calculate the total order price by a single person_id. 191 | 192 | ### Solution 193 | 194 |
195 | 196 | SQL Solutions 197 | 198 |
199 | 200 | #1 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 |
207 | 208 |
209 | 210 | #2 211 | 212 | ```sql 213 | INSERT INTO orders ( person_id, product_name, product_price, quantity ) VALUES ( 0, 'Product', 12.50, 2 ); 214 | ``` 215 | 216 |
217 | 218 |
219 | 220 | #3 221 | 222 | ```sql 223 | SELECT * FROM orders; 224 | ``` 225 | 226 |
227 | 228 |
229 | 230 | #4 231 | 232 | ```sql 233 | SELECT SUM(quantity) FROM orders; 234 | ``` 235 | 236 |
237 | 238 |
239 | 240 | #5 241 | 242 | ```sql 243 | SELECT SUM(product_price * quantity) FROM orders; 244 | ``` 245 | 246 |
247 | 248 |
249 | 250 | #6 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 |
258 | 259 |
260 | 261 | ## Table - artist 262 | 263 | ### Instructions 264 | 265 | 1. Add 3 new artists to the artist table. ( It's already created ) 266 | 2. Select 10 artists in reverse alphabetical order. 267 | 3. Select 5 artists in alphabetical order. 268 | 4. Select all artists that start with the word 'Black'. 269 | 5. Select all artists that contain the word 'Black'. 270 | 271 | ### Solution 272 | 273 |
274 | 275 | SQL Solutions 276 | 277 |
278 | 279 | #1 280 | 281 | ```sql 282 | INSERT INTO artist ( name ) VALUES ( 'artist name' ); 283 | ``` 284 | 285 |
286 | 287 |
288 | 289 | #2 290 | 291 | ```sql 292 | SELECT * FROM artist ORDER BY name DESC LIMIT 10; 293 | ``` 294 | 295 |
296 | 297 |
298 | 299 | #3 300 | 301 | ```sql 302 | SELECT * FROM artist ORDER BY name ASC LIMIT 5; 303 | ``` 304 | 305 |
306 | 307 |
308 | 309 | #4 310 | 311 | ```sql 312 | SELECT * FROM artist WHERE name LIKE 'Black%'; 313 | ``` 314 | 315 |
316 | 317 |
318 | 319 | #5 320 | 321 | ```sql 322 | SELECT * FROM artist WHERE name LIKE '%Black%'; 323 | ``` 324 | 325 |
326 | 327 |
328 | 329 | ## Table - employee 330 | 331 | ### Instructions 332 | 333 | 1. List all employee first and last names only that live in Calgary. 334 | 2. Find the birthdate for the youngest employee. 335 | 3. Find the birthdate for the oldest employee. 336 | 4. Find everyone that reports to Nancy Edwards (Use the ReportsTo column). 337 | * You will need to query the employee table to find the Id for Nancy Edwards 338 | 5. Count how many people live in Lethbridge. 339 | 340 | ### Solution 341 | 342 |
343 | 344 | SQL Solutions 345 | 346 |
347 | 348 | #1 349 | 350 | ```sql 351 | SELECT first_name, last_name FROM employee WHERE city = 'Calgary'; 352 | ``` 353 | 354 |
355 | 356 |
357 | 358 | #2 359 | 360 | ```sql 361 | SELECT MAX(birth_date) from employee; 362 | ``` 363 | 364 |
365 | 366 |
367 | 368 | #3 369 | 370 | ```sql 371 | SELECT MIN(birth_date) from employee; 372 | ``` 373 | 374 |
375 | 376 |
377 | 378 | #4 379 | 380 | ```sql 381 | SELECT * FROM employee WHERE reports_to = 2; 382 | ``` 383 | 384 |
385 | 386 |
387 | 388 | #5 389 | 390 | ```sql 391 | SELECT COUNT(*) FROM employee WHERE city = 'Lethbridge'; 392 | ``` 393 | 394 |
395 | 396 |
397 | 398 | ## Table - invoice 399 | 400 | ### Instructions 401 | 402 | 1. Count how many orders were made from the USA. 403 | 2. Find the largest order total amount. 404 | 3. Find the smallest order total amount. 405 | 4. Find all orders bigger than $5. 406 | 5. Count how many orders were smaller than $5. 407 | 6. Count how many orders were in CA, TX, or AZ (use IN). 408 | 7. Get the average total of the orders. 409 | 8. Get the total sum of the orders. 410 | 411 | ### Solution 412 | 413 |
414 | 415 | SQL Solutions 416 | 417 |
418 | 419 | #1 420 | 421 | ```sql 422 | SELECT COUNT(*) FROM invoice WHERE billing_country = 'USA'; 423 | ``` 424 | 425 |
426 | 427 |
428 | 429 | #2 430 | 431 | ```sql 432 | SELECT MAX(total) FROM invoice; 433 | ``` 434 | 435 |
436 | 437 |
438 | 439 | #3 440 | 441 | ```sql 442 | SELECT MIN(total) FROM invoice; 443 | ``` 444 | 445 |
446 | 447 |
448 | 449 | #4 450 | 451 | ```sql 452 | SELECT * FROM invoice WHERE total > 5; 453 | ``` 454 | 455 |
456 | 457 |
458 | 459 | #5 460 | 461 | ```sql 462 | SELECT COUNT(*) FROM invoice WHERE total < 5; 463 | ``` 464 | 465 |
466 | 467 |
468 | 469 | #6 470 | 471 | ```sql 472 | SELECT COUNT(*) FROM invoice WHERE billing_state in ('CA', 'TX', 'AZ'); 473 | ``` 474 | 475 |
476 | 477 |
478 | 479 | #7 480 | 481 | ```sql 482 | SELECT AVG(total) FROM invoice; 483 | ``` 484 | 485 |
486 | 487 |
488 | 489 | #8 490 | 491 | ```sql 492 | SELECT SUM(total) FROM invoice; 493 | ``` 494 | 495 |
496 | 497 |
498 | 499 | 500 | ## Resources 501 | 502 |
503 | 504 | SQL 505 | 506 | * [SQL Teaching](http://www.sqlteaching.com/) 507 | * [SQL Bolt](http://sqlbolt.com/) 508 | 509 |
510 | 511 | ## Contributions 512 | 513 | If you see a problem or a typo, please fork, make the necessary changes, and create a pull request so we can review your changes and merge them into the master repo and branch. 514 | 515 | ## Copyright 516 | 517 | © DevMountain LLC, 2017. Unauthorized use and/or duplication of this material without express and written permission from DevMountain, LLC is strictly prohibited. Excerpts and links may be used, provided that full and clear credit is given to DevMountain with appropriate and specific direction to the original content. 518 | 519 |

520 | 521 |

522 | 523 | --------------------------------------------------------------------------------