Advertisement
avisrivastava254084

Untitled

Sep 16th, 2019
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 36.54 KB | None | 0 0
  1. # Running docker container and restoring database
  2. 1. docker pull mcr.microsoft.com/mssql/server:2017-latest
  3. 2. docker run -e 'ACCEPT_EULA=Y' -e 'MSSQL_SA_PASSWORD=<YourStrong!Passw0rd>' \
  4. --name 'sql1' -p 1401:1433 \
  5. -v sql1data:/var/opt/mssql \
  6. -d mcr.microsoft.com/mssql/server:2017-latest
  7. 3. docker exec -it sql1 mkdir /var/opt/mssql/backup
  8. 4. docker cp us_national_statistics.bak sql1:/var/opt/mssql/backup
  9. 5. docker exec -it sql1 "bash"
  10. 6. /opt/mssql-tools/bin/sqlcmd -S localhost -U SA [The password is: <YourStrong!Passw0rd>]
  11. 7. RESTORE DATABASE US_NATIONAL_COPY FROM DISK = "/var/opt/mssql/backup/us_national_statistics.bak" WITH REPLACE, RECOVERY, MOVE "us_national_statistics" TO "/var/opt/mssql/data/usns_copy.mdf", MOVE "us_national_statistics_log" TO "/var/opt/mssql/data/usns_copy.ldf";
  12.  
  13. # Interacting in SQL
  14. ## List all databases
  15. ```
  16. SELECT name from master.sys.databases
  17. GO
  18. ```
  19. ```
  20. name
  21. --------------------------------------------------------------------------------------------------------------------------------
  22. master
  23. tempdb
  24. model
  25. msdb
  26. US_NATIONAL_COPY
  27.  
  28. (5 rows affected)
  29. ```
  30.  
  31. ## Switch to US_NATIONAL_COPY database
  32. ```
  33. USE US_NATIONAL_COPY
  34. GO
  35. ```
  36. ```
  37. Changed database context to 'US_NATIONAL_COPY'.
  38. ```
  39.  
  40. ## List all the base tables in this database
  41. ```
  42. SELECT TABLE_NAME FROM US_NATIONAL_COPY.INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE'
  43. GO
  44. ```
  45. ```
  46. TABLE_NAME
  47. --------------------------------------------------------------------------------------------------------------------------------
  48. education_codes
  49. employment_categories
  50. household_income
  51. energy_census_and_economic_data_us_2010_2014
  52. person_economic_info
  53. states
  54.  
  55. (6 rows affected)
  56. ```
  57.  
  58. ## List all column names from person_economic_info
  59. ```
  60. SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME='person_economic_info'
  61. GO
  62. ```
  63. ```
  64. COLUMN_NAME
  65. --------------------------------------------------------------------------------------------------------------------------------
  66. age
  67. marital_status
  68. address_state
  69. income
  70. income_category
  71. car_price
  72. car_price_category
  73. education
  74. years_employed
  75. retired
  76. employment_category
  77. gender
  78. length_at_current_residence
  79. wireless
  80. multiple_lines
  81. voice_mail
  82. pager
  83. internet
  84. caller_id
  85. call_waiting
  86. own_tv
  87. own_dvd_player
  88. own_4k_tv
  89. own_smartphone
  90. own_computer
  91. own_fax
  92. read_newspapers
  93.  
  94. (27 rows affected)
  95. ```
  96.  
  97. ## Creating View: annotated_person_info
  98. I am going to have state_number and state_name in this view only. I could not understand the complete question as to why do we need employment categoy in this.
  99. ```
  100. CREATE VIEW annotated_person_info AS SELECT numeric_id, us_state_terr from states ;
  101. GO
  102. ```
  103. ```
  104. 1> select * from annotated_person_info
  105. 2> GO
  106. numeric_id us_state_terr
  107. ----------- --------------------------------------------------
  108. 0 Not Reported
  109. 1 Alabama
  110. 2 Alaska
  111. 3 Arizona
  112. 4 Arkansas
  113. 5 California
  114. 6 Colorado
  115. 7 Connecticut
  116. 8 Delaware
  117. 9 Florida
  118. 10 Georgia
  119. 11 Hawaii
  120. 12 Idaho
  121. 13 Illinois
  122. 14 Indiana
  123. 15 Iowa
  124. 16 Kansas
  125. 17 Kentucky
  126. 18 Louisiana
  127. 19 Maine
  128. 20 Maryland
  129. 21 Massachusetts
  130. 22 Michigan
  131. 23 Minnesota
  132. 24 Mississippi
  133. 25 Missouri
  134. 26 Montana
  135. 27 Nebraska
  136. 28 Nevada
  137. 29 New Hampshire
  138. 30 New Jersey
  139. 31 New Mexico
  140. 32 New York
  141. 33 North Carolina
  142. 34 North Dakota
  143. 35 Ohio
  144. 36 Oklahoma
  145. 37 Oregon
  146. 38 Pennsylvania
  147. 39 Rhode Island
  148. 40 South Carolina
  149. 41 South Dakota
  150. 42 Tennessee
  151. 43 Texas
  152. 44 Utah
  153. 45 Vermont
  154. 46 Virginia
  155. 47 Washington
  156. 48 West Virginia
  157. 49 Wisconsin
  158. 50 Wyoming
  159. 51 American Samoa
  160. 52 District of Columbia
  161. 53 Federated States of Micronesia
  162. 54 Guam
  163. 55 Marshall Islands
  164. 56 Northern Mariana Islands
  165. 57 Palau
  166. 58 Puerto Rico
  167. 59 Virgin Islands
  168.  
  169. (60 rows affected)
  170. ```
  171.  
  172. ## For each state, give the following summary information (result should have one row per state). Use the annotated_person_info view to includes the name of the state from the states table
  173. ```
  174. Number of people reported
  175. Number of 4k televisions
  176. Number of smartphones
  177. Highest income
  178. Average income
  179. Percentage of respondents that are male
  180. ```
  181. First, I am going to join the two tables on the basis of state_id:
  182. ```
  183. select p.address_state state_code, a.us_state_terr from person_economic_info p inner join annotated_person_info a on p.address_state = a.numeric_id
  184. ```
  185. ```
  186. 40 South Carolina
  187. 0 Not Reported
  188. 13 Illinois
  189. 13 Illinois
  190. 25 Missouri
  191. 12 Idaho
  192. 22 Michigan
  193. 21 Massachusetts
  194. 3 Arizona
  195. 3 Arizona
  196. 4 Arkansas
  197. ```
  198. Now, I have to select the average income and the state name.
  199.  
  200. ```
  201. select avg(p.income) average_income, p.address_state state_code, a.us_state_terr state_name from person_economic_info p inner join annotated_person_info a on p.address_state = a.numeric_id group by p.address_state, a.us_state_terr
  202. GO
  203. ```
  204. ```
  205. average_income state_code state_name
  206. -------------- ----------- --------------------------------------------------
  207. 50934 0 Not Reported
  208. 92300 23 Minnesota
  209. 130800 46 Virginia
  210. 124157 29 New Hampshire
  211. 64412 9 Florida
  212. 70611 15 Iowa
  213. 52695 3 Arizona
  214. 379500 52 District of Columbia
  215. 110550 32 New York
  216. 99961 26 Montana
  217. 68013 12 Idaho
  218. 80931 35 Ohio
  219. 55961 6 Colorado
  220. 24500 43 Texas
  221. 19000 49 Wisconsin
  222. 72682 21 Massachusetts
  223. 108750 27 Nebraska
  224. 144041 38 Pennsylvania
  225. 61725 7 Connecticut
  226. 98250 44 Utah
  227. 50630 1 Alabama
  228. 13000 50 Wyoming
  229. 90896 24 Mississippi
  230. 122500 47 Washington
  231. 85459 18 Louisiana
  232. 123039 30 New Jersey
  233. 70571 10 Georgia
  234. 54461 41 South Dakota
  235. 57660 4 Arkansas
  236. 76218 19 Maine
  237. 119376 25 Missouri
  238. 85000 36 Oklahoma
  239. 75777 13 Illinois
  240. 81500 42 Tennessee
  241. 61735 5 California
  242. 106790 22 Michigan
  243. 105972 33 North Carolina
  244. 93400 39 Rhode Island
  245. 64792 16 Kansas
  246. 136000 45 Vermont
  247. 48132 2 Alaska
  248. 79333 48 West Virginia
  249. 90469 17 Kentucky
  250. 139339 31 New Mexico
  251. 107883 34 North Dakota
  252. 85368 40 South Carolina
  253. 60650 11 Hawaii
  254. 86782 20 Maryland
  255. 119888 28 Nevada
  256. 79475 14 Indiana
  257. 94703 37 Oregon
  258. 63089 8 Delaware
  259. 79000 51 American Samoa
  260.  
  261. (53 rows affected)
  262. ```
  263.  
  264. Next step would be to get the number of people reported:
  265. In the next step, I can go for max(income) to get the highest income per state using group by.
  266. ```
  267. select avg(p.income) average_income, max(p.income) highest_income, p.address_state state_code, a.us_state_terr state_name from person_economic_info p inner join annotated_person_info a on p.address_state = a.numeric_id group by p.address_state, a.us_state_terr
  268. 2> GO
  269. ```
  270. ```
  271. average_income highest_income state_code state_name
  272. -------------- -------------- ----------- --------------------------------------------------
  273. 50934 725000 0 Not Reported
  274. 92300 743000 23 Minnesota
  275. 130800 309000 46 Virginia
  276. 124157 496000 29 New Hampshire
  277. 64412 525000 9 Florida
  278. 70611 674000 15 Iowa
  279. 52695 759000 3 Arizona
  280. 379500 592000 52 District of Columbia
  281. 110550 665000 32 New York
  282. 99961 530000 26 Montana
  283. 68013 437000 12 Idaho
  284. 80931 243000 35 Ohio
  285. 55961 406000 6 Colorado
  286. 24500 40000 43 Texas
  287. 19000 24000 49 Wisconsin
  288. 72682 333000 21 Massachusetts
  289. 108750 476000 27 Nebraska
  290. 144041 489000 38 Pennsylvania
  291. 61725 368000 7 Connecticut
  292. 98250 419000 44 Utah
  293. 50630 359000 1 Alabama
  294. 13000 19000 50 Wyoming
  295. 90896 320000 24 Mississippi
  296. 122500 463000 47 Washington
  297. 85459 788000 18 Louisiana
  298. 123039 613000 30 New Jersey
  299. 70571 873000 10 Georgia
  300. 54461 238000 41 South Dakota
  301. 57660 707000 4 Arkansas
  302. 76218 441000 19 Maine
  303. 119376 884000 25 Missouri
  304. 85000 252000 36 Oklahoma
  305. 75777 1070000 13 Illinois
  306. 81500 467000 42 Tennessee
  307. 61735 718000 5 California
  308. 106790 706000 22 Michigan
  309. 105972 481000 33 North Carolina
  310. 93400 345000 39 Rhode Island
  311. 64792 297000 16 Kansas
  312. 136000 434000 45 Vermont
  313. 48132 362000 2 Alaska
  314. 79333 126000 48 West Virginia
  315. 90469 837000 17 Kentucky
  316. 139339 568000 31 New Mexico
  317. 107883 382000 34 North Dakota
  318. 85368 462000 40 South Carolina
  319. 60650 426000 11 Hawaii
  320. 86782 374000 20 Maryland
  321. 119888 504000 28 Nevada
  322. 79475 976000 14 Indiana
  323. 94703 382000 37 Oregon
  324. 63089 529000 8 Delaware
  325. 79000 190000 51 American Samoa
  326.  
  327. (53 rows affected)
  328. ```
  329. Now, for "Number of people reported", I will have to have the count per state. So, I am going to use the COUNT function.
  330. In order to get the count of number of people per state:
  331. ```
  332. 1> select count(*) number_of_people_reported, address_state state_code from person_economic_info group by person_economic_info.address_state
  333. 2> GO
  334. number_of_people_reported state_code
  335. ------------------------- -----------
  336. 455 0
  337. 120 23
  338. 5 46
  339. 57 29
  340. 211 9
  341. 175 15
  342. 365 3
  343. 2 52
  344. 40 32
  345. 77 26
  346. 224 12
  347. 29 35
  348. 285 6
  349. 6 43
  350. 3 49
  351. 126 21
  352. 64 27
  353. 24 38
  354. 255 7
  355. 8 44
  356. 395 1
  357. 3 50
  358. 87 24
  359. 6 47
  360. 137 18
  361. 51 30
  362. 257 10
  363. 13 41
  364. 324 4
  365. 128 19
  366. 69 25
  367. 16 36
  368. 207 13
  369. 10 42
  370. 340 5
  371. 110 22
  372. 37 33
  373. 15 39
  374. 135 16
  375. 4 45
  376. 392 2
  377. 3 48
  378. 166 17
  379. 53 31
  380. 43 34
  381. 19 40
  382. 209 11
  383. 115 20
  384. 45 28
  385. 183 14
  386. 27 37
  387. 267 8
  388. 3 51
  389.  
  390. (53 rows affected)
  391. ```
  392.  
  393. Now. combining this query with the previous query:
  394. ```
  395. select count(*) number_of_people, avg(p.income) average_income, max(p.income) highest_income, p.address_state state_code, a.us_state_terr state_name from person_economic_info p inner join annotated_person_info a on p.address_state = a.numeric_id group by p.address_state, a.us_state_terr
  396. GO
  397. ```
  398. ```
  399. number_of_people average_income highest_income state_code state_name
  400. ---------------- -------------- -------------- ----------- --------------------------------------------------
  401. 455 50934 725000 0 Not Reported
  402. 120 92300 743000 23 Minnesota
  403. 5 130800 309000 46 Virginia
  404. 57 124157 496000 29 New Hampshire
  405. 211 64412 525000 9 Florida
  406. 175 70611 674000 15 Iowa
  407. 365 52695 759000 3 Arizona
  408. 2 379500 592000 52 District of Columbia
  409. 40 110550 665000 32 New York
  410. 77 99961 530000 26 Montana
  411. 224 68013 437000 12 Idaho
  412. 29 80931 243000 35 Ohio
  413. 285 55961 406000 6 Colorado
  414. 6 24500 40000 43 Texas
  415. 3 19000 24000 49 Wisconsin
  416. 126 72682 333000 21 Massachusetts
  417. 64 108750 476000 27 Nebraska
  418. 24 144041 489000 38 Pennsylvania
  419. 255 61725 368000 7 Connecticut
  420. 8 98250 419000 44 Utah
  421. 395 50630 359000 1 Alabama
  422. 3 13000 19000 50 Wyoming
  423. 87 90896 320000 24 Mississippi
  424. 6 122500 463000 47 Washington
  425. 137 85459 788000 18 Louisiana
  426. 51 123039 613000 30 New Jersey
  427. 257 70571 873000 10 Georgia
  428. 13 54461 238000 41 South Dakota
  429. 324 57660 707000 4 Arkansas
  430. 128 76218 441000 19 Maine
  431. 69 119376 884000 25 Missouri
  432. 16 85000 252000 36 Oklahoma
  433. 207 75777 1070000 13 Illinois
  434. 10 81500 467000 42 Tennessee
  435. 340 61735 718000 5 California
  436. 110 106790 706000 22 Michigan
  437. 37 105972 481000 33 North Carolina
  438. 15 93400 345000 39 Rhode Island
  439. 135 64792 297000 16 Kansas
  440. 4 136000 434000 45 Vermont
  441. 392 48132 362000 2 Alaska
  442. 3 79333 126000 48 West Virginia
  443. 166 90469 837000 17 Kentucky
  444. 53 139339 568000 31 New Mexico
  445. 43 107883 382000 34 North Dakota
  446. 19 85368 462000 40 South Carolina
  447. 209 60650 426000 11 Hawaii
  448. 115 86782 374000 20 Maryland
  449. 45 119888 504000 28 Nevada
  450. 183 79475 976000 14 Indiana
  451. 27 94703 382000 37 Oregon
  452. 267 63089 529000 8 Delaware
  453. 3 79000 190000 51 American Samoa
  454.  
  455. (53 rows affected)
  456. ```
  457.  
  458. Now, I need to figure out how to have a condition and then have it's result counted:
  459. ```
  460. 1> select count(*) number_of_4k_tv_owners, address_state state_code from person_economic_info where own_4k_tv=1 group by address_state
  461. 2> GO
  462. number_of_4k_tv_owners state_code
  463. ---------------------- -----------
  464. 423 0
  465. 120 23
  466. 5 46
  467. 57 29
  468. 208 9
  469. 174 15
  470. 342 3
  471. 2 52
  472. 40 32
  473. 77 26
  474. 222 12
  475. 29 35
  476. 277 6
  477. 6 43
  478. 3 49
  479. 125 21
  480. 62 27
  481. 22 38
  482. 249 7
  483. 7 44
  484. 371 1
  485. 2 50
  486. 86 24
  487. 6 47
  488. 137 18
  489. 51 30
  490. 248 10
  491. 13 41
  492. 310 4
  493. 125 19
  494. 69 25
  495. 15 36
  496. 202 13
  497. 9 42
  498. 329 5
  499. 110 22
  500. 36 33
  501. 14 39
  502. 133 16
  503. 4 45
  504. 379 2
  505. 3 48
  506. 164 17
  507. 50 31
  508. 43 34
  509. 19 40
  510. 207 11
  511. 113 20
  512. 44 28
  513. 181 14
  514. 26 37
  515. 259 8
  516. 3 51
  517.  
  518. (53 rows affected)
  519. ```
  520.  
  521. So, the query in the final query formed as of now is:
  522. ```
  523. select count(case when p.own_4k_tv = 1 then 1 end) number_of_4k_tv_owners, count(*) number_of_people, avg(p.income) average_income, max(p.income) highest_income, p.address_state state_code, a.us_state_terr state_name from person_economic_info p inner join annotated_person_info a on p.address_state = a.numeric_id group by p.address_state, a.us_state_terr
  524. GO
  525. ```
  526. ```
  527. number_of_4k_tv_owners number_of_people average_income highest_income state_code state_name
  528. ---------------------- ---------------- -------------- -------------- ----------- --------------------------------------------------
  529. 423 455 50934 725000 0 Not Reported
  530. 120 120 92300 743000 23 Minnesota
  531. 5 5 130800 309000 46 Virginia
  532. 57 57 124157 496000 29 New Hampshire
  533. 208 211 64412 525000 9 Florida
  534. 174 175 70611 674000 15 Iowa
  535. 342 365 52695 759000 3 Arizona
  536. 2 2 379500 592000 52 District of Columbia
  537. 40 40 110550 665000 32 New York
  538. 77 77 99961 530000 26 Montana
  539. 222 224 68013 437000 12 Idaho
  540. 29 29 80931 243000 35 Ohio
  541. 277 285 55961 406000 6 Colorado
  542. 6 6 24500 40000 43 Texas
  543. 3 3 19000 24000 49 Wisconsin
  544. 125 126 72682 333000 21 Massachusetts
  545. 62 64 108750 476000 27 Nebraska
  546. 22 24 144041 489000 38 Pennsylvania
  547. 249 255 61725 368000 7 Connecticut
  548. 7 8 98250 419000 44 Utah
  549. 371 395 50630 359000 1 Alabama
  550. 2 3 13000 19000 50 Wyoming
  551. 86 87 90896 320000 24 Mississippi
  552. 6 6 122500 463000 47 Washington
  553. 137 137 85459 788000 18 Louisiana
  554. 51 51 123039 613000 30 New Jersey
  555. 248 257 70571 873000 10 Georgia
  556. 13 13 54461 238000 41 South Dakota
  557. 310 324 57660 707000 4 Arkansas
  558. 125 128 76218 441000 19 Maine
  559. 69 69 119376 884000 25 Missouri
  560. 15 16 85000 252000 36 Oklahoma
  561. 202 207 75777 1070000 13 Illinois
  562. 9 10 81500 467000 42 Tennessee
  563. 329 340 61735 718000 5 California
  564. 110 110 106790 706000 22 Michigan
  565. 36 37 105972 481000 33 North Carolina
  566. 14 15 93400 345000 39 Rhode Island
  567. 133 135 64792 297000 16 Kansas
  568. 4 4 136000 434000 45 Vermont
  569. 379 392 48132 362000 2 Alaska
  570. 3 3 79333 126000 48 West Virginia
  571. 164 166 90469 837000 17 Kentucky
  572. 50 53 139339 568000 31 New Mexico
  573. 43 43 107883 382000 34 North Dakota
  574. 19 19 85368 462000 40 South Carolina
  575. 207 209 60650 426000 11 Hawaii
  576. 113 115 86782 374000 20 Maryland
  577. 44 45 119888 504000 28 Nevada
  578. 181 183 79475 976000 14 Indiana
  579. 26 27 94703 382000 37 Oregon
  580. 259 267 63089 529000 8 Delaware
  581. 3 3 79000 190000 51 American Samoa
  582.  
  583. (53 rows affected)
  584. ```
  585. Ref: [Stackoverflow answer](https://stackoverflow.com/a/7319527/10834788)
  586.  
  587. Now, I will do the same count method for smartphone owners:
  588. ```
  589. select count(case when p.own_smartphone = 1 then 1 end) number_of_smartphone_owners, count(case when p.own_4k_tv = 1 then 1 end) number_of_4k_tv_owners, count(*) number_of_people, avg(p.income) average_income, max(p.income) highest_income, p.address_state state_code, a.us_state_terr state_name from person_economic_info p inner join annotated_person_info a on p.address_state = a.numeric_id group by p.address_state, a.us_state_terr
  590. GO
  591. ```
  592.  
  593. Now, for "Percentage of respondents that are male", I need to apply the maths formula in sql.
  594. First going with the basic sql query of calculating percentage:
  595. ```
  596. select address_state, (count(case when p.gender = 'm' then 1 end) * 100.0 / count(*)) male_percentage, (count(case when p.gender = 'f' then 1 end) * 100.0 / count(*)) female_percentage from person_economic_info p group by p.address_state
  597. ```
  598. Now, I will put this in the final query formed:
  599. ```
  600. select (count(case when p.gender = 'm' then 1 end) * 100.0 / count(*)) male_percentage, count(case when p.own_smartphone = 1 then 1 end) number_of_smartphone_owners, count(case when p.own_4k_tv = 1 then 1 end) number_of_4k_tv_owners, count(*) number_of_people, avg(p.income) average_income, max(p.income) highest_income, p.address_state state_code, a.us_state_terr state_name from person_economic_info p inner join annotated_person_info a on p.address_state = a.numeric_id group by p.address_state, a.us_state_terr
  601. GO
  602. ```
  603. ```
  604. 2> GO
  605. male_percentage number_of_smartphone_owners number_of_4k_tv_owners number_of_people average_income highest_income state_code state_name
  606. ---------------------------- --------------------------- ---------------------- ---------------- -------------- -------------- ----------- --------------------------------------------------
  607. 51.208791208791 87 423 455 50934 725000 0 Not Reported
  608. 52.500000000000 30 120 120 92300 743000 23 Minnesota
  609. 60.000000000000 1 5 5 130800 309000 46 Virginia
  610. 52.631578947368 11 57 57 124157 496000 29 New Hampshire
  611. 46.919431279620 55 208 211 64412 525000 9 Florida
  612. 54.285714285714 29 174 175 70611 674000 15 Iowa
  613. 47.397260273972 69 342 365 52695 759000 3 Arizona
  614. 50.000000000000 0 2 2 379500 592000 52 District of Columbia
  615. 45.000000000000 6 40 40 110550 665000 32 New York
  616. 48.051948051948 19 77 77 99961 530000 26 Montana
  617. 46.875000000000 38 222 224 68013 437000 12 Idaho
  618. 51.724137931034 5 29 29 80931 243000 35 Ohio
  619. 50.175438596491 69 277 285 55961 406000 6 Colorado
  620. 66.666666666666 2 6 6 24500 40000 43 Texas
  621. 33.333333333333 0 3 3 19000 24000 49 Wisconsin
  622. 49.206349206349 22 125 126 72682 333000 21 Massachusetts
  623. 32.812500000000 21 62 64 108750 476000 27 Nebraska
  624. 50.000000000000 2 22 24 144041 489000 38 Pennsylvania
  625. 49.411764705882 38 249 255 61725 368000 7 Connecticut
  626. 50.000000000000 1 7 8 98250 419000 44 Utah
  627. 46.582278481012 80 371 395 50630 359000 1 Alabama
  628. 66.666666666666 0 2 3 13000 19000 50 Wyoming
  629. 54.022988505747 19 86 87 90896 320000 24 Mississippi
  630. 50.000000000000 1 6 6 122500 463000 47 Washington
  631. 51.094890510948 34 137 137 85459 788000 18 Louisiana
  632. 45.098039215686 12 51 51 123039 613000 30 New Jersey
  633. 52.529182879377 44 248 257 70571 873000 10 Georgia
  634. 53.846153846153 1 13 13 54461 238000 41 South Dakota
  635. 53.395061728395 72 310 324 57660 707000 4 Arkansas
  636. 50.781250000000 32 125 128 76218 441000 19 Maine
  637. 44.927536231884 13 69 69 119376 884000 25 Missouri
  638. 56.250000000000 1 15 16 85000 252000 36 Oklahoma
  639. 57.971014492753 39 202 207 75777 1070000 13 Illinois
  640. 80.000000000000 3 9 10 81500 467000 42 Tennessee
  641. 51.470588235294 63 329 340 61735 718000 5 California
  642. 49.090909090909 22 110 110 106790 706000 22 Michigan
  643. 56.756756756756 9 36 37 105972 481000 33 North Carolina
  644. 40.000000000000 4 14 15 93400 345000 39 Rhode Island
  645. 47.407407407407 34 133 135 64792 297000 16 Kansas
  646. 75.000000000000 0 4 4 136000 434000 45 Vermont
  647. 46.683673469387 75 379 392 48132 362000 2 Alaska
  648. 33.333333333333 2 3 3 79333 126000 48 West Virginia
  649. 52.409638554216 42 164 166 90469 837000 17 Kentucky
  650. 54.716981132075 12 50 53 139339 568000 31 New Mexico
  651. 41.860465116279 8 43 43 107883 382000 34 North Dakota
  652. 57.894736842105 2 19 19 85368 462000 40 South Carolina
  653. 49.760765550239 52 207 209 60650 426000 11 Hawaii
  654. 42.608695652173 29 113 115 86782 374000 20 Maryland
  655. 66.666666666666 9 44 45 119888 504000 28 Nevada
  656. 48.087431693989 38 181 183 79475 976000 14 Indiana
  657. 59.259259259259 6 26 27 94703 382000 37 Oregon
  658. 50.936329588014 60 259 267 63089 529000 8 Delaware
  659. 33.333333333333 0 3 3 79000 190000 51 American Samoa
  660.  
  661. (53 rows affected)
  662. ```
  663.  
  664. ## For each state AND gender, give the same information (result should have two rows per state)
  665. Now, in this case, I need to have two groups - state and gender. Although, I will remove the male percentage because it does not make sense now that we have a proper classification:
  666.  
  667. ```
  668. select p.gender gender, count(case when p.own_smartphone = 1 then 1 end) number_of_smartphone_owners, count(case when p.own_4k_tv = 1 then 1 end) number_of_4k_tv_owners, count(*) number_of_people, avg(p.income) average_income, max(p.income) highest_income, p.address_state state_code, a.us_state_terr state_name from person_economic_info p inner join annotated_person_info a on p.address_state = a.numeric_id group by p.address_state, a.us_state_terr, p.gender order by state_code
  669. ```
  670.  
  671. ## Show each state’s rank for i)People responding ii) Number of smartphones iii)Average income
  672. This will be three different queries.
  673. i) People responding:
  674. ```
  675. select count(*) number_of_people, p.address_state state_code, a.us_state_terr state_name from person_economic_info p inner join annotated_person_info a on p.address_state = a.numeric_id group by p.address_state, a.us_state_terr order by number_of_people desc
  676. ```
  677. ii) Number of smartphones:
  678. ```
  679. select count(case when p.own_smartphone = 1 then 1 end) number_of_smartphone_owners, p.address_state state_code, a.us_state_terr state_name from person_economic_info p inner join annotated_person_info a on p.address_state = a.numeric_id group by p.address_state, a.us_state_terr order by number_of_smartphone_owners desc
  680. ```
  681. iii) Average income
  682. ```
  683. select avg(income) average_income, p.address_state state_code, a.us_state_terr state_name from person_economic_info p inner join annotated_person_info a on p.address_state = a.numeric_id group by p.address_state, a.us_state_terr order by average_income desc
  684. ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement