Advertisement
Guest User

Untitled

a guest
Mar 20th, 2019
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.62 KB | None | 0 0
  1. SELECT names-Pattern Matching Strings
  2.  
  3. 1. The % is a wild-card it can match any characters
  4. Q. Find the country that start with Y
  5. A. SELECT name FROM world
  6. WHERE name LIKE 'Y%'
  7. 2.
  8. Q. Find the countries that end with y
  9. A. SELECT name FROM world
  10. WHERE name LIKE '%y'
  11.  
  12. 3. Luxembourg has an x - so does one other country. List them both.
  13. Q.Find the countries that contain the letter x
  14. A.SELECT name FROM world
  15. WHERE name LIKE '%x%'
  16.  
  17. 4. Iceland, Switzerland end with land - but are there others?
  18. Q. Find the countries that end with land
  19. A. SELECT name FROM world
  20. WHERE name LIKE '%land'
  21.  
  22. 5. Columbia starts with a C and ends with ia - there are two more like this.
  23. Q. Find the countries that start with C and end with ia
  24. A. SELECT name FROM world
  25. WHERE name LIKE 'C%ia'
  26.  
  27. 6. Greece has a double e - who has a double o?
  28. Q. Find the country that has oo in the name
  29. A. SELECT name FROM world
  30. WHERE name LIKE '%oo%'
  31.  
  32. 7. Bahamas has three a - who else?
  33. Q. Find the countries that have three or more a in the name
  34. A. SELECT name FROM world
  35. WHERE name LIKE '%a%a%a%'
  36.  
  37. 8. India and Angola have an n as the second character. You can use the underscore as a single character wildcard.
  38. Q. Find the countries that have "t" as the second character.
  39. A. SELECT name FROM world
  40. WHERE name LIKE '_t%'
  41. ORDER BY name
  42.  
  43. 9. Lesotho and Moldova both have two o characters separated by two other characters.
  44. Q. Find the countries that have two "o" characters separated by two others.
  45. A. SELECT name FROM world
  46. WHERE name LIKE '%o__o%'
  47.  
  48. 10. Cuba and Togo have four characters names.
  49. Q. Find the countries that have exactly four characters.
  50. A. SELECT name FROM world
  51. WHERE name LIKE '____'
  52.  
  53. 11. The capital of Luxembourg is Luxembourg. Show all the countries where the capital is the same as the name of the country
  54. Q. Find the country where the name is the capital city.
  55. A. SELECT name
  56. FROM world
  57. WHERE name LIKE capital
  58.  
  59. 12.The capital of Mexico is Mexico City. Show all the countries where the capital has the country together with the word "City".
  60. THE CONCAT FUNCTION
  61. Q. Find the country where the capital is the country plus "City".
  62. A. SELECT name
  63. FROM world
  64. WHERE capital LIKE '%City'
  65.  
  66.  
  67. 13.
  68. Q. Find the capital and the name where the capital includes the name of the country.
  69. A. SELECT capital, name
  70. FROM world
  71. WHERE capital LIKE concat('%', name, '%')
  72.  
  73. 14. You should include Mexico City as it is longer than Mexico. You should not include Luxembourg as the capital is the same as the country.
  74. Q. Find the capital and the name where the capital is an extension of name of the country.
  75. A. SELECT capital, name
  76. FROM world
  77. WHERE capital LIKE concat('%', name, '%') AND
  78. NOT capital LIKE name
  79.  
  80. 15. You can use the SQL function REPLACE.
  81. Q. Show the name and the extension where the capital is an extension of name of the country.
  82. A. SELECT name, REPLACE(capital,name,'')
  83. FROM world
  84. WHERE capital LIKE concat(name, '%') AND
  85. NOT capital LIKE name
  86.  
  87. ## Introduction
  88. 1.Observe the result of running this SQL command to show the name, continent and population of all countries.
  89. Q.
  90. A. SELECT name, continent, population FROM world
  91.  
  92. 2. How to use WHERE to filter records
  93. Q. Show the name for the countries that have a population of at least 200 million. 200 million is 200000000, there are eight zeros.
  94. A. SELECT name FROM world
  95. WHERE population > 200000000
  96.  
  97. 3. PER CAPITA GDP. per capita GDP is the GDP divided by the population GDP/population
  98. Q. Give the name and the per capita GDP for those countries with a population of at least 200 million.
  99. A. SELECT name, gdp/population
  100. FROM world
  101. WHERE population > 200000000
  102.  
  103. 4. In Millions
  104. Q. Show the name and population in millions for the countries of the continent 'South America'. Divide the population by 1000000 to get population in millions.
  105. A. SELECT name, population/1000000
  106. FROM world
  107. WHERE continent = 'South America'
  108.  
  109. 5. Show Name and Population
  110. Q. Show the name and population for France, Germany, Italy
  111. A. SELECT name, population
  112. FROM world
  113. WHERE name IN ('France', 'Germany', 'Italy')
  114.  
  115. 6. Includes a word
  116. Q. Show the countries which have a name that includes the word 'United'
  117. A. SELECT name
  118. FROM world
  119. WHERE name LIKE '%united%'
  120.  
  121. 7. Two ways to be big (OR): Show the countries that are big by area or big by population. Show name, population and area.
  122. Q. Two ways to be big: A country is big if it has an area of more than 3 million sq km or it has a population of more than 250 million.
  123. A. SELECT name, population, area
  124. FROM world
  125. WHERE area > 3000000 OR population > 250000000
  126.  
  127. 8. EXCLUSIVE | One or the other (but not both)
  128. Q. Exclusive OR (XOR). Show the countries that are big by area or big by population but not both. Show name, population and area.
  129. A. SELECT name, population, area
  130. FROM world
  131. WHERE (area > 3000000 AND population < 250000000)
  132. OR (population > 250000000 AND area < 3000000)
  133.  
  134. 9. Rounding | 2 decimal places | Divide by 1000000 (6 zeros) for millions. Divide by 1000000000 (9 zeros) for billions.
  135. Q. Show the name and population in millions and the GDP in billions for the countries of the continent 'South America'. Use the ROUND function to show the values to two decimal places.
  136. A. SELECT name, ROUND(population/1000000, 2), ROUND(gdp/1000000000, 2)
  137. FROM world
  138. WHERE continent = 'South America'
  139.  
  140. 10. Trillion Dollar economics | Show per-capita GDP for the trillion dollar countries to the nearest $1000.
  141. Q. Show the name and per-capita GDP for those countries with a GDP of at least one trillion (1000000000000; that is 12 zeros). Round this value to the nearest 1000.
  142. A. SELECT name, ROUND((gdp/population),-3)
  143. FROM world
  144. WHERE gdp >= 1000000000000
  145.  
  146. 11. LENGTH | Name and capital have the same length
  147. Q. Show the name and capital where the name and the capital have the same number of characters.
  148. A. SELECT name, capital
  149. FROM world
  150. WHERE LENGTH(name) = LENGTH(capital)
  151.  
  152. 12. LEFT | <> Not equals opeartor | Matching name and capital
  153. Q. The capital of Sweden is Stockholm. Both words start with the letter 'S'. Show the name and the capital where the first letters of each match. Don't include countries where the name and the capital are the same word.
  154. A. SELECT name, capital
  155. FROM world
  156. WHERE LEFT(name,1) = LEFT(capital,1)
  157. AND name <> capital
  158.  
  159. 13. name NOT LIKE '%a%' | All the vowels
  160. Q. Find the country that has all the vowels and no spaces in its name. Equatorial Guinea and Dominican Republic have all of the vowels (a e i o u) in the name. They don't count because they have more than one word in the name.
  161. A. SELECT name
  162. FROM world
  163. WHERE name LIKE '%a%' AND name LIKE '%e%' AND name LIKE '%i%' AND name LIKE '%o%' AND name LIKE '%u%' AND name NOT LIKE '% %'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement