SQL练习1-SQLZoo 1-3章错题整理

重点语法

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

教程1 SELECT_names

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

表结构
world (name, continent)
name:国家名称
continent:洲

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.

使用 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])

参数 描述
column_name 必需。要提取字符的字段。
start 必需。规定开始位置(起始值是 1)。
length 可选。要返回的字符数。如果省略,则 MID() 函数返回剩余文本。
  • (2)REPLACE(): 字符串替换函数

语法:REPLACE(original,search,replace)

参数 描述
original 被搜索的字符串。
search 要搜索并被 replace 替换的字符串。如果 search 是空字符串,则按原样返回原始字符串。
replace 该字符串用于替换 search。如果 replace 是空字符串,则删除出现的所有 search。
  • 说明: 用字符串表达式3替换字符串表达式1中出现的所有字符串表达式2的匹配项。返回新的字符串。 如果有某个参数为 NULL,此函数返回 NULL。

2 SELECT from WORLD Tutorial

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

表结构
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的分成一组 排序
得到结果连接起来就是新的排序表.

你可能感兴趣的:(mysql,sql,数据库)