SQL练习_1 | 0,1,2 | SQLZOO_20191002

本系列刷题笔记主要用以记录刷SQLZOO的过程中的思路、个人答案以及陌生的或者新的知识点。

题目来源 - SQLZOO
SQLZOO中题目中文版本与英文版本略有差异,题目以英文版为准

目录
0 SELECT basics
1 SELECT name
2 SELECT from World

0 SELECT basics(SELECT 基础)

查询表格

查询表格_world

0_1 Show the population of Germany

SELECT population
FROM world
WHERE name = 'Germany'

0_2 Show the name and the population for 'Sweden', 'Norway' and 'Denmark'.

SELECT name,
       population
FROM world
WHERE name IN ('Sweden','Norway','Denmark')

0_3 Show the country and the area for countries with an area between 200,000 and 250,000.

SELECT name,
       area
FROM world
WHERE area BETWEEN 200000 AND 250000

1 SELECT name

查询表格(同Select Basics - World)

1_1 Find the country that start with Y

SELECT name
FROM world
WHERE name LIKE 'Y%'

1_2 Find the countries that end with y

SELECT name
FROM world
WHERE name LIKE '%y'

1_3 Find the countries that contain the letter x

SELECT name
FROM world
WHERE name LIKE '%x%'

1_4 Find the countries that end with land

SELECT name
FROM world
WHERE name LIKE '%land'

1_5 Find the countries that start with C and end with ia

SELECT name
FROM world
WHERE name LIKE 'C%ia'

1_6 Find the country that has oo in the name

SELECT name
FROM world
WHERE name LIKE '%oo%'

1_7 Find the countries that have three or more a in the name

SELECT name
FROM world
WHERE name LIKE '%a%a%a%''

1_8 Find the countries that have "t" as the second character.

SELECT name
FROM world
WHERE name LIKE '_t%''
ORDER BY name

1_9 Find the countries that have two "o" characters separated by two others.

SELECT name
FROM world
WHERE name LIKE '%o__o%''

1_10 Find the countries that have exactly four characters.

SELECT name
FROM world
WHERE name LIKE '%____%''

1_11 Find the country where the name is the capital city.

SELECT name
FROM world
WHERE name = capital

1_12 Find the country where the capital is the country plus "City".

SELECT name
FROM world
WHERE capital = concat(name,' City')

1_13 Find the capital and the name where the capital includes the name of the country.

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

1_14 Find the capital and the name where the capital is an extension of name of the country.

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

1_15 Show the name and the extension where the capital is an extension of name of the country.

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

2 SELECT from World

查询表格(同Select Basics - World)

2_1 Observe the result of running this SQL command to show the name, continent and population of all countries.

SELECT name,
       continent,
       population
FROM world

2_2 Show the name for the countries that have a population of at least 200 million. 200 million is 200000000, there are eight zeros.

SELECT name
FROM world
WHERE population >= 200000000

2_3 Give the name and the per capita GDP for those countries with a population of at least 200 million.

SELECT name,
       gdp / population
FROM world
WHERE population >= 200000000

2_4 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.

SELECT name,
       population / 1000000 AS population
FROM world
WHERE continent = 'South America'

2_5 Show the name and population for France, Germany, Italy

SELECT name,
       population
FROM world
WHERE name IN ('France','Germany','Italy')

2_6 Show the countries which have a name that includes the word 'United'

SELECT name
FROM world
WHERE name LIKE '%United%'

2_7 Show the countries that are big by area or big by population. Show name, population and area.

SELECT name,
       population,
       area
FROM world
WHERE area > 3000000
OR    population > 250000000

2_8 Show the countries that are big by area or big by population but not both. Show name, population and area.

SELECT name,
       population,
       area
FROM world
WHERE area > 3000000
XOR    population > 250000000

2_9 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'

2_10 Show per-capita GDP for the trillion dollar countries to the nearest $1000.

SELECT name,
       ROUND((gdp /(population*1000)))*1000
FROM world
WHERE gdp >= 1000000000000

2_11 Show the name and capital where the name and the capital have the same number of characters.

SELECT name,
       capital
FROM world
WHERE length(name) = length(capital)

2_12 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.

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

2_13 Find the country that has all the vowels and no spaces in its name.

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 '% %'
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 220,884评论 6 513
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 94,212评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 167,351评论 0 360
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 59,412评论 1 294
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 68,438评论 6 397
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 52,127评论 1 308
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,714评论 3 420
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,636评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 46,173评论 1 319
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 38,264评论 3 339
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,402评论 1 352
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 36,073评论 5 347
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,763评论 3 332
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,253评论 0 23
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,382评论 1 271
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,749评论 3 375
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,403评论 2 358

推荐阅读更多精彩内容