Advertisement
Guest User

Untitled

a guest
Jun 19th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQL 4.79 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 millions of US Dollars
  5. --   The value immediately after the problem statement is the expected number of rows that should be returned by the query.
  6.  
  7. -- 1. The name and population of all cities in Ontario, Canada (27 rows)
  8. SELECT *
  9. FROM city
  10. WHERE district = 'Ontario';
  11. -- 2. The name and population of all cities in Montana (1 row)
  12. SELECT city.name, city.population
  13. FROM city
  14. WHERE district = 'Montana';
  15. -- 3. The name, form of government, and head of state of all countries in Europe (46 rows)
  16. SELECT name, governmentform, headofstate
  17. FROM country
  18. WHERE continent = 'Europe';
  19. -- 4. The name, population, surface area, and average life expectancy of all countries in Asia (51 rows)
  20. SELECT name, population, surfacearea, lifeexpectancy
  21. FROM country
  22. WHERE continent = 'Asia';
  23. -- 5. The name, country code, and population of all cities with a population greater than 8 million people (10 rows)
  24. SELECT name, countrycode, population
  25. FROM city
  26. WHERE population > 8000000;
  27. -- 6. The name, country code, and population of all cities with a population less than one thousand people (11 rows)
  28. SELECT name, countrycode, population
  29. FROM city
  30. WHERE population < 1000;
  31. -- 7. The name, continent, and GNP of all countries with a GNP greater than one trillion dollars (6 rows)
  32. SELECT name, continent, GNP
  33. FROM country
  34. WHERE GNP > 1000000;
  35. -- 8. The name, continent, population, GNP, and average life expectancy of all countries with an average life expectancy greater than 80 years (5 rows)
  36. SELECT name, continent, population, GNP, lifeexpectancy
  37. FROM country
  38. WHERE lifeexpectancy > 80;
  39. -- 9. The name and population of all cities in the USA with a population of greater than 1 million people (9 rows)
  40. SELECT name, population
  41. FROM city
  42. WHERE population > 1000000 AND countrycode = 'USA';
  43. -- 10. The name and population of all cities in China with a population of greater than 1 million people (35 rows)
  44. SELECT name, population
  45. FROM city
  46. WHERE countrycode = 'CHN' AND population > 1000000;
  47. -- 11. The name and region of all countries in North or South America (51 rows)
  48. SELECT name, region
  49. FROM country
  50. WHERE continent = 'North America' OR continent = 'South America';
  51. -- 12. The name, continent, and head of state of all countries whose form of government is a monarchy (43 rows)
  52. SELECT name, continent, headofstate
  53. FROM country
  54. WHERE governmentform  LIKE '%monarchy%';
  55. -- 13. The name of all cities in the USA with a population between 1 million and 2 million people (6 rows)
  56. SELECT name
  57. FROM city
  58. WHERE countrycode = 'USA' AND population >= 1000000 AND population <= 2000000;
  59. -- 14. The name and region of all countries in North or South America except for countries in the Caribbean (27 rows)
  60. SELECT name, region
  61. FROM country
  62. WHERE continent IN ('North America', 'South America') AND region != 'Caribbean';
  63. -- 15. The name, population, and GNP of all countries with a GNP greater than $1 trillion dollars and a population of less than 100 million people (4 rows)
  64. SELECT name, population, GNP
  65. FROM country
  66. WHERE GNP > 1000000 AND population < 100000000;
  67.  
  68. -- 16. The name and population of all cities in Texas that have a population of greater than 1 million people (3 rows)
  69. SELECT name, population
  70. FROM city
  71. WHERE district = 'Texas' AND population > 1000000;
  72. -- 17. The name and average life expectancy of all countries on the continent of Oceania (28 rows)
  73. SELECT name, lifeexpectancy
  74. FROM country
  75. WHERE continent = 'Oceania';
  76. -- 18. The name and average life expectancy of all countries on the continent of Oceania for which an average life expectancy has been provided (i.e. not equal to null) (20 rows)
  77. SELECT name, lifeexpectancy
  78. FROM country
  79. WHERE continent = 'Oceania' AND lifeexpectancy IS NOT NULL;
  80. -- 19. The name of all countries on the continent of Oceania for which an average life expectancy has not been provided (i.e. equal to null) (8 rows)
  81. SELECT name
  82. FROM country
  83. WHERE continent = 'Oceania' AND lifeexpectancy IS NULL;
  84. -- 20. The name, continent, GNP, and average life expectancy of all countries that have an average life expectancy of at least 70 years and a GNP between $1 million and $100 million dollars (3 rows)
  85. SELECT name, continent, GNP, lifeexpectancy
  86. FROM country
  87. WHERE lifeexpectancy >= 70 AND GNP >= 1 AND GNP <= 100;
  88. -- 21. The per capita GNP (i.e. GNP divided by population) in US Dollars of all countries in Europe (46 rows)
  89. SELECT name, (GNP / population) AS 'Per Capita GNP'
  90. FROM country
  91. WHERE continent = 'Europe'
  92.  
  93. -- 22. The number of years since independence for all countries that have a year of independence (192 rows)
  94. SELECT name, (2018 - indepyear) AS 'Years Since Independence'
  95. FROM country
  96. WHERE indepyear IS NOT NULL;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement