Advertisement
Guest User

Untitled

a guest
Jun 19th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQL 6.53 KB | None | 0 0
  1. -- The following queries utilize the "world" database.
  2. -- Write queries for the following problems.
  3. -- Notes:
  4. --   GNP is expressed in units of one million US Dollars
  5. --   The value immediately after the problem statement is the expected number
  6. --   of rows that should be returned by the query.
  7. SELECT * FROM country;
  8. SELECT * FROM city ORDER BY district;
  9. -- 1. The name and state plus population of all cities in states that border Ohio
  10. -- (i.e. cities located in Pennsylvania, West Virginia, Kentucky, Indiana, and
  11. -- Michigan).  
  12. -- The name and state should be returned as a single column called
  13. -- name_and_state and should contain values such as ‘Detroit, Michigan’.  
  14. -- The results should be ordered alphabetically by state name and then by city
  15. -- name.
  16. -- (19 rows)
  17.  
  18. SELECT (name + ', ' + district) AS 'Name And State', population
  19. FROM city
  20. WHERE district IN ('Pennsylvania', 'West Virginia', 'Kentucky', 'Indiana', 'Michigan')
  21. ORDER BY district ASC, name ASC;
  22.  
  23.  
  24. -- 2. The name, country code, and region of all countries in Africa.  The name and
  25. -- country code should be returned as a single column named country_and_code
  26. -- and should contain values such as ‘Angola (AGO)’
  27. -- (58 rows)
  28.  
  29. SELECT (name +', ' + '(' + code + ')') AS 'Country and Code', region
  30. FROM country
  31. WHERE continent = 'Africa';
  32.  
  33.  
  34. -- 3. The per capita GNP (i.e. GNP multipled by 1000000 then divided by population) of all countries in the
  35. -- world sorted from highest to lowest. Recall: GNP is express in units of one million US Dollars
  36. -- (highest per capita GNP in world: 37459.26)
  37.  
  38. SELECT name, (GNP * 1000000 / population) AS 'Per Capita GNP'
  39. FROM country
  40. WHERE GNP > 0
  41. ORDER BY [Per Capita GNP] DESC;
  42.  
  43. -- 4. The average life expectancy of countries in South America.
  44. -- (average life expectancy in South America: 70.9461)
  45.  
  46. SELECT AVG(lifeexpectancy) AS 'Avg Life Expectancy'
  47. FROM country
  48. WHERE continent = 'South America';
  49.  
  50. -- 5. The sum of the population of all cities in California.
  51. -- (total population of all cities in California: 16716706)
  52.  
  53. SELECT SUM(population) AS 'Population of CA'
  54. FROM city
  55. WHERE district = 'California';
  56.  
  57. -- 6. The sum of the population of all cities in China.
  58. -- (total population of all cities in China: 175953614)
  59.  
  60. SELECT SUM(population) AS 'Cities in China (population)'
  61. FROM city
  62. WHERE countrycode = 'CHN';
  63.  
  64. -- 7. The maximum population of all countries in the world.
  65. -- (largest country population in world: 1277558000)
  66.  
  67. SELECT MAX(population) AS 'Max Population'
  68. FROM country;
  69.  
  70. -- 8. The maximum population of all cities in the world.
  71. -- (largest city population in world: 10500000)
  72. SELECT MAX(population) AS 'Most Populated City'
  73. FROM city;
  74.  
  75. -- 9. The maximum population of all cities in Australia.
  76. -- (largest city population in Australia: 3276207)
  77. SELECT MAX(population) AS 'Most Populated City'
  78. FROM city WHERE countrycode = 'AUS';
  79.  
  80. -- 10. The minimum population of all countries in the world.
  81. -- (smallest_country_population in world: 50)
  82.  
  83. SELECT MIN(population) AS 'Least Populated Country'
  84. FROM country
  85. WHERE population > 0;
  86.  
  87. -- 11. The average population of cities in the United States.
  88. -- (avgerage city population in USA: 286955.3795)
  89. SELECT ROUND(AVG(population), 4) AS avg_city_population
  90. FROM city
  91. WHERE countrycode = 'USA';
  92.  
  93. -- 12. The average population of cities in China.
  94. -- (average city population in China: 484720.6997 approx.)
  95.  
  96. SELECT AVG(population) AS avg_city_population
  97. FROM city
  98. WHERE countrycode = 'USA';
  99.  
  100. -- 13. The surface area of each continent ordered from highest to lowest.
  101. -- (largest continental surface area: 31881000, "Asia")
  102.  
  103. SELECT SUM(surfacearea) AS 'Total Surface Area', continent
  104. FROM country
  105. GROUP BY continent
  106. ORDER BY 'Total Surface Area' DESC;
  107.  
  108.  
  109. -- 14. The highest population density (population divided by surface area) of all
  110. -- countries in the world.
  111. -- (highest population density in world: 26277.7777)
  112.  
  113. SELECT ROUND(MAX(population / surfacearea), 4) AS 'Highest Density'
  114. FROM country;
  115.  
  116.  
  117. -- 15. The population density and life expectancy of the top ten countries with the
  118. -- highest life expectancies in descending order.
  119. -- (highest life expectancies in world: 83.5, 166.6666, "Andorra")
  120.  
  121.  
  122. SELECT TOP 10 lifeexpectancy, (population / surfacearea) AS Density, name
  123. FROM country
  124. WHERE lifeexpectancy IS NOT NULL
  125. ORDER BY lifeexpectancy DESC;
  126.  
  127. -- 16. The difference between the previous and current GNP of all the countries in
  128. -- the world ordered by the absolute value of the difference. Display both
  129. -- difference and absolute difference.
  130. -- (smallest difference: 1.00, 1.00, "Ecuador")
  131.  
  132. SELECT (gnp - gnpold) AS 'Difference' , ABS(gnp - gnpold) AS 'Absolute Difference', name
  133. FROM country
  134. WHERE GNP IS NOT NULL AND GNPOLD IS NOT NULL
  135. ORDER BY [Absolute Difference];
  136.  
  137. -- 17. The average population of cities in each country (hint: use city.countrycode)
  138. -- ordered from highest to lowest.
  139. -- (highest avg population: 4017733.0000, "SGP")
  140.  
  141. SELECT AVG(population) AS 'Average Population', countrycode
  142. FROM city
  143. WHERE population > 0
  144. GROUP BY countrycode
  145. ORDER BY 'Average Population' DESC;
  146.    
  147. -- 18. The count of cities in each state in the USA, ordered by state name.
  148. -- (45 rows)
  149.  
  150. SELECT SUM(population) AS Population, district AS State
  151. FROM city
  152. WHERE countrycode = 'USA'
  153. GROUP BY district
  154. ORDER BY district;
  155.    
  156. -- 19. The count of countries on each continent, ordered from highest to lowest.
  157. -- (highest count: 58, "Africa")
  158.  
  159. SELECT COUNT(name) AS country_count, continent
  160. FROM country
  161. GROUP BY continent
  162. ORDER BY country_count DESC;
  163.    
  164. -- 20. The count of cities in each country ordered from highest to lowest.
  165. -- (largest number of  cities ib a country: 363, "CHN")
  166.  
  167. SELECT COUNT(district) AS city_count, countrycode
  168. FROM city
  169. GROUP BY countrycode
  170. ORDER BY city_count DESC;
  171.    
  172. -- 21. The population of the largest city in each country ordered from highest to
  173. -- lowest.
  174. -- (largest city population in world: 10500000, "IND")
  175.  
  176. SELECT MAX(population) AS 'Largest City Population', countrycode AS Country
  177. FROM city
  178. GROUP BY population, countrycode
  179. ORDER BY population DESC;
  180.  
  181. -- 22. The average, minimum, and maximum non-null life expectancy of each continent
  182. -- ordered from lowest to highest average life expectancy.
  183. -- (lowest average life expectancy: 52.5719, 37.2, 76.8, "Africa")
  184.  
  185. SELECT ROUND(AVG(lifeexpectancy), 4) AS Average, MIN(lifeexpectancy) AS Minimum, MAX(lifeexpectancy) AS Maximum, continent
  186. FROM country
  187. WHERE lifeexpectancy IS NOT NULL
  188. GROUP BY continent
  189. ORDER BY Average ASC;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement