SQL语句学习笔记-sqlzoo

SQLZOO练习笔记

在练习过程中,首次出现的语法记录下来,备忘。练习中基础语句的嵌套使用就不在此记录,多练才是王道~

1. Basic

SELCET name, population FROM world

WHERE name = 'Germany';

WHERE name IN ('Ireland','Iceland');

WHERE area > 1000000;

WHERE area BETWEEN 250000 AND 300000;

# 找出name中以F开头'F%'的词, F结尾'%F', 有F'的%F%', 第二个字母为n的词'_n%'(_表示一个万用字符)

WHERE name LIKE 'F%';

# 找出首都为国名+City,  SQL里面等号为‘=’ 不是‘==’

WHERE capital = concat(name, ' City')

#找出名字由首都+延伸词组成,并显示名字和延伸词, replace(A, a, b) 将A中的a替换成b

SELECT name, replace(capital, name, '')
FROM world
WHERE capital LIKE concat(name, '_%') 

2. SELECT FROM world

# 使用ROUND(population/1000000,2)保留小数点后两位, ROUND(,-3)保留到千位

SELECT name, ROUND(population/1000000,2), ROUND(gdp/1000000000,2) FROM world
WHERE continent = 'South America'

SELECT name, ROUND(gdp/population,-3) FROM world
WHERE gdp > 1000000000000

# CASE WHEN condition1 THEN value1

              WHEN condition2 THEN value2

              ELSE def_value

              END

SELECT name, population
      ,CASE WHEN population<1000000 
            THEN 'small'
            WHEN population<10000000 
            THEN 'medium'
            ELSE 'large'
       END
  FROM bbc

3. SELECT FROM nobel

#type character 'Ü'  mac OS: 'option+U' then  'shift+U'

#需要显示单引号时,需要两个单引号表示 'EUGENE O'NEILL'

SELECT * FROM nobel
WHERE winner = 'EUGENE O''NEILL'

# 将特定的多个项目排在最后 ORDER BY name IN ('A','B'), name   将name为A或B的排在最后,其他的按name排序(name为A或B排1,其他排0,所以在最后)

SELECT winner, subject FROM nobel
WHERE yr = 1984
ORDER BY subject IN ('Chemistry','Physics'), subject, winner

#  将特定的单个项目排在最前或最后,ORDER BY IF语句,ORDER BY IF (name='A', 0, 1), name   如果name为A则排0,其他排1,若后面加DESC,则反排

4. SELECT within SELECT

SELECT name FROM world
  WHERE population >
     (SELECT population FROM world
      WHERE name='Russia')

# 用ALL对一个列表中所有值比较

SELECT name
  FROM world
 WHERE population >= ALL(SELECT population
                           FROM world
                          WHERE population>0)

# 为表格命名,方便不同表格内容比较

SELECT continent, name, area FROM world x
  WHERE area >= ALL
    (SELECT area FROM world y
        WHERE y.continent=x.continent
          AND area>0)

5. SUM and COUNT(COUNT计数, SUM求和, AVG, MAX, DISTINCT去重(若用'DISTINCT name, continent'是对name和continent组合起来去重),GROUP BY分组计算)

# GROUP BY同时条件筛选时WHERE表示GROUP BY之前的条件筛选,HAVING表示分组之后的筛选

# WHERE :以下對於每一個洲份,顯示洲份和至少有1000萬人(10,000,000)口國家的數目

SELECT continent, COUNT(name) FROM world
WHERE population >= 10000000
GROUP BY continent

# HAVING : 列出有至少100百萬(1億)(100,000,000)人口的洲份。

SELECT continent FROM world
GROUP BY continent
HAVING SUM(population) >= 100000000

6. JOIN

SELECT player,stadium
  FROM game JOIN goal ON (game.id=goal.matchid)

# INNER JOIN内联结、FULL JOIN全联结、LEFT JOIN左联结、RIGHT JOIN右联结

SQL语句学习笔记-sqlzoo_第1张图片SQL语句学习笔记-sqlzoo_第2张图片SQL语句学习笔记-sqlzoo_第3张图片SQL语句学习笔记-sqlzoo_第4张图片

# CROSS JOIN交叉联结,返回两表的笛卡尔积

7. More JOIN

是关于JOIN, SELECT IN SELECT, GROUP BY, ORDER BY的嵌套练习

8. Using NULL

# IS NULL选出是NULL的项目

SELECT name FROM teacher
WHERE dept IS NULL

# COALESCE联结合并  coalesce(a, 'b')将a中的NULL值用'b'代替  

# 例子中,将party中是null值的用'None'代替

SELECT name, party
      ,COALESCE(party,'None') AS aff
  FROM msp WHERE name LIKE 'C%'

9. SELF JOIN

# 将同一个表分别命名为a和b, 再将这两个一样的表JOIN,就可以以两个表的形式进行操作了

SELECT a.company, a.num, a.stop, b.stop
FROM route a JOIN route b ON
  (a.company=b.company AND a.num=b.num)
WHERE a.stop=53

# 将两个表分成四个表, 再两两self join

SELECT a.company, a.num FROM route a JOIN route b 
ON (a.company=b.company AND a.num=b.num) 
JOIN stops stopa ON stopa.id=a.stop JOIN stops stopb ON stopb.id=b.stop
WHERE stopa.name='Craiglockhart' AND stopb.name='Tollcross'

# 终极self join 

#Q10:Find the routes involving two buses that can go from Craiglockhart to Sighthill.
Show the bus no. and company for the first bus, the name of the stop for the transfer,
and the bus no. and company for the second bus.

SELECT DISTINCT bus1.num, bus1.company, name, bus2.num, bus2.company 
FROM 
(SELECT a1.num, a1.company, b1.stop FROM route a1 JOIN route b1 
ON a1.num=b1.num AND a1.company=b1.company AND a1.stop != b1.stop 
WHERE a1.stop = (SELECT id FROM stops WHERE name='Craiglockhart')) bus1 
JOIN 
(SELECT a2.num, a2.company, b2.stop FROM route a2 JOIN route b2 
ON a2.num=b2.num AND a2.company=b2.company AND a2.stop != b2.stop 
WHERE a2.stop = (SELECT id FROM stops WHERE name='Sighthill')) bus2 
ON bus1.stop=bus2.stop 
JOIN stops ON bus1.stop = stops.id

 

你可能感兴趣的:(SQL语句学习笔记-sqlzoo)