SQL練習1-SQLZoo 1-3章錯題整理

重點語法

concat, replace, mid, xor, round, left, right, <>sql

教程1 SELECT_names

地址https://sqlzoo.net/wiki/SELEC...express

表結構
world (name, continent)
name:國家名稱
continent:洲api

Q & A
13.找出全部首都和其國家名字,而首都要有國家名字出現。函數

SELECT capital,name
    FROM world
    WHERE capital LIKE concat('%',name,'%')
  • 困難:對concat的語法不熟悉。CONCAT函數能夠將兩個字符串鏈接爲一個字符串,那麼對於2個以上字符呢?
  • 知識點:CONCAT(id, name, work_date),參數能夠是2個以上

15."Monaco-Ville"是合併的國家名字,由 "Monaco" 和延伸詞"-Ville"拼接而來.顯示國家名字及其延伸詞,如首都是國家名字的延伸。 可使用SQL函數REPLACEMID.ui

使用 MID
SELECT name,MID(capital,length(name)+1) as extend
    FROM world
    WHERE capital LIKE concat(name,'_%')
使用 REPLACE
SELECT name, REPLACE(capital,name,'')
    FROM world
    WHERE capital LIKE concat(name,'_%')
  • 知識點:
  • (1)MID(): 從文本字段中提取字符。

語法:MID(column_name,start[,length])spa

參數 描述
column_name 必需。要提取字符的字段。
start 必需。規定開始位置(起始值是 1)。
length 可選。要返回的字符數。若是省略,則 MID() 函數返回剩餘文本。
  • (2)REPLACE(): 字符串替換函數

語法:REPLACE(original,search,replace).net

參數 描述
original 被搜索的字符串。
search 要搜索並被 replace 替換的字符串。若是 search 是空字符串,則按原樣返回原始字符串。
replace 該字符串用於替換 search。若是 replace 是空字符串,則刪除出現的全部 search。
  • 說明: 用字符串表達式3替換字符串表達式1中出現的全部字符串表達式2的匹配項。返回新的字符串。 若是有某個參數爲 NULL,此函數返回 NULL。

2 SELECT from WORLD Tutorial

地址https://sqlzoo.net/wiki/SELEC...code

表結構
world (name, continent, area, population, gdp) 排序

Q & A
8.Exclusive OR (XOR). Show the countries that are big by area (more than 3 million) or big by population (more than 250 million) but not both. Show name, population and area.教程

SELECT name,population,area
    FROM world
    WHERE area>3000000 XOR population>250000000
  • 知識點:XOR 邏輯異或。就是兩個不能同時成立,也不能同時不成立,只成立其中一個條件.

9.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.
For South America show population in millions and GDP in billions both to 2 decimal places.

SELECT name,round(population/1000000,2),ROUND(gdp/1000000000,2)
    FROM world
    WHERE continent ='South America'
  • 知識點:ROUND函數用於把數值字段舍入爲指定的小數位數.
    能夠用於換算單位.

語法:ROUND(column_name,decimals)

參數 描述
column_name 必需。要舍入的字段。
decimals 必需。規定要返回的小數位數。

12.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.
• You can use the function LEFT to isolate the first character.
• You can use <> as the NOT EQUALS operator.

SELECT name,capital
    FROM world
    WHERE  LEFT(name,1) =  LEFT(capital,1) 
    AND name<>capital

知識點:

  • LEFT(str,len) RIGHT(str,len)
    LEFT、RIGHT函數返回 str 最左邊、右邊的 len 個字符串
  • <> : 不等於, 比較運算符, 其功能與!=相同但效率上<>高。

13.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.
Find the country that has all the vowels and no spaces in its name.
• You can use the phrase name NOT LIKE '%a%' to exclude characters from your results.
• The query shown misses countries like Bahamas and Belarus because they contain at least one 'a'.

SELECT name FROM world
    WHERE name LIKE '%a%'
      AND name LIKE '%e%'
      AND name LIKE '%i%'
      AND name LIKE '%o%'
      AND name LIKE '%u%'
      AND name NOT LIKE '% %'

3 SELECT from Nobel Tutorial

地址https://sqlzoo.net/wiki/SELEC...

表結構
nobel (yr, subject, winner)

Q & A
14.The expression subject IN ('Chemistry','Physics') can be used as a value - it will be 0 or 1.
Show the 1984 winners and subject ordered by subject and winner name; but list Chemistry and Physics last.

SELECT winner, subject FROM nobel 
    WHERE yr=1984 
    ORDER BY subject IN ('Physics','Chemistry'),subject ,winner
  • 知識點:排除後兩個表達式,這是一個分組排序,

subject in(xxx)爲0的分紅一組 排序 subject in(xxx)爲1的分紅一組 排序 獲得結果鏈接起來就是新的排序表.

相關文章
相關標籤/搜索