├── ChinookDatabaseSchema.png ├── DataScientistRolePlay.txt ├── README.md ├── SQL_data_science_quiz_1.sql ├── SQL_data_science_quiz_2.sql ├── SQL_data_science_quiz_3.sql ├── SQL_data_science_quiz_4.sql └── YelpDatabaseSchema.png /ChinookDatabaseSchema.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/trentparkinson/SQL-for-Data-Science/9e6aebaf60b25bf99d8d312244b3e6ab6b4165d7/ChinookDatabaseSchema.png -------------------------------------------------------------------------------- /DataScientistRolePlay.txt: -------------------------------------------------------------------------------- 1 | 2 | By: Trent Parkinson 3 | Date: Jan-18th-2018 4 | 5 | Data Scientist Role Play: Profiling and Analyzing the Yelp Dataset Coursera Worksheet 6 | 7 | This is a 2-part assignment. In the first part, you are asked a series of questions that 8 | will help you profile and understand the data just like a data scientist would. For this 9 | first part of the assignment, you will be assessed both on the correctness of your 10 | findings, as well as the code you used to arrive at your answer. You will be graded on 11 | how easy your code is to read, so remember to use proper formatting and comments where 12 | necessary. 13 | 14 | In the second part of the assignment, you are asked to come up with your own inferences 15 | and analysis of the data for a particular research question you want to answer. You will be 16 | required to prepare the dataset for the analysis you choose to do. As with the first part, 17 | you will be graded, in part, on how easy your code is to read, so use proper formatting 18 | and comments to illustrate and communicate your intent as required. 19 | 20 | For both parts of this assignment, use this "worksheet." It provides all the questions 21 | you are being asked, and your job will be to transfer your answers and SQL coding where 22 | indicated into this worksheet so that your peers can review your work. You should be able 23 | to use any Text Editor (Windows Notepad, Apple TextEdit, Notepad ++, Sublime Text, etc.) 24 | to copy and paste your answers. If you are going to use Word or some other page layout 25 | application, just be careful to make sure your answers and code are lined appropriately. 26 | In this case, you may want to save as a PDF to ensure your formatting remains intact 27 | for you reviewer. 28 | 29 | 30 | 31 | Part 1: Yelp Dataset Profiling and Understanding 32 | 33 | 1. Profile the data by finding the total number of records for each of the tables below: 34 | 35 | SELECT COUNT(*) 36 | FROM table 37 | 38 | i. Attribute table = 10000 39 | ii. Business table = 10000 40 | iii. Category table = 10000 41 | iv. Checkin table = 10000 42 | v. elite_years table = 10000 43 | vi. friend table = 10000 44 | vii. hours table = 10000 45 | viii. photo table = 10000 46 | ix. review table = 10000 47 | x. tip table = 10000 48 | xi. user table = 10000 49 | 50 | 51 | 52 | 2. Find the total number of distinct records for each of the keys listed below: 53 | 54 | SELECT COUNT(DISTINCT(key)) 55 | FROM table 56 | 57 | i. Business = id: 10000 58 | ii. Hours = business_id: 1562 59 | iii. Category = business_id: 2643 60 | iv. Attribute = business_id: 1115 61 | v. Review = id:10000, business_id: 8090, user_id: 9581 62 | vi. Checkin = business_id: 493 63 | vii. Photo = id: 10000, business_id: 6493 64 | viii. Tip = user_id: 537, business_id: 3979 65 | ix. User = id: 10000 66 | x. Friend = user_id: 11 67 | xi. Elite_years = user_id: 2780 68 | 69 | 70 | 71 | 3. Are there any columns with null values in the Users table? Indicate "yes," or "no." 72 | 73 | Answer: "no" 74 | 75 | 76 | SQL code used to arrive at answer: 77 | 78 | SELECT COUNT(*) 79 | FROM user 80 | WHERE id IS NULL OR 81 | name IS NULL OR 82 | review_count IS NULL OR 83 | yelping_since IS NULL OR 84 | useful IS NULL OR 85 | funny IS NULL OR 86 | cool IS NULL OR 87 | fans IS NULL OR 88 | average_stars IS NULL OR 89 | compliment_hot IS NULL OR 90 | compliment_more IS NULL OR 91 | compliment_profile IS NULL OR 92 | compliment_cute IS NULL OR 93 | compliment_list IS NULL OR 94 | compliment_note IS NULL OR 95 | compliment_plain IS NULL OR 96 | compliment_cool IS NULL OR 97 | compliment_funny IS NULL OR 98 | compliment_writer IS NULL OR 99 | compliment_photos IS NULL 100 | 101 | 102 | 103 | 4. Find the minimum, maximum, and average value for the following fields: 104 | 105 | SELECT AVG(column) 106 | FROM table 107 | 108 | i. Table: Review, Column: Stars 109 | 110 | min: 1 max: 5 avg: 3.7082 111 | 112 | 113 | ii. Table: Business, Column: Stars 114 | 115 | min: 1 max: 5 avg: 3.6549 116 | 117 | 118 | iii. Table: Tip, Column: Likes 119 | 120 | min: 0 max: 2 avg: 0.0144 121 | 122 | 123 | iv. Table: Checkin, Column: Count 124 | 125 | min: 1 max: 53 avg: 1.9414 126 | 127 | 128 | v. Table: User, Column: Review_count 129 | 130 | min: 0 max: 2000 avg: 24.2995 131 | 132 | 133 | 134 | 5. List the cities with the most reviews in descending order: 135 | 136 | SQL code used to arrive at answer: 137 | 138 | SELECT city, 139 | SUM(review_count) AS reviews 140 | FROM business 141 | GROUP BY city 142 | ORDER BY reviews DESC 143 | 144 | Copy and Paste the Result Below: 145 | 146 | +-----------------+---------+ 147 | | city | reviews | 148 | +-----------------+---------+ 149 | | Las Vegas | 82854 | 150 | | Phoenix | 34503 | 151 | | Toronto | 24113 | 152 | | Scottsdale | 20614 | 153 | | Charlotte | 12523 | 154 | | Henderson | 10871 | 155 | | Tempe | 10504 | 156 | | Pittsburgh | 9798 | 157 | | Montréal | 9448 | 158 | | Chandler | 8112 | 159 | | Mesa | 6875 | 160 | | Gilbert | 6380 | 161 | | Cleveland | 5593 | 162 | | Madison | 5265 | 163 | | Glendale | 4406 | 164 | | Mississauga | 3814 | 165 | | Edinburgh | 2792 | 166 | | Peoria | 2624 | 167 | | North Las Vegas | 2438 | 168 | | Markham | 2352 | 169 | | Champaign | 2029 | 170 | | Stuttgart | 1849 | 171 | | Surprise | 1520 | 172 | | Lakewood | 1465 | 173 | | Goodyear | 1155 | 174 | +-----------------+---------+ 175 | 176 | 177 | 6. Find the distribution of star ratings to the business in the following cities: 178 | 179 | i. Avon 180 | 181 | SQL code used to arrive at answer: 182 | 183 | SELECT stars, 184 | SUM(review_count) AS count 185 | FROM business 186 | WHERE city == 'Avon' 187 | GROUP BY stars 188 | 189 | 190 | Copy and Paste the Resulting Table Below (2 columns - star rating and count): 191 | 192 | +-------+-------+ 193 | | stars | count | 194 | +-------+-------+ 195 | | 1.5 | 10 | 196 | | 2.5 | 6 | 197 | | 3.5 | 88 | 198 | | 4.0 | 21 | 199 | | 4.5 | 31 | 200 | | 5.0 | 3 | 201 | +-------+-------+ 202 | 203 | 204 | ii. Beachwood 205 | 206 | SQL code used to arrive at answer: 207 | 208 | SELECT stars, 209 | SUM(review_count) AS count 210 | FROM business 211 | WHERE city == 'Beachwood' 212 | GROUP BY stars 213 | 214 | Copy and Paste the Resulting Table Below (2 columns - star rating and count): 215 | 216 | +-------+-------+ 217 | | stars | count | 218 | +-------+-------+ 219 | | 2.0 | 8 | 220 | | 2.5 | 3 | 221 | | 3.0 | 11 | 222 | | 3.5 | 6 | 223 | | 4.0 | 69 | 224 | | 4.5 | 17 | 225 | | 5.0 | 23 | 226 | +-------+-------+ 227 | 228 | 229 | 7. Find the top 3 users based on their total number of reviews: 230 | 231 | SQL code used to arrive at answer: 232 | 233 | SELECT id, 234 | name, 235 | review_count 236 | FROM user 237 | ORDER BY review_count DESC 238 | LIMIT 3 239 | 240 | Copy and Paste the Result Below: 241 | 242 | +------------------------+--------+--------------+ 243 | | id | name | review_count | 244 | +------------------------+--------+--------------+ 245 | | -G7Zkl1wIWBBmD0KRy_sCw | Gerald | 2000 | 246 | | -3s52C4zL_DHRK0ULG6qtg | Sara | 1629 | 247 | | -8lbUNlXVSoXqaRRiHiSNg | Yuri | 1339 | 248 | +------------------------+--------+--------------+ 249 | 250 | 8. Does posing more reviews correlate with more fans? 251 | 252 | Yes, but also the amount of time that they have been yelping. The longer they 253 | have been yelping and the more reviews they give has a higher fan count. 254 | 255 | Please explain your findings and interpretation of the results: 256 | 257 | SELECT id, 258 | name, 259 | review_count, 260 | fans, 261 | yelping_since 262 | FROM user 263 | ORDER BY fans DESC 264 | 265 | +------------------------+-----------+--------------+------+---------------------+ 266 | | id | name | review_count | fans | yelping_since | 267 | +------------------------+-----------+--------------+------+---------------------+ 268 | | -9I98YbNQnLdAmcYfb324Q | Amy | 609 | 503 | 2007-07-19 00:00:00 | 269 | | -8EnCioUmDygAbsYZmTeRQ | Mimi | 968 | 497 | 2011-03-30 00:00:00 | 270 | | --2vR0DIsmQ6WfcSzKWigw | Harald | 1153 | 311 | 2012-11-27 00:00:00 | 271 | | -G7Zkl1wIWBBmD0KRy_sCw | Gerald | 2000 | 253 | 2012-12-16 00:00:00 | 272 | | -0IiMAZI2SsQ7VmyzJjokQ | Christine | 930 | 173 | 2009-07-08 00:00:00 | 273 | | -g3XIcCb2b-BD0QBCcq2Sw | Lisa | 813 | 159 | 2009-10-05 00:00:00 | 274 | | -9bbDysuiWeo2VShFJJtcw | Cat | 377 | 133 | 2009-02-05 00:00:00 | 275 | | -FZBTkAZEXoP7CYvRV2ZwQ | William | 1215 | 126 | 2015-02-19 00:00:00 | 276 | | -9da1xk7zgnnfO1uTVYGkA | Fran | 862 | 124 | 2012-04-05 00:00:00 | 277 | | -lh59ko3dxChBSZ9U7LfUw | Lissa | 834 | 120 | 2007-08-14 00:00:00 | 278 | | -B-QEUESGWHPE_889WJaeg | Mark | 861 | 115 | 2009-05-31 00:00:00 | 279 | | -DmqnhW4Omr3YhmnigaqHg | Tiffany | 408 | 111 | 2008-10-28 00:00:00 | 280 | | -cv9PPT7IHux7XUc9dOpkg | bernice | 255 | 105 | 2007-08-29 00:00:00 | 281 | | -DFCC64NXgqrxlO8aLU5rg | Roanna | 1039 | 104 | 2006-03-28 00:00:00 | 282 | | -IgKkE8JvYNWeGu8ze4P8Q | Angela | 694 | 101 | 2010-10-01 00:00:00 | 283 | | -K2Tcgh2EKX6e6HqqIrBIQ | .Hon | 1246 | 101 | 2006-07-19 00:00:00 | 284 | | -4viTt9UC44lWCFJwleMNQ | Ben | 307 | 96 | 2007-03-10 00:00:00 | 285 | | -3i9bhfvrM3F1wsC9XIB8g | Linda | 584 | 89 | 2005-08-07 00:00:00 | 286 | | -kLVfaJytOJY2-QdQoCcNQ | Christina | 842 | 85 | 2012-10-08 00:00:00 | 287 | | -ePh4Prox7ZXnEBNGKyUEA | Jessica | 220 | 84 | 2009-01-12 00:00:00 | 288 | | -4BEUkLvHQntN6qPfKJP2w | Greg | 408 | 81 | 2008-02-16 00:00:00 | 289 | | -C-l8EHSLXtZZVfUAUhsPA | Nieves | 178 | 80 | 2013-07-08 00:00:00 | 290 | | -dw8f7FLaUmWR7bfJ_Yf0w | Sui | 754 | 78 | 2009-09-07 00:00:00 | 291 | | -8lbUNlXVSoXqaRRiHiSNg | Yuri | 1339 | 76 | 2008-01-03 00:00:00 | 292 | | -0zEEaDFIjABtPQni0XlHA | Nicole | 161 | 73 | 2009-04-30 00:00:00 | 293 | +------------------------+-----------+--------------+------+---------------------+ 294 | 295 | 296 | 297 | 9. Are there more reviews with the word "love" or with the word "hate" in them? 298 | 299 | Answer: love has 1780, while hate only has 232 :) 'love prevails' 300 | 301 | 302 | SQL code used to arrive at answer: 303 | 304 | SELECT COUNT(*) SELECT COUNT(*) 305 | FROM review FROM review 306 | WHERE text LIKE "%love%" WHERE text LIKE "%hate%" 307 | 308 | = 1780 = 232 309 | 310 | 10. Find the top 10 users with the most fans: 311 | 312 | SQL code used to arrive at answer: 313 | 314 | SELECT id, 315 | name, 316 | fans 317 | FROM user 318 | ORDER BY fans DESC 319 | LIMIT 10 320 | 321 | 322 | Copy and Paste the Result Below: 323 | 324 | +------------------------+-----------+------+ 325 | | id | name | fans | 326 | +------------------------+-----------+------+ 327 | | -9I98YbNQnLdAmcYfb324Q | Amy | 503 | 328 | | -8EnCioUmDygAbsYZmTeRQ | Mimi | 497 | 329 | | --2vR0DIsmQ6WfcSzKWigw | Harald | 311 | 330 | | -G7Zkl1wIWBBmD0KRy_sCw | Gerald | 253 | 331 | | -0IiMAZI2SsQ7VmyzJjokQ | Christine | 173 | 332 | | -g3XIcCb2b-BD0QBCcq2Sw | Lisa | 159 | 333 | | -9bbDysuiWeo2VShFJJtcw | Cat | 133 | 334 | | -FZBTkAZEXoP7CYvRV2ZwQ | William | 126 | 335 | | -9da1xk7zgnnfO1uTVYGkA | Fran | 124 | 336 | | -lh59ko3dxChBSZ9U7LfUw | Lissa | 120 | 337 | +------------------------+-----------+------+ 338 | 339 | 340 | 11. Is there a strong correlation between having a high number of fans and being listed 341 | as "useful" or "funny?" 342 | 343 | Yes, see interpretation. 344 | 345 | SQL code used to arrive at answer: 346 | 347 | SELECT name, 348 | fans, 349 | useful, 350 | funny, 351 | review_count, 352 | yelping_since 353 | FROM user 354 | ORDER BY fans DESC 355 | 356 | Copy and Paste the Result Below: 357 | 358 | +-----------+------+--------+--------+--------------+---------------------+ 359 | | name | fans | useful | funny | review_count | yelping_since | 360 | +-----------+------+--------+--------+--------------+---------------------+ 361 | | Amy | 503 | 3226 | 2554 | 609 | 2007-07-19 00:00:00 | 362 | | Mimi | 497 | 257 | 138 | 968 | 2011-03-30 00:00:00 | 363 | | Harald | 311 | 122921 | 122419 | 1153 | 2012-11-27 00:00:00 | 364 | | Gerald | 253 | 17524 | 2324 | 2000 | 2012-12-16 00:00:00 | 365 | | Christine | 173 | 4834 | 6646 | 930 | 2009-07-08 00:00:00 | 366 | | Lisa | 159 | 48 | 13 | 813 | 2009-10-05 00:00:00 | 367 | | Cat | 133 | 1062 | 672 | 377 | 2009-02-05 00:00:00 | 368 | | William | 126 | 9363 | 9361 | 1215 | 2015-02-19 00:00:00 | 369 | | Fran | 124 | 9851 | 7606 | 862 | 2012-04-05 00:00:00 | 370 | | Lissa | 120 | 455 | 150 | 834 | 2007-08-14 00:00:00 | 371 | | Mark | 115 | 4008 | 570 | 861 | 2009-05-31 00:00:00 | 372 | | Tiffany | 111 | 1366 | 984 | 408 | 2008-10-28 00:00:00 | 373 | | bernice | 105 | 120 | 112 | 255 | 2007-08-29 00:00:00 | 374 | | Roanna | 104 | 2995 | 1188 | 1039 | 2006-03-28 00:00:00 | 375 | | Angela | 101 | 158 | 164 | 694 | 2010-10-01 00:00:00 | 376 | | .Hon | 101 | 7850 | 5851 | 1246 | 2006-07-19 00:00:00 | 377 | | Ben | 96 | 1180 | 1155 | 307 | 2007-03-10 00:00:00 | 378 | | Linda | 89 | 3177 | 2736 | 584 | 2005-08-07 00:00:00 | 379 | | Christina | 85 | 158 | 34 | 842 | 2012-10-08 00:00:00 | 380 | | Jessica | 84 | 2161 | 2091 | 220 | 2009-01-12 00:00:00 | 381 | | Greg | 81 | 820 | 753 | 408 | 2008-02-16 00:00:00 | 382 | | Nieves | 80 | 1091 | 774 | 178 | 2013-07-08 00:00:00 | 383 | | Sui | 78 | 9 | 18 | 754 | 2009-09-07 00:00:00 | 384 | | Yuri | 76 | 1166 | 220 | 1339 | 2008-01-03 00:00:00 | 385 | | Nicole | 73 | 13 | 10 | 161 | 2009-04-30 00:00:00 | 386 | +-----------+------+--------+--------+--------------+---------------------+ 387 | 388 | 389 | Please explain your findings and interpretation of the results: 390 | 391 | Yes, but there does seem to be one major outlier, number three Harald. The 392 | other users seem to have a correlation with more `useful` and `funny` 393 | results in more fans, but also in conjunction with review_count and time 394 | they have been yelping. 395 | 396 | 397 | 398 | Part 2: Inferences and Analysis 399 | 400 | 1. Pick one city and category of your choice and group the businesses in that city 401 | or category by their overall star rating. Compare the businesses with 2-3 stars to 402 | the businesses with 4-5 stars and answer the following questions. Include your code. 403 | 404 | 405 | i. Do the two groups you chose to analyze have a different distribution of hours? 406 | 407 | The 4-5 star group seems to have shorter hours then the 2-3 star group. 408 | Please note the query returned only three businesses so not a great 409 | sample size. 410 | 411 | ii. Do the two groups you chose to analyze have a different number of reviews? 412 | 413 | Yes and no, one of the 4-5 star group has a lot more reviews but then the other 414 | 4-5 star group has close to the same number of reviews as the 2-3 star group 415 | 416 | iii. Are you able to infer anything from the location data provided between these two 417 | groups? Explain. 418 | 419 | No, every business is in a different zip-code. 420 | 421 | SQL code used for analysis: 422 | 423 | SELECT B.name, 424 | B.review_count, 425 | H.hours, 426 | postal_code, 427 | CASE 428 | WHEN hours LIKE "%monday%" THEN 1 429 | WHEN hours LIKE "%tuesday%" THEN 2 430 | WHEN hours LIKE "%wednesday%" THEN 3 431 | WHEN hours LIKE "%thursday%" THEN 4 432 | WHEN hours LIKE "%friday%" THEN 5 433 | WHEN hours LIKE "%saturday%" THEN 6 434 | WHEN hours LIKE "%sunday%" THEN 7 435 | END AS ord, 436 | CASE 437 | WHEN B.stars BETWEEN 2 AND 3 THEN '2-3 stars' 438 | WHEN B.stars BETWEEN 4 AND 5 THEN '4-5 stars' 439 | END AS star_rating 440 | FROM business B INNER JOIN hours H 441 | ON B.id = H.business_id 442 | INNER JOIN category C 443 | ON C.business_id = B.id 444 | WHERE (B.city == 'Las Vegas' 445 | AND 446 | C.category LIKE 'shopping') 447 | AND 448 | (B.stars BETWEEN 2 AND 3 449 | OR 450 | B.stars BETWEEN 4 AND 5) 451 | GROUP BY stars,ord 452 | ORDER BY ord,star_rating ASC 453 | 454 | 455 | 456 | 2. Group business based on the ones that are open and the ones that are closed. What 457 | differences can you find between the ones that are still open and the ones that are 458 | closed? List at least two differences and the SQL code you used to arrive at your 459 | answer. 460 | 461 | i. Difference 1: 462 | 463 | The businesses that are open tend to have more reviews than ones that 464 | are closed on average. 465 | 466 | Open: AVG(review_count) = 31.757 467 | Closed: AVG(review_count0 = 23.198 468 | 469 | 470 | ii. Difference 2: 471 | 472 | The average star rating is higher for businesses that are open than 473 | businesses that are closed. 474 | 475 | Open: AVG(stars) = 3.679 476 | Closed: AVG(stars) = 3.520 477 | 478 | 479 | SQL code used for analysis: 480 | 481 | SELECT COUNT(DISTINCT(id)), 482 | AVG(review_count), 483 | SUM(review_count), 484 | AVG(stars), 485 | is_open 486 | FROM business 487 | GROUP BY is_open 488 | 489 | 490 | 491 | 3. For this last part of your analysis, you are going to choose the type of analysis you 492 | want to conduct on the Yelp dataset and are going to prepare the data for analysis. 493 | 494 | Ideas for analysis include: Parsing out keywords and business attributes for sentiment 495 | analysis, clustering businesses to find commonalities or anomalies between them, 496 | predicting the overall star rating for a business, predicting the number of fans a 497 | user will have, and so on. These are just a few examples to get you started, so feel 498 | free to be creative and come up with your own problem you want to solve. Provide 499 | answers, in-line, to all of the following: 500 | 501 | i. Indicate the type of analysis you chose to do: 502 | 503 | Predicting whether a business will stay open or close. We wish not to explicitly 504 | examine the text of the reviews, but this would be an interesting analysis. 505 | 506 | ii. Write 1-2 brief paragraphs on the type of data you will need for your analysis 507 | and why you chose that data: 508 | 509 | To better help businesses understand the importance of different factors which 510 | will help their business stay open. Some data that may be important; number of 511 | reviews, star rating of business, hours open, and of course location location 512 | location. We will gather the latitude and longitude as well as city, state, 513 | postal_code, and address to make processing easier later on. Categories and 514 | attributes will be used to better distinguish between different types of 515 | businesses. `is_open` will determine which business is open and which business 516 | have closed (not hours) but permanently. 517 | 518 | iii. Output of your finished dataset: 519 | 520 | +------------------------+--------------------------------+-----------------------------+---------------+-------+-------------+----------+-----------+--------------+-------+--------------+---------------+-----------------+----------------+--------------+----------------+--------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+---------+ 521 | | id | name | address | city | state | postal_code | latitude | longitude | review_count | stars | monday_hours | tuesday_hours | wednesday_hours | thursday_hours | friday_hours | saturday_hours | sunday_hours | categories | attributes | is_open | 522 | +------------------------+--------------------------------+-----------------------------+---------------+-------+-------------+----------+-----------+--------------+-------+--------------+---------------+-----------------+----------------+--------------+----------------+--------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+---------+ 523 | | -0DET7VdEQOJVJ_v6klEug | Flaming Kitchen | 3235 York Regional Road 7 | Markham | ON | L3R 3P9 | 43.8484 | -79.3487 | 25 | 3.0 | 12:00-23:00 | 12:00-23:00 | 12:00-23:00 | 12:00-23:00 | 12:00-23:00 | 12:00-23:00 | 12:00-23:00 | Asian Fusion,Restaurants | RestaurantsTableService,GoodForMeal,Alcohol,Caters,HasTV,RestaurantsGoodForGroups,NoiseLevel,WiFi,RestaurantsAttire,RestaurantsReservations,OutdoorSeating,RestaurantsPriceRange2,BikeParking,RestaurantsDelivery,Ambience,RestaurantsTakeOut,GoodForKids,BusinessParking | 1 | 524 | | -2HjuT4yjLZ3b5f_abD87Q | Freeman's Car Stereo | 4821 South Blvd | Charlotte | NC | 28217 | 35.1727 | -80.8755 | 8 | 3.5 | 9:00-19:00 | 9:00-19:00 | 9:00-19:00 | 9:00-19:00 | 9:00-19:00 | 9:00-17:00 | None | Electronics,Shopping,Automotive,Car Stereo Installation | BusinessAcceptsCreditCards,RestaurantsPriceRange2,BusinessParking,WheelchairAccessible | 1 | 525 | | -CdstAUdEvci8GeJG8owpQ | Motors & More | 2315 Highland Dr | Las Vegas | NV | 89102 | 36.1465 | -115.167 | 7 | 5.0 | 7:00-17:00 | 7:00-17:00 | 7:00-17:00 | 7:00-17:00 | 7:00-17:00 | 8:00-12:00 | None | Home Services,Solar Installation,Heating & Air Conditioning/HVAC | BusinessAcceptsCreditCards,BusinessAcceptsBitcoin,ByAppointmentOnly | 1 | 526 | | -K4gAv8_vjx8-2BxkVeRkA | Baby Cakes | 4145 Erie St | Willoughby | OH | 44094 | 41.6399 | -81.4064 | 5 | 3.5 | None | 11:00-17:00 | 11:00-17:00 | 11:00-20:00 | 11:00-17:00 | 10:00-17:00 | None | Bakeries,Food | BusinessAcceptsCreditCards,RestaurantsTakeOut,WheelchairAccessible,RestaurantsDelivery | 1 | 527 | | -PtTGvWsckUL8tTutHr6Ew | Snip-its Rocky River | 21609 Center Ridge Rd | Rocky River | OH | 44116 | 41.4595 | -81.8587 | 18 | 2.5 | 10:00-19:00 | 10:00-19:00 | 10:00-19:00 | 10:00-19:00 | 10:00-19:00 | 9:00-17:30 | 10:00-16:00 | Beauty & Spas,Hair Salons | BusinessAcceptsCreditCards,RestaurantsPriceRange2,GoodForKids,BusinessParking,ByAppointmentOnly | 1 | 528 | | -ayZoW_iNDsunYXX_0x1YQ | Standard Restaurant Supply | 2922 E McDowell Rd | Phoenix | AZ | 85008 | 33.4664 | -112.018 | 15 | 3.5 | 8:00-18:00 | 8:00-18:00 | 8:00-18:00 | 8:00-18:00 | 8:00-18:00 | 9:00-17:00 | None | Shopping,Wholesalers,Restaurant Supplies,Professional Services,Wholesale Stores | BusinessAcceptsCreditCards,RestaurantsPriceRange2,BusinessParking,BikeParking,WheelchairAccessible | 1 | 529 | | -d9qyfNhLMQwVVg_raBKeg | What A Bagel | 973 Eglinton Avenue W | York | ON | M6C 2C4 | 43.6999 | -79.4295 | 8 | 3.0 | 6:00-15:30 | 6:00-15:30 | 6:00-15:30 | 6:00-15:30 | 6:00-15:30 | 6:00-15:30 | None | Restaurants,Bagels,Breakfast & Brunch,Food | NoiseLevel,RestaurantsAttire,RestaurantsTableService,OutdoorSeating | 1 | 530 | | -hjbcaxaU9yYXY2iI-49sw | Pinnacle Fencing Solutions | | Phoenix | AZ | 85060 | 33.4805 | -111.997 | 13 | 4.0 | 8:00-16:00 | 8:00-16:00 | 8:00-16:00 | 8:00-16:00 | 8:00-16:00 | None | None | Home Services,Contractors,Fences & Gates | BusinessAcceptsCreditCards,ByAppointmentOnly | 1 | 531 | | -iu4FxdfxN4rU4Fu9BjiFw | Alterations Express | 17240 Royalton Rd | Strongsville | OH | 44136 | 41.3141 | -81.8207 | 3 | 4.0 | 8:00-19:00 | 8:00-19:00 | 8:00-19:00 | 8:00-19:00 | 8:00-19:00 | 8:00-18:00 | None | Shopping,Bridal,Dry Cleaning & Laundry,Local Services,Sewing & Alterations | BusinessParking,BusinessAcceptsCreditCards,RestaurantsPriceRange2,BusinessAcceptsBitcoin,BikeParking,ByAppointmentOnly,WheelchairAccessible | 1 | 532 | | -j4NsiRzSMrMk2N_bGH_SA | Extra Space Storage | 2880 W Elliot Rd | Chandler | AZ | 85224 | 33.3496 | -111.892 | 5 | 4.0 | 8:00-17:30 | 8:00-17:30 | 8:00-17:30 | 8:00-17:30 | 8:00-17:30 | 8:00-17:30 | 10:00-14:00 | Home Services,Self Storage,Movers,Shopping,Local Services,Home Decor,Home & Garden | BusinessAcceptsCreditCards | 1 | 533 | | -uiBBVWI6tMDm2JFbZFrOw | Gussied Up | 1090 Bathurst St | Toronto | ON | M5R 1W5 | 43.6727 | -79.4142 | 6 | 4.5 | None | 11:00-19:00 | 11:00-19:00 | 11:00-19:00 | 11:00-19:00 | 11:00-17:00 | 12:00-16:00 | Women's Clothing,Shopping,Fashion | BusinessAcceptsCreditCards,RestaurantsPriceRange2,BusinessParking,BikeParking | 1 | 534 | | 0-aPEeNc2zVb5Gp-i7Ckqg | Buddy's Muffler & Exhaust | 1509 Hickory Grove Rd | Gastonia | NC | 28056 | 35.2772 | -81.06 | 4 | 5.0 | 8:30-17:00 | 8:30-17:00 | 8:30-17:00 | 8:30-17:00 | 8:30-17:00 | 9:00-15:00 | None | Automotive,Auto Repair | BusinessAcceptsCreditCards | 1 | 535 | | 01xXe2m_z048W5gcBFpoJA | Five Guys | 2641 N 44th St, Ste 100 | Phoenix | AZ | 85008 | 33.478 | -111.986 | 63 | 3.5 | 10:00-22:00 | 10:00-22:00 | 10:00-22:00 | 10:00-22:00 | 10:00-22:00 | 10:00-22:00 | 10:00-22:00 | American (New),Burgers,Fast Food,Restaurants | RestaurantsTableService,GoodForMeal,Alcohol,Caters,HasTV,RestaurantsGoodForGroups,NoiseLevel,WiFi,RestaurantsAttire,RestaurantsReservations,OutdoorSeating,BusinessAcceptsCreditCards,RestaurantsPriceRange2,BikeParking,RestaurantsDelivery,Ambience,RestaurantsTakeOut,GoodForKids,DriveThru,BusinessParking | 1 | 536 | | 06I2r8S3tHP_LwGnnkk6Uw | All Storage - Anthem | 2620 W Horizon Ridge Pkwy | Henderson | NV | 89052 | 36.0021 | -115.102 | 3 | 3.5 | 9:00-16:30 | 9:00-16:30 | 9:00-16:30 | 9:00-16:30 | 9:00-16:30 | 9:00-16:30 | None | Truck Rental,Local Services,Self Storage,Parking,Automotive | BusinessAcceptsCreditCards,BusinessAcceptsBitcoin | 1 | 537 | | 07h3mGtTovPJE660nX6E-A | Mood | 1 Greenside Place | Edinburgh | EDH | EH1 3AA | 55.957 | -3.18502 | 11 | 2.0 | None | None | None | 22:30-3:00 | 22:00-3:00 | 22:00-3:00 | 22:30-3:00 | Dance Clubs,Nightlife | Alcohol,OutdoorSeating,BusinessAcceptsCreditCards,RestaurantsPriceRange2,AgesAllowed,Music,Smoking,RestaurantsGoodForGroups,WheelchairAccessible | 0 | 538 | | 0AJF-USLN6K5T4caooDdjw | Starbucks | 4605 E Chandler Blvd, Ste A | Phoenix | AZ | 85048 | 33.3044 | -111.984 | 52 | 3.0 | 5:00-20:00 | 5:00-20:00 | 5:00-20:00 | 5:00-20:30 | 5:00-20:00 | 5:00-20:00 | 5:00-20:00 | Coffee & Tea,Food | BusinessParking,Caters,WiFi,OutdoorSeating,BusinessAcceptsCreditCards,RestaurantsPriceRange2,BikeParking,RestaurantsTakeOut | 1 | 539 | | 0B3W6KxkD3o4W4l6cq735w | Big Smoke Burger | 260 Yonge Street | Toronto | ON | M4B 2L9 | 43.6546 | -79.3805 | 47 | 3.0 | 10:30-21:00 | 10:30-21:00 | 10:30-21:00 | 10:30-21:00 | 10:30-21:00 | 10:30-21:00 | 11:00-19:00 | Poutineries,Burgers,Restaurants | RestaurantsTableService,GoodForMeal,Alcohol,Caters,HasTV,RestaurantsGoodForGroups,NoiseLevel,WiFi,RestaurantsAttire,RestaurantsReservations,OutdoorSeating,BusinessAcceptsCreditCards,RestaurantsPriceRange2,WheelchairAccessible,BikeParking,RestaurantsDelivery,Ambience,RestaurantsTakeOut,GoodForKids,DriveThru,BusinessParking | 1 | 540 | | 0IySwcfqwJjpHPsYwjpAkg | Subway | 2904 Yorkmont Rd | Charlotte | NC | 28208 | 35.1903 | -80.9288 | 7 | 3.5 | 6:00-22:00 | 6:00-22:00 | 6:00-22:00 | 6:00-22:00 | 6:00-22:00 | 10:00-21:00 | None | Fast Food,Restaurants,Sandwiches | Ambience,RestaurantsPriceRange2,GoodForKids | 1 | 541 | | 0K2rKvqdBmiOAUTebcUohQ | Red Rock Canyon Visitor Center | 1000 Scenic Loop Dr | Las Vegas | NV | 89161 | 36.1357 | -115.428 | 32 | 4.5 | 8:00-16:30 | 8:00-16:30 | 8:00-16:30 | 8:00-16:30 | 8:00-16:30 | 8:00-16:30 | 8:00-16:30 | Education,Visitor Centers,Professional Services,Special Education,Local Services,Community Service/Non-Profit,Hotels & Travel,Travel Services,Gift Shops,Shopping,Parks,Hiking,Flowers & Gifts,Active Life | BusinessAcceptsCreditCards,GoodForKids | 1 | 542 | | 0Ni7Stqt4RFWDGjOYRi2Bw | Scent From Above Company | 2501 W Behrend Dr, Ste 67 | Scottsdale | AZ | 85027 | 33.6656 | -112.111 | 14 | 4.5 | 6:00-16:00 | 6:00-16:00 | 6:00-16:00 | 6:00-16:00 | 6:00-16:00 | None | None | Home Cleaning,Local Services,Professional Services,Carpet Cleaning,Home Services,Office Cleaning,Window Washing | BusinessAcceptsCreditCards,ByAppointmentOnly | 1 | 543 | | 0WBMEfqXQnEOAIkV-uCW6w | The Charlotte Room | 19 Charlotte Street | Toronto | ON | M5V 2H5 | 43.6466 | -79.3938 | 10 | 3.5 | 15:00-1:00 | 15:00-1:00 | 15:00-1:00 | 15:00-1:00 | 15:00-2:00 | 18:00-2:00 | None | Event Planning & Services,Bars,Nightlife,Lounges,Pool Halls,Venues & Event Spaces | BusinessParking,HasTV,CoatCheck,NoiseLevel,OutdoorSeating,BusinessAcceptsCreditCards,RestaurantsPriceRange2,Music,WheelchairAccessible,Smoking,Ambience,BestNights,RestaurantsGoodForGroups,HappyHour,GoodForDancing,Alcohol | 0 | 544 | | 0Y3lHyqRHfWOBuQlS1bM0g | PC Savants | 11966 W Candelaria Ct | Sun City | AZ | 85373 | 33.6901 | -112.319 | 11 | 5.0 | 10:00-19:00 | 10:00-19:00 | 10:00-19:00 | 10:00-19:00 | 10:00-19:00 | 11:00-18:00 | 11:00-18:00 | IT Services & Computer Repair,Electronics Repair,Local Services,Mobile Phone Repair | BusinessAcceptsCreditCards,BusinessAcceptsBitcoin | 1 | 545 | | 0aKsGxx7XP2TMs_fn_9xVw | Sweet Ruby Jane Confections | 8975 S Eastern Ave, Ste 3-B | Las Vegas | NV | 89123 | 36.015 | -115.118 | 30 | 4.0 | 10:00-19:00 | 10:00-19:00 | 10:00-19:00 | 10:00-19:00 | 10:00-19:00 | 10:00-19:00 | None | Food,Chocolatiers & Shops,Bakeries,Specialty Food,Desserts | BusinessAcceptsCreditCards,RestaurantsPriceRange2,BusinessParking,WheelchairAccessible | 0 | 546 | | 0cxO1Lx2Pi7u6ftWX3Wksg | Oinky's Pork Chop Heaven | 22483 Emery Rd | North Randall | OH | 44128 | 41.4352 | -81.5214 | 3 | 3.0 | 6:00-23:00 | 6:00-23:00 | 6:00-23:00 | 6:00-23:00 | 6:00-23:00 | 6:00-23:00 | 6:00-23:00 | Soul Food,Restaurants | RestaurantsAttire,RestaurantsGoodForGroups,GoodForKids,RestaurantsReservations,RestaurantsTakeOut | 1 | 547 | | 0e-j5VcEn54EZT-FKCUZdw | Sushi Osaka | 5084 Dundas Street W | Toronto | ON | M9A 1C2 | 43.6452 | -79.5324 | 8 | 4.5 | 11:00-23:00 | 11:00-23:00 | 11:00-23:00 | 11:00-23:00 | 11:00-23:00 | 11:00-23:00 | 14:00-23:00 | Sushi Bars,Restaurants,Japanese,Korean | RestaurantsTakeOut,WiFi,RestaurantsGoodForGroups,RestaurantsReservations | 1 | 548 | +------------------------+--------------------------------+-----------------------------+---------------+-------+-------------+----------+-----------+--------------+-------+--------------+---------------+-----------------+----------------+--------------+----------------+--------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+---------+ 549 | 550 | iv. Provide the SQL code you used to create your final dataset: 551 | 552 | SELECT B.id, 553 | B.name, 554 | B.address, 555 | B.city, 556 | B.state, 557 | B.postal_code, 558 | B.latitude, 559 | B.longitude, 560 | B.review_count, 561 | B.stars, 562 | MAX(CASE 563 | WHEN H.hours LIKE "%monday%" THEN TRIM(H.hours,'%MondayTuesWednesThursFriSatSun|%') 564 | END) AS monday_hours, 565 | MAX(CASE 566 | WHEN H.hours LIKE "%tuesday%" THEN TRIM(H.hours,'%MondayTuesWednesThursFriSatSun|%') 567 | END) AS tuesday_hours, 568 | MAX(CASE 569 | WHEN H.hours LIKE "%wednesday%" THEN TRIM(H.hours,'%MondayTuesWednesThursFriSatSun|%') 570 | END) AS wednesday_hours, 571 | MAX(CASE 572 | WHEN H.hours LIKE "%thursday%" THEN TRIM(H.hours,'%MondayTuesWednesThursFriSatSun|%') 573 | END) AS thursday_hours, 574 | MAX(CASE 575 | WHEN H.hours LIKE "%friday%" THEN TRIM(H.hours,'%MondayTuesWednesThursFriSatSun|%') 576 | END) AS friday_hours, 577 | MAX(CASE 578 | WHEN H.hours LIKE "%saturday%" THEN TRIM(H.hours,'%MondayTuesWednesThursFriSatSun|%') 579 | END) AS saturday_hours, 580 | MAX(CASE 581 | WHEN H.hours LIKE "%sunday%" THEN TRIM(H.hours,'%MondayTuesWednesThursFriSatSun|%') 582 | END) AS sunday_hours, 583 | GROUP_CONCAT(DISTINCT(C.category)) AS categories, 584 | GROUP_CONCAT(DISTINCT(A.name)) AS attributes, 585 | B.is_open 586 | FROM business B 587 | INNER JOIN hours H 588 | ON B.id = H.business_id 589 | INNER JOIN category C 590 | ON B.id = C.business_id 591 | INNER JOIN attribute A 592 | ON B.id = A.business_id 593 | GROUP BY B.id -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # SQL-for-Data-Science 2 | Coursera - University of California, Davis 3 | -------------------------------------------------------------------------------- /SQL_data_science_quiz_1.sql: -------------------------------------------------------------------------------- 1 | --Q1) Retrieve all the records from the Employees table. 2 | 3 | SELECT * FROM Employees 4 | 5 | ------------------------------------------------------------------------------------ 6 | 7 | --Q2) Retrieve the FirstName, LastName, Birthdate, Address, City, and State from 8 | -- the Employees table. 9 | 10 | SELECT FirstName, LastName, Birthdate, Address, City, State 11 | FROM Employees 12 | WHERE BirthDate = '1965-03-03 00:00:00' 13 | 14 | ------------------------------------------------------------------------------------ 15 | 16 | --Q3) Retrieve all the columns from the Tracks table, but only return 20 rows. 17 | 18 | select * from Tracks 19 | limit 20 -------------------------------------------------------------------------------- /SQL_data_science_quiz_2.sql: -------------------------------------------------------------------------------- 1 | --Q1) Find all the tracks that have a length of 5,000,000 milliseconds or more. 2 | 3 | SELECT COUNT(TrackId) 4 | FROM TRACKS 5 | WHERE Milliseconds >= 5000000 6 | 7 | ------------------------------------------------------------------------------------ 8 | 9 | --Q2) Find all the invoices whose total is between $5 and $15 dollars. 10 | 11 | SELECT InvoiceID,Total 12 | FROM Invoices 13 | WHERE Total > 5 AND Total < 15 14 | 15 | ------------------------------------------------------------------------------------ 16 | 17 | --Q3) Find all the customers from the following States: RJ, DF, AB, BC, CA, WA, NY. 18 | 19 | SELECT FirstName, LastName, Company, State 20 | FROM Customers 21 | WHERE State IN ('RJ','DF','AB','BC','CA','WA','NY') 22 | 23 | ------------------------------------------------------------------------------------ 24 | 25 | --Q4) Find all the invoices for customer 56 and 58 where the total was between 26 | -- $1.00 and $5.00. 27 | 28 | SELECT CustomerId, InvoiceId, Total, InvoiceDate 29 | FROM Invoices 30 | WHERE CustomerID IN (56,58) AND 31 | Total BETWEEN 1 AND 5 32 | 33 | ------------------------------------------------------------------------------------ 34 | 35 | --Q5) Find all the tracks whose name starts with 'All'. 36 | 37 | SELECT TrackId, Name 38 | FROM Tracks 39 | WHERE Name LIKE 'All%' 40 | 41 | ------------------------------------------------------------------------------------ 42 | 43 | --Q6) Find all the customer emails that start with "J" and are from gmail.com. 44 | 45 | SELECT CustomerId, Email 46 | FROM Customers 47 | WHERE Email LIKE "J%@gmail.com" 48 | 49 | ------------------------------------------------------------------------------------ 50 | 51 | --Q7) Find all the invoices from Brasilia, Edmonton, and Vancouver and sort in 52 | -- descending order by invoice ID. 53 | 54 | SELECT InvoiceId, BillingCity, Total 55 | FROM Invoices 56 | WHERE BillingCity IN ('Brasilia','Edmonton','Vancouver') 57 | ORDER BY InvoiceId DESC 58 | 59 | ------------------------------------------------------------------------------------ 60 | 61 | --Q8) Show the number of orders placed by each customer and sort the result by 62 | -- the number of orders in descending order. 63 | 64 | SELECT CustomerId, COUNT(*) AS Orders 65 | FROM Invoices 66 | GROUP BY CustomerId 67 | ORDER BY Orders DESC 68 | 69 | ------------------------------------------------------------------------------------ 70 | 71 | --Q9) Find the albums with 12 or more tracks. 72 | 73 | SELECT AlbumId, Count(*) AS Ntracks 74 | FROM Tracks 75 | GROUP BY AlbumId 76 | HAVING COUNT (*) >= 12 -------------------------------------------------------------------------------- /SQL_data_science_quiz_3.sql: -------------------------------------------------------------------------------- 1 | --Q1) Using a subquery, find the names of all the tracks for the 2 | -- album "Californication". 3 | 4 | SELECT Name, 5 | AlbumID 6 | FROM Tracks 7 | WHERE AlbumId IN (SELECT AlbumId 8 | FROM Albums 9 | WHERE Title = "Californication"); 10 | 11 | ------------------------------------------------------------------------------------ 12 | -- Q2) Find the total number of invoices for each customer along 13 | -- with the customer's full name, city and email. 14 | 15 | SELECT FirstName, 16 | LastName, 17 | City, 18 | Email, 19 | COUNT(I.CustomerId) AS Invoices 20 | FROM Customers C INNER JOIN Invoices I 21 | ON C.CustomerId = I.CustomerId 22 | GROUP BY C.CustomerId 23 | 24 | ------------------------------------------------------------------------------------ 25 | -- Q3) Retrieve the track name, album, artist, and trackID for 26 | -- all the albums. 27 | 28 | SELECT Tracks.Name, 29 | A.Name AS Artist, 30 | Albums.Title AS Album, 31 | Tracks.TrackId 32 | FROM ((Tracks INNER JOIN Albums 33 | ON Tracks.AlbumId = Albums.AlbumId) 34 | INNER JOIN Artists A 35 | ON A.ArtistId = Albums.ArtistId); 36 | 37 | ------------------------------------------------------------------------------------ 38 | -- Q4) Retrieve a list with the managers last name, and the last 39 | -- name of the employees who report to him or her. 40 | 41 | SELECT M.LastName AS Manager, 42 | E.LastName AS Employee 43 | FROM Employees E INNER JOIN Employees M 44 | ON E.ReportsTo = M.EmployeeID 45 | 46 | ------------------------------------------------------------------------------------ 47 | -- Q5) Find the name and ID of the artists who do not have albums. 48 | 49 | SELECT Name AS Artist, 50 | Artists.ArtistId, 51 | Albums.Title AS Album 52 | FROM Artists 53 | LEFT JOIN Albums 54 | ON Artists.ArtistId = Albums.ArtistId 55 | WHERE Album IS NULL 56 | 57 | ------------------------------------------------------------------------------------ 58 | -- Q6) Use a UNION to create a list of all the employee's & 59 | -- customer's first names and last names ordered by the last 60 | -- name in descending order. 61 | 62 | SELECT FirstName, 63 | LastName 64 | FROM Employees 65 | UNION 66 | SELECT FirstName, 67 | LastName 68 | FROM Customers 69 | ORDER BY LastName DESC 70 | 71 | ------------------------------------------------------------------------------------ 72 | -- Q7) See if there are any customers who have a different city 73 | -- listed in their billing city versus their customer city. 74 | 75 | SELECT C.FirstName, 76 | C.LastName, 77 | C.City AS CustomerCity, 78 | I.BillingCity 79 | FROM Customers C 80 | INNER JOIN Invoices I 81 | ON C.CustomerId = I.CustomerId 82 | WHERE CustomerCity != BillingCity -------------------------------------------------------------------------------- /SQL_data_science_quiz_4.sql: -------------------------------------------------------------------------------- 1 | -- Q1) Pull a list of customer ids with the customer’s full name, and address, 2 | -- along with combining their city and country together. Be sure to make a 3 | -- space in between these two and make it UPPER CASE. 4 | 5 | SELECT CustomerId, 6 | FirstName || " " || LastName AS FullName, 7 | Address, 8 | UPPER(City || " " || Country) AS CityCountry 9 | FROM Customers 10 | 11 | ------------------------------------------------------------------------------------ 12 | -- Q2) Create a new employee user id by combining the first 4 letter of the 13 | -- employee’s first name with the first 2 letters of the employee’s last name. 14 | -- Make the new field lower case and pull each individual step to show your work. 15 | 16 | SELECT FirstName, 17 | LastName, 18 | LOWER(SUBSTR(FirstName,1,4)) AS A, 19 | LOWER(SUBSTR(LastName,1,2)) AS B, 20 | LOWER(SUBSTR(FirstName,1,4)) || LOWER(SUBSTR(LastName,1,2)) AS userId 21 | FROM Employees 22 | 23 | ------------------------------------------------------------------------------------ 24 | -- Q3) Show a list of employees who have worked for the company for 15 or more 25 | -- years using the current date function. Sort by lastname ascending. 26 | 27 | SELECT FirstName, 28 | LastName, 29 | HireDate, 30 | (STRFTIME('%Y', 'now') - STRFTIME('%Y', HireDate)) 31 | - (STRFTIME('%m-%d', 'now') < STRFTIME('%m-%d', HireDate)) 32 | AS YearsWorked 33 | FROM Employees 34 | WHERE YearsWorked >= 15 35 | ORDER BY LastName ASC 36 | 37 | ------------------------------------------------------------------------------------ 38 | -- Q4) Profiling the Customers table, answer the following question. 39 | 40 | SELECT COUNT(*) 41 | FROM Customers 42 | WHERE [some_column] IS NULL 43 | 44 | -- some_column: FirstName, PostalCode, Company, Fax, Phone, Address 45 | -- Answers: Postal Code, Company, Fax, Phone 46 | 47 | ------------------------------------------------------------------------------------ 48 | -- Q5) Find the cities with the most customers and rank in descending order. 49 | 50 | SELECT City, 51 | COUNT(*) 52 | FROM Customers 53 | GROUP BY City 54 | ORDER BY COUNT(*) DESC 55 | 56 | ------------------------------------------------------------------------------------ 57 | -- Q6) Create a new customer invoice id by combining a customer’s invoice id with 58 | -- their first and last name while ordering your query in the following order: 59 | -- firstname, lastname, and invoiceID. 60 | 61 | SELECT C.FirstName, 62 | C.LastName, 63 | I.InvoiceId, 64 | C.FirstName || C.LastName || I.InvoiceID AS NewId 65 | FROM Customers C INNER JOIN Invoices I 66 | ON C.CustomerId = I.CustomerID 67 | WHERE NewId LIKE 'AstridGruber%' -------------------------------------------------------------------------------- /YelpDatabaseSchema.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/trentparkinson/SQL-for-Data-Science/9e6aebaf60b25bf99d8d312244b3e6ab6b4165d7/YelpDatabaseSchema.png --------------------------------------------------------------------------------