Advertisement
Guest User

Untitled

a guest
Nov 21st, 2014
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.03 KB | None | 0 0
  1. /* 1.
  2. Using a UNION query, create a report that lists the name and the district of the
  3. cities in Canada (code CAN) and the Netherlands (code NLD). Note that this query
  4. can be written without using a UNION. The purpose of this question is to use UNION.
  5. (city table)*/
  6.  
  7. SELECT name, district, CountryCode
  8. FROM city
  9. WHERE CountryCode = "CAN"
  10. UNION
  11. SELECT name, district, CountryCode
  12. FROM city
  13. WHERE CountryCode = "NLD"
  14. ORDER BY CountryCode, district;
  15.  
  16. /* 2.
  17. Using an INNER JOIN..ON, create a report that list the name of cities, the country
  18. and the indepyear of all countries whose indepYear field is not null. (city and
  19. country tables).*/
  20.  
  21. SELECT cy.name, c.name, c.indepyear
  22. FROM city cy INNER JOIN country c
  23. ON (cy.countrycode = c.Code)
  24. WHERE c.indepyear IS NOT NULL;
  25.  
  26. /* 3.
  27. Rewrite the previous query using WHERE/FROM.*/
  28.  
  29. SELECT cy.name, c.name, c.indepyear
  30. FROM city cy, country c
  31. WHERE cy.countrycode = c.Code
  32. AND c.indepyear IS NOT NULL;
  33.  
  34. /* 4.
  35. Using a subquery, list the name of the city, the countrycode, the population and the
  36. average city population of all cities with a population less than the average population.
  37. (city table)*/
  38.  
  39. SELECT name, countrycode, population, (SELECT AVG(population) FROM city) as "Average Pop"
  40. FROM City
  41. GROUP BY population
  42. HAVING population<(SELECT AVG(population) FROM city);
  43.  
  44. /* 5.
  45. Using INNER JOIN ..ON, write a query that displays, for the city of Toronto, the name
  46. of the city and the head of state of its country. (city and country tables)*/
  47.  
  48. SELECT cy.name, c.headofstate
  49. FROM city cy INNER JOIN country c
  50. ON (cy.countrycode = c.code)
  51. WHERE cy.name = "Toronto";
  52.  
  53. /* 6.
  54. Using INNER JOIN ..ON the three tables, write a query that displays the name of the
  55. city, the city continent, the city head of state, the year of independance and the
  56. percentage of people who speaks English in the city of Toronto. (city, countrylanguage
  57. and country tables)*/
  58.  
  59. SELECT cy.name, c.continent, c.headofstate, c.indepyear, cl.percentage, cl.language
  60. FROM city cy INNER JOIN countrylanguage cl
  61. ON (cy.countrycode = cl.countrycode)
  62. INNER JOIN country c
  63. ON (c.code = cl.countrycode)
  64. WHERE cy.name = "Toronto" AND cl.language = "English";
  65.  
  66. /* 7.
  67. Rewrite the previous query using WHERE/FROM.*/
  68.  
  69. SELECT cy.name, c.continent, c.headofstate, c.indepyear, cl.percentage, cl.language
  70. FROM city cy, countrylanguage cl, country c
  71. WHERE (cy.countrycode = cl.countrycode) AND (c.code = cl.countrycode)
  72. AND cy.name = "Toronto" AND cl.language = "English";
  73.  
  74. /*Querying table city and country.*/
  75.  
  76. /* 8.
  77. Using a subquery, list the name of the city, the countrycode and the population of the
  78. city with the largest population. (city table)*/
  79.  
  80. SELECT name, countrycode, MAX(population)
  81. FROM city;
  82.  
  83. /* 9.
  84. Using a subquery, list the name of the city, the countrycode and the population of all
  85. the cities whose population is larger than the average population. (city table)*/
  86.  
  87. SELECT name, countrycode, population
  88. FROM city
  89. HAVING population>(SELECT AVG(population) FROM city);
  90.  
  91. /* 10.
  92. What is wrong with the following subquery?
  93. SELECT name,countrycode,population,avg(population)
  94. FROM city
  95. WHERE population > (SELECT avg(population) FROM city)*/
  96.  
  97. /*Selecting avg(population) instead of (SELECT avg(population) FROM city) will not return the desired results.
  98. It will do the average of each field.
  99. It also does not have a closing ;*/
  100.  
  101.  
  102. /* 11.
  103. Replace the first avg(population) with (SELECT AVG(population) from city)
  104. What happens? Why?*/
  105.  
  106. SELECT name,countrycode,population,(SELECT avg(population) FROM city)
  107. FROM city
  108. WHERE population > (SELECT avg(population) FROM city);
  109. /*The query now works because it is fetching the average of all city populations*/
  110.  
  111. /* 12.
  112. Using a subquery, list the name of the country, the continent and the life expectancy
  113. of all countries whose life expectancy is less than the average life expectancy.
  114. (country table)*/
  115.  
  116. SELECT name, continent, lifeexpectancy
  117. FROM country
  118. HAVING lifeexpectancy < (SELECT avg(lifeexpectancy) FROM country)
  119. ORDER BY lifeexpectancy;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement