Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.73 KB | None | 0 0
  1. --SELECT SUM(open)/COUNT(open) AS avgerage_open
  2. --FROM tutorial.aapl_historical_stock_price
  3. --SELECT MIN(low) AS lowest_stock_price
  4. -- FROM tutorial.aapl_historical_stock_price
  5. --SELECT MAX(close - open) AS highest_increase
  6. --FROM tutorial.aapl_historical_stock_price
  7. --SELECT AVG(volume)
  8. --FROM tutorial.aapl_historical_stock_price
  9. /*SELECT year,
  10. month,
  11. COUNT(volume) AS shares
  12. FROM tutorial.aapl_historical_stock_price
  13. GROUP BY year, month
  14. ORDER BY year, month*/ /*SELECT year, AVG(close-open) AS average_daily_change
  15. FROM tutorial.aapl_historical_stock_price
  16. GROUP BY Year
  17. ORDER BY Year*/ /*SELECT year, month, MIN(low), MAX(high)
  18. FROM tutorial.aapl_historical_stock_price
  19. GROUP BY Year, Month
  20. ORDER BY Year, Month*/ /*SELECT year,
  21. month,
  22. MAX(high) AS month_high
  23. FROM tutorial.aapl_historical_stock_price
  24. GROUP BY year, month
  25. HAVING MAX(high) > 400
  26. ORDER BY year, month*/ /*SELECT DISTINCT year
  27. FROM tutorial.aapl_historical_stock_price
  28. ORDER BY year*/ /*SELECT year,
  29. COUNT(DISTINCT month) AS months_in_year
  30. FROM tutorial.aapl_historical_stock_price
  31. GROUP BY year
  32. ORDER BY year*/ /*SELECT COUNT(DISTINCT year),
  33. COUNT(DISTINCT month)
  34. FROM tutorial.aapl_historical_stock_price*/ /*SELECT player_name, state,
  35. CASE WHEN state = 'CA' THEN 'yes'
  36. ELSE 'no' END AS is_from_california
  37. FROM benn.college_football_players
  38. ORDER BY is_from_california DESC*/ /*SELECT player_name,
  39. height,
  40. CASE WHEN height > 75 THEN 'over 75'
  41. WHEN height > 70 AND height <= 75 THEN '71-75'
  42. WHEN height > 64 AND height <= 70 THEN '65-70'
  43. ELSE '64 or under' END AS height_group
  44. FROM benn.college_football_players*/ /*SELECT *,
  45. CASE WHEN year = 'SR' OR year = 'JR' THEN player_name
  46. ELSE NULL END AS player_name_if_junior_or_senior
  47. FROM benn.college_football_players*/ --Write a query that counts the number of 300lb+ players for each of the following regions: West Coast (CA, OR, WA), Texas, and Other (Everywhere else).
  48. /*SELECT COUNT(player_name) AS players_over_300lb , CASE WHEN state = 'CA' OR state = 'OR' OR state = 'WA' THEN 'West Coast'
  49. WHEN state = 'TX' THEN 'Texas'
  50. ELSE 'Other' END AS region
  51. FROM benn.college_football_players
  52. WHERE weight >= 300
  53. GROUP BY region*/ --Write a query that calculates the combined weight of all underclass players (FR/SO) in California as well as the combined weight of all upperclass players (JR/SR) in California.
  54. /*SELECT SUM(weight) AS players_combined_weight ,
  55. CASE WHEN year = 'FR' OR year = 'SO' THEN 'underclass players'
  56. WHEN year = 'JR' OR year = 'SR' THEN 'upperclass players'
  57. ELSE NULL END AS player_years
  58. FROM benn.college_football_players
  59. WHERE state = 'CA'
  60. GROUP BY player_years*/ --Write a query that displays the number of players in each state, with FR, SO, JR, and SR players in separate columns and another column for the total
  61. ---number of players. Order results such that states with the most players come first.
  62. /* SELECT state,
  63. COUNT(CASE WHEN year = 'FR' THEN 1 ELSE NULL END) AS fr_count,
  64. COUNT(CASE WHEN year = 'SO' THEN 1 ELSE NULL END) AS so_count,
  65. COUNT(CASE WHEN year = 'JR' THEN 1 ELSE NULL END) AS jr_count,
  66. COUNT(CASE WHEN year = 'SR' THEN 1 ELSE NULL END) AS sr_count,
  67. COUNT(state) AS total_players
  68. FROM benn.college_football_players
  69. GROUP BY state
  70. ORDER BY total_players DESC*/ --Write a query that shows the number of players at schools with names that start with A through M, and the number at schools with names starting with N - Z.
  71. /*SELECT CASE WHEN school_name < 'n' THEN 'A-M'
  72. WHEN school_name >= 'n' THEN 'N-Z'
  73. ELSE NULL END AS school_name_catog,
  74. COUNT(player_name) AS players
  75. FROM benn.college_football_players
  76. GROUP BY school_name_catog*/ --Write a query that selects the school name, player name, position, and weight for every player in Georgia, ordered by weight (heaviest to lightest). Be sure to make an alias for the table, and to reference all column names in relation to the alias.
  77. /*SELECT players.school_name,
  78. players.player_name,
  79. players.position,
  80. players.weight
  81. FROM benn.college_football_players players
  82. WHERE players.state = 'GA'
  83. ORDER BY players.weight DESC*/ /*SELECT teams.conference, AVG(players.weight) AS average_weight
  84. FROM benn.college_football_players players
  85. JOIN benn.college_football_teams teams
  86. ON teams.school_name = players.school_name
  87. GROUP BY teams.conference
  88. ORDER BY average_weight*/
  89.  
  90.  
  91. --Write a query that displays player names, school names and conferences for schools in the "FBS (Division I-A Teams)" division.
  92. /*SELECT players.player_name AS players_player_name,
  93. players.school_name AS players_school_name,
  94. teams.conference AS teams_conference
  95. FROM benn.college_football_players players
  96. JOIN benn.college_football_teams teams
  97. ON teams.school_name = players.school_name
  98. WHERE teams.division = 'FBS (Division I-A Teams)'*/
  99.  
  100. -- SELECT companies.permalink AS companies_permalink,
  101. -- companies.name AS companies_name,
  102. -- acquisitions.company_permalink AS acquisitions_permalink,
  103. -- acquisitions.acquired_at AS acquired_date
  104. -- FROM tutorial.crunchbase_companies companies
  105. -- LEFT JOIN tutorial.crunchbase_acquisitions acquisitions
  106. -- ON companies.permalink = acquisitions.company_permalink
  107.  
  108.  
  109.  
  110. --Write a query that performs an inner join between the tutorial.crunchbase_acquisitions table and the tutorial.crunchbase_companies table, but instead of listing individual rows, count the number of non-null rows in each table.
  111. -- SELECT COUNT(companies.permalink) AS companies_rowcount,
  112. -- COUNT(acquisitions.company_permalink) AS acquisitions_rowcount
  113. -- FROM tutorial.crunchbase_companies companies
  114. -- JOIN tutorial.crunchbase_acquisitions acquisitions
  115. -- ON companies.permalink = acquisitions.company_permalink
  116.  
  117. --Modify the query above to be a LEFT JOIN. Note the difference in results.
  118. -- SELECT COUNT(companies.permalink) AS companies_rowcount,
  119. -- COUNT(acquisitions.company_permalink) AS acquisitions_rowcount
  120. -- FROM tutorial.crunchbase_companies companies
  121. -- LEFT JOIN tutorial.crunchbase_acquisitions acquisitions
  122. -- ON companies.permalink = acquisitions.company_permalink
  123.  
  124.  
  125. --Modify the query above to be a RIGHT JOIN. Note the difference in results.
  126. -- SELECT COUNT(companies.permalink) AS companies_rowcount,
  127. -- COUNT(acquisitions.company_permalink) AS acquisitions_rowcount
  128. -- FROM tutorial.crunchbase_companies companies
  129. -- RIGHT JOIN tutorial.crunchbase_acquisitions acquisitions
  130. -- ON companies.permalink = acquisitions.company_permalink
  131.  
  132.  
  133. --Count the number of unique companies (don't double-count companies) and unique acquired companies by state. Do not include results for which there is no state data, and order by the number of acquired companies from highest to lowest.
  134. -- SELECT companies.state_code, COUNT(DISTINCT companies.permalink) AS company_num,
  135. -- COUNT(DISTINCT acquisitions.company_permalink) as acquired_num
  136. -- FROM tutorial.crunchbase_companies companies
  137. -- LEFT JOIN tutorial.crunchbase_acquisitions acquisitions
  138. -- ON companies.permalink = acquisitions.company_permalink
  139. -- WHERE companies.state_code IS NOT NULL
  140. -- GROUP BY companies.state_code
  141. -- ORDER BY acquired_num DESC
  142.  
  143.  
  144.  
  145. -- Rewrite the previous practice query in which you counted total and acquired companies by state, but with a RIGHT JOIN instead of a LEFT JOIN. The goal is to produce the exact same results.
  146. -- SELECT companies.state_code, COUNT(DISTINCT companies.permalink) AS company_num,
  147. -- COUNT(DISTINCT acquisitions.company_permalink) as acquired_num
  148. -- FROM tutorial.crunchbase_acquisitions acquisitions
  149. -- RIGHT JOIN tutorial.crunchbase_companies companies
  150. -- ON acquisitions.company_permalink = companies.permalink
  151. -- WHERE companies.state_code IS NOT NULL
  152. -- GROUP BY companies.state_code
  153. -- ORDER BY acquired_num DESC
  154.  
  155.  
  156.  
  157. --Write a query that shows a company's name, "status" (found in the Companies table), and the number of unique investors in that company. Order by the number of investors from most to fewest. Limit to only companies in the state of New York.
  158. -- SELECT t1.company_name, t2.status,
  159. -- COUNT(DISTINCT t1.investor_name) AS investors_num
  160. -- FROM tutorial.crunchbase_investments t1
  161. -- LEFT JOIN tutorial.crunchbase_companies t2
  162. -- ON t1.company_permalink = t2.permalink
  163. -- WHERE t1.company_state_code = 'NY'
  164. -- GROUP BY t1.company_name, t2.status
  165. -- ORDER BY investors_num DESC
  166.  
  167.  
  168.  
  169. --Write a query that lists investors based on the number of companies in which they are invested. Include a row for companies with no investor, and order from most companies to least.
  170. -- SELECT CASE WHEN t2.investor_name IS NULL THEN 'No Investors'
  171. -- ELSE t2.investor_name END AS investor,
  172. -- COUNT(DISTINCT t1.permalink) AS companies_invested_in
  173. -- FROM tutorial.crunchbase_companies t1
  174. -- LEFT JOIN tutorial.crunchbase_investments t2
  175. -- ON t1.permalink = t2.company_permalink
  176. -- GROUP BY investor
  177. -- ORDER BY companies_invested_in DESC
  178.  
  179.  
  180.  
  181. -- SELECT COUNT(CASE WHEN companies.permalink IS NOT NULL AND acquisitions.company_permalink IS NULL
  182. -- THEN companies.permalink ELSE NULL END) AS companies_only,
  183. -- COUNT(CASE WHEN companies.permalink IS NOT NULL AND acquisitions.company_permalink IS NOT NULL
  184. -- THEN companies.permalink ELSE NULL END) AS both_tables,
  185. -- COUNT(CASE WHEN companies.permalink IS NULL AND acquisitions.company_permalink IS NOT NULL
  186. -- THEN acquisitions.company_permalink ELSE NULL END) AS acquisitions_only
  187. -- FROM tutorial.crunchbase_companies companies
  188. -- FULL JOIN tutorial.crunchbase_acquisitions acquisitions
  189. -- ON companies.permalink = acquisitions.company_permalink
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196. --Write a query that joins tutorial.crunchbase_companies and tutorial.crunchbase_investments_part1 using a FULL JOIN. Count up the number of rows that are matched/unmatched as in the example above.
  197. -- SELECT COUNT(CASE WHEN companies.permalink IS NOT NULL AND invest_part_1.company_permalink IS NULL
  198. -- THEN companies.permalink ELSE NULL END) AS companies_only,
  199. -- COUNT(CASE WHEN companies.permalink IS NOT NULL AND invest_part_1.company_permalink IS NOT NULL
  200. -- THEN companies.permalink ELSE NULL END) AS both_tables,
  201. -- COUNT(CASE WHEN companies.permalink IS NULL AND invest_part_1.company_permalink IS NOT NULL
  202. -- THEN invest_part_1.company_permalink ELSE NULL END) AS invest_part_1_only
  203. -- FROM tutorial.crunchbase_companies companies
  204. -- FULL JOIN tutorial.crunchbase_investments_part1 invest_part_1
  205. -- ON companies.permalink = invest_part_1.company_permalink
  206.  
  207.  
  208. --UNION EXAMPLE
  209. -- SELECT *
  210. -- FROM tutorial.crunchbase_investments_part1
  211. -- UNION
  212. -- SELECT *
  213. -- FROM tutorial.crunchbase_investments_part2
  214.  
  215.  
  216.  
  217. -- Write a query that appends the two crunchbase_investments datasets above (including duplicate values). Filter the first dataset to only companies with names that start with the letter "T", and filter the second to companies with names starting with "M" (both not case-sensitive). Only include the company_permalink, company_name, and investor_name columns.
  218. -- SELECT company_permalink, company_name, investor_name
  219. -- FROM tutorial.crunchbase_investments_part1
  220. -- WHERE company_name ILIKE 'T%'
  221. -- UNION ALL
  222. -- SELECT company_permalink, company_name, investor_name
  223. -- FROM tutorial.crunchbase_investments_part2
  224. -- WHERE company_name ILIKE 'M%'
  225.  
  226.  
  227.  
  228. -- Write a query that shows 3 columns. The first indicates which dataset (part 1 or 2) the data comes from, the second shows company status, and the third is a count of the number of investors.
  229.  
  230. -- Hint: you will have to use the tutorial.crunchbase_companies table as well as the investments tables. And you'll want to group by status and dataset.
  231. -- SELECT 'investments_part1' AS dataset_part,
  232. -- companies.status,
  233. -- COUNT(DISTINCT investments.investor_permalink) AS investors
  234. -- FROM tutorial.crunchbase_companies companies
  235. -- LEFT JOIN tutorial.crunchbase_investments_part1 investments
  236. -- ON companies.permalink = investments.company_permalink
  237. -- GROUP BY dataset_part, status
  238. -- UNION ALL
  239. -- SELECT 'investments_part2' AS dataset_part,
  240. -- companies.status,
  241. -- COUNT(DISTINCT investments.investor_permalink) AS investors
  242. -- FROM tutorial.crunchbase_companies companies
  243. -- LEFT JOIN tutorial.crunchbase_investments_part2 investments
  244. -- ON companies.permalink = investments.company_permalink
  245. -- GROUP BY dataset_part, status
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement