SQLZOO笔记
xampp吧
全部回复
仅看楼主
level 5
多角星 楼主
尝试做sqlzoo上的练习题并记录下来
2021年01月26日 15点01分 1
level 5
多角星 楼主
SELECT basics/zh
這個教程介紹SQL語言。我們會使用SELECT語句。我們會使用WORLD表格
name:國家名稱
continent:洲份
area:面積
population:人口
gdp:國內生產總值
1.
這個例子顯示’France法國’的人口。字串應該在'單引號'中。
修改此例子,以顯示德國 Germany 的人口。
参考答案
SELECT population FROM world
WHERE name = 'Germany'
Per Capita GDP
2.
查詢顯示面積為 5,000,000 以上平方公里的國家,該國家的人口密度(population/area)。人口密度並不是 WORLD 表格中的欄,但我們可用公式(population/area)計算出來。
修改此例子,查詢面積為 5,000,000 以上平方公里的國家,對每個國家顯示她的名字和人均國內生產總值(gdp/population)。
参考答案
SELECT name, gdp/population FROM world
WHERE area > 5000000
Scandinavia
3.
檢查列表:單詞“IN”可以讓我們檢查一個項目是否在列表中。
此示例顯示了“Luxembourg 盧森堡”,“Mauritius 毛里求斯”和“Samoa 薩摩亞”的國家名稱和人口。
顯示“Ireland 愛爾蘭”,“Iceland 冰島”,“Denmark 丹麥”的國家名稱和人口。
参考答案
SELECT name, population FROM world
WHERE name IN ('Ireland', 'Iceland', 'Denmark');
Just the right size
4.
哪些國家是不是太小,又不是太大?
BETWEEN 允許範圍檢查 - 注意,這是包含性的。 此例子顯示面積為 250,000 及 300,000 之間的國家名稱和該國面積。
修改此例子,以顯示面積為 200,000 及 250,000 之間的國家名稱和該國面積。
参考答案
SELECT name, area FROM world
WHERE area BETWEEN 200000 AND 250000
2021年02月01日 15点02分 11
level 5
多角星 楼主
SELECT basics
Introducing the world table of countries
1.
The example uses a WHERE clause to show the population of 'France'. Note that strings (pieces of text that are data) should be in 'single quotes';
Modify it to show the population of German
参考答案
SELECT population FROM world
WHERE name = 'Germany'
Scandinavia
2.
Checking a list The word IN allows us to check if an item is in a list. The example shows the name and population for the countries 'Brazil', 'Russia', 'India' and 'China'.
Show the name and the population for 'Sweden', 'Norway' and 'Denmark'.
参考答案
SELECT name, population FROM world
WHERE name IN ('Sweden', 'Norway', 'Denmark');
Just the right size
3.
Which countries are not too small and not too big? BETWEEN allows range checking (range specified is inclusive of boundary values). The example below shows countries with an area of 250,000-300,000 sq. km. Modify it to 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
2021年02月01日 15点02分 12
level 5
多角星 楼主
SELECT names/zh
name:國家名稱
continent:洲份
Pattern Matching Strings
此教程使用LIKE運算子來檢查國家名字,我們會在world表格中運用SELECT語句:
Summary
1.
你可以用WHERE name LIKE 'B%'來找出以 B 為開首的國家。
%是萬用字元,可以用代表任何字完。
找出以 Y 為開首的國家。
参考答案
SELECT name FROM world
WHERE name LIKE 'Y%'
2.
找出以 Y 為結尾的國家。
参考答案
SELECT name FROM world
WHERE name LIKE '%Y'
3.
“Luxembourg 盧森堡”中有一個x字母,還有一個國家的名字中有x。列出這兩個國家。
找出所有國家,其名字包括字母x。
参考答案
SELECT name FROM world
WHERE name LIKE '%x%'
4.
“Iceland 冰島”和“Switzerland 瑞士”的名字都是以”land”作結束的。還有其他嗎?
找出所有國家,其名字以 land 作結尾。
参考答案
SELECT name FROM world
WHERE name LIKE '%land'
5.
“Columbia 哥倫比亞”是以 C 作開始,ia 作結尾的。還有兩個國家相同。
找出所有國家,其名字以 C 作開始,ia 作結尾。
参考答案
SELECT name FROM world
WHERE name LIKE 'C%ia'
6.
“Greece 希臘”中有雙 e 字。哪個國家有雙 o 字呢?
找出所有國家,其名字包括字母oo。
参考答案
SELECT name FROM world
WHERE name LIKE '%oo%'
7.
“Bahamas 巴哈馬”中有三個 a,還有嗎?
找出所有國家,其名字包括三個或以上的a。
参考答案
SELECT name FROM world
WHERE name LIKE '%a%a%a%'
8.
“India 印度”和”Angola 安哥拉”的第二個字母都是 n。
你可以用底線符_當作單一個字母的萬用字元。
SELECT name FROM world WHERE name LIKE '_n%'ORDER BY name
找出所有國家,其名字以t作第二個字母。
参考答案
SELECT name FROM world
WHERE name LIKE '_t%'
ORDER BY name
9.
“Lesotho 賴索托”和”Moldova 摩爾多瓦”都有兩個字母 o,被另外兩個字母相隔着。
找出所有國家,其名字都有兩個字母 o,被另外兩個字母相隔着。
参考答案
SELECT name FROM world
WHERE name LIKE '%o__o%'
10.
“Cuba古巴”和”Togo 多哥”都是 4 個字母。
找出所有國家,其名字都是 4 個字母的。
参考答案
SELECT name FROM world
WHERE name LIKE '____'
2021年02月01日 15点02分 13
level 5
多角星 楼主
SELECT names/zh
更困難的問題
如你覺得以上題目太容易了,非常好。Well done for getting this far. 下面的題目更困難,更有挑戰性!
11.
“Luxembourg 盧森堡”的首都 capital 都同樣叫“Luxembourg”。
顯示所有國家名字,其首都和國家名字是相同的。
参考答案
SELECT name
FROM world
WHERE name = capital
12.
“Mexico 墨西哥”的首都是”Mexico City”。
顯示所有國家名字,其首都是國家名字加上”City”。
concat函數
参考答案
SELECT name
FROM world
WHERE capital LIKE concat(name,' City')
13.
找出所有首都和其國家名字,而首都要有國家名字中出現。
参考答案
SELECT capital,name
FROM world
WHERE capital LIKE concat('%',name,'%')
14.
找出所有首都和其國家名字,而首都是國家名字的延伸。
你應顯示 Mexico City,因它比其國家名字 Mexico 長。
你不應顯示 Luxembourg,因它的首都和國家名相是相同的
参考答案
SELECT name,capital
FROM world
WHERE capital LIKE concat(name,'_%')
15.
"Monaco-Ville"是合併國家名字 "Monaco" 和延伸詞"-Ville".
顯示國家名字,及其延伸詞,如首都是國家名字的延伸。
你可以使用SQL函數 REPLACE 或 MID.
参考答案
SELECT name,REPLACE(capital, name,'') as ext
FROM world
WHERE capital LIKE concat(name,'_%')
2021年02月01日 15点02分 14
level 5
多角星 楼主
SELECT Quiz/zh
一些關於基本SQL語句的問題
2021年02月01日 15点02分 15
level 5
多角星 楼主
SQLZOO:SELECT from WORLD Tutorial/zh
name:國家名稱
continent:洲份
area:面積
population:人口
gdp:國內生產總值
Country Profile
在這教程中,我們會使用SELECT語句,對World表格進行查詢。
1.
閱讀此表的注意事項 觀察運行一個簡單的SQL命令的結果。
参考答案
SELECT name, continent, population FROM world
2.
如何使用WHERE來篩選記錄。 顯示具有至少2億人口的國家名稱。 2億是200000000,有八個零。
参考答案
SELECT name FROM world
WHERE population > 200000000
3.
找出有至少200百萬(2億)人口的國家名稱,及人均國內生產總值。
求助:如何人均國內生產總值計算
参考答案
SELECT name, gdp/population FROM world
WHERE population>200000000
4.
顯示'South America'南美洲大陸的國家名字和以百萬為單位人口數。 將人口population 除以一百萬(1000000)得可得到以百萬為單位人口數。
参考答案
SELECT name, population/1000000 FROM world
WHERE continent='South America'
5.
顯示法國,德國,意大利(France, Germany, Italy)的國家名稱和人口。
参考答案
SELECT name ,population FROM world
WHERE name IN ('France', 'Germany', 'Italy')
6.
顯示包含單詞“United”為名稱的國家。
参考答案
SELECT name FROM world
WHERE name like '%United%'
7.
成為大國的兩種方式:如果它有3百萬平方公里以上的面積,或擁有250百萬(2.5億)以上人口。
展示大國的名稱,人口和面積。
参考答案
SELECT name, population, area FROM world
WHERE area > 3000000 or population > 250000000
8.
美國、印度和中國(USA, India, China)是人口又大,同時面積又大的國家。排除這些國家。
顯示以人口或面積為大國的國家,但不能同時兩者。顯示國家名稱,人口和面積。
参考答案
SELECT name, population, area FROM world
WHERE area > 3000000 xor population > 250000000
9.
除以為1000000(6個零)是以百萬計。除以1000000000(9個零)是以十億計。使用 ROUND 函數來顯示的數值到小數點後兩位。
對於南美顯示以百萬計人口,以十億計2位小數GDP。百萬和十億除以為1000000(6個零)是以百萬計。除以1000000000(9個零)是以十億計。
参考答案
SELECT name, ROUND(population/1000000,2), ROUND(gdp/1000000000,2) FROM world
WHERE continent='South America'
10.
顯示國家有至少一個萬億元國內生產總值(萬億,也就是12個零)的人均國內生產總值。四捨五入這個值到最接近1000。
顯示萬億元國家的人均國內生產總值,四捨五入到最近的$ 1000。
参考答案
SELECT name, ROUND(gdp/population,-3) FROM world
WHERE gdp > 1000000000000
2021年02月01日 15点02分 16
level 5
多角星 楼主
SQLZOO:SELECT from WORLD Tutorial/zh
困難的題目
11.
The CASE statement shown is used to substitute North America for Caribbean in the third column.
Show the name - but substitute Australasia for Oceania - for countries beginning with N.
参考答案
SELECT name,
CASE WHEN continent='Oceania' THEN 'Australasia'
ELSE continent END
FROM world
WHERE name LIKE 'N%'
12.Show the name and the continent - but substitute Eurasia for Europe and Asia; substitute America - for each country in North America or South America or Caribbean. Show countries beginning with A or B
参考答案
SELECT name,
CASE WHEN continent in ('Europe','Asia') THEN 'Eurasia'
WHEN continent in ('North America','South America','Caribbean') THEN 'America'
ELSE continent END
FROM world
WHERE name LIKE 'A%' or name LIKE 'B%'
13.
Put the continents right...
Oceania becomes Australasia
Countries in Eurasia and Turkey go to Europe/Asia
Caribbean islands starting with 'B' go to North America, other Caribbean islands go to South America
Show the name, the original continent and the new continent of all countries.
参考答案
SELECT name, continent,
CASE WHEN continent = 'Oceania' THEN 'Australasia'
WHEN continent in ('Eurasia','Turkey') THEN 'Europe/Asia'
WHEN continent = 'Caribbean' AND name LIKE 'B%' THEN 'North America'
WHEN continent = 'Caribbean' AND name NOT LIKE 'B%' THEN 'South America'
ELSE continent END
FROM world
ORDER BY name
2021年02月01日 15点02分 17
level 5
多角星 楼主
SELECT from WORLD Tutorial
In this tutorial you will use the SELECT command on the table world:
Name and capital have the same length
11.
Greece has capital Athens.
Each of the strings 'Greece', and 'Athens' has 6 characters.
Show the name and capital where the name and the capital have the same number of characters.
You can use the LENGTH function to find the number of characters in a string
参考答案
SELECT name,capital
FROM world
WHERE LEN(capital) = LEN(name)
Matching name and capital
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
All the vowels
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 '% %'
2021年02月01日 15点02分 18
level 5
多角星 楼主
BBC QUIZ/zh
world
2021年02月01日 15点02分 19
level 5
多角星 楼主
SELECT from Nobel Tutorial/zh
nobel 諾貝爾獎得獎者
我們繼續練習簡單的單一表格SQL查詢。
這個教程是有關諾貝爾獎得獎者的:
nobel(yr, subject, winner)
yr: 年份
subject: 獎項
winner: 得獎者
練習
使用SELECT語句。
1.
更改查詢以顯示1950年諾貝爾獎的獎項資料。
参考答案
SELECT yr, subject, winner
FROM nobel
WHERE yr = 1950
2.
顯示誰贏得了1962年文學獎(Literature)。
参考答案
SELECT winner
FROM nobel
WHERE yr = 1962
AND subject = 'Literature'
3.
顯示“愛因斯坦”('Albert Einstein') 的獲獎年份和獎項。
参考答案
SELECT yr, subject
FROM nobel
WHERE winner = 'Albert Einstein'
4.
顯示2000年及以後的和平獎(‘Peace’)得獎者。
参考答案
SELECT winner
FROM nobel
WHERE yr >= 2000
AND subject = 'Peace'
5.
顯示1980年至1989年(包含首尾)的文學獎(Literature)獲獎者所有細節(年,主題,獲獎者)。
参考答案
SELECT yr, subject, winner
FROM nobel
WHERE yr BETWEEN 1980 AND 1989
AND subject = 'Literature'
6.
顯示總統獲勝者的所有細節:
西奧多•羅斯福 Theodore Roosevelt
伍德羅•威爾遜 Woodrow Wilson
吉米•卡特 Jimmy Carter
参考答案
SELECT * FROM nobel
WHERE winner IN ('Theodore Roosevelt',
'Woodrow Wilson',
'Jimmy Carter')
7.
顯示名字為John 的得獎者。 (注意:外國人名字(First name)在前,姓氏(Last name)在後)
参考答案
SELECT winner
FROM nobel
WHERE winner LIKE 'John%'
8.
顯示1980年物理學(physics)獲獎者,及1984年化學獎(chemistry)獲得者。
参考答案
SELECT yr, subject, winner
FROM nobel
WHERE yr = 1980 AND subject = 'physics'
OR yr = 1984 AND subject = 'chemistry'
9.
查看1980年獲獎者,但不包括化學獎(Chemistry)和醫學獎(Medicine)。
参考答案
SELECT yr, subject, winner
FROM nobel
WHERE yr = 1980
AND subject not in ('Chemistry','Medicine')
10.
顯示早期的醫學獎(Medicine)得獎者(1910之前,不包括1910),及近年文學獎(Literature)得獎者(2004年以後,包括2004年)。
参考答案
SELECT yr, subject, winner
FROM nobel
WHERE subject = 'Medicine' AND yr < 1910
OR subject = 'Literature' AND yr >= 2004
2021年02月11日 10点02分 20
level 5
多角星 楼主
SELECT from Nobel Tutorial/zh
Harder Questions
11.
Find all details of the prize won by PETER GRÜNBERG
Non-ASCII characters
参考答案
SELECT yr, subject, winner
FROM nobel
WHERE winner like 'PETER GR_NBERG'
12.
查找尤金•奧尼爾EUGENE O'NEILL得獎的所有細節 Find all details of the prize won by EUGENE O'NEILL
跳脫字符:單引號
参考答案
SELECT yr, subject, winner
FROM nobel
WHERE winner = 'EUGENE O\'NEILL'
13.
騎士列隊 Knights in order
列出爵士的獲獎者、年份、獎頁(爵士的名字以Sir開始)。先顯示最新獲獎者,然後同年再按名稱順序排列。
参考答案
SELECT winner, yr, subject
FROM nobel
WHERE winner like 'Sir%'
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
2021年02月21日 12点02分 22
level 5
多角星 楼主
SELECT from Nobel Tutorial
nobel Nobel Laureates
We continue practicing simple SQL queries on a single table.
This tutorial is concerned with a table of Nobel prize winners:
nobel(yr, subject, winner)
Using the SELECT statement.
Only Presidents
6.
Show all details of the presidential winners:
Theodore Roosevelt
Woodrow Wilson
Jimmy Carter
Barack Obama
参考答案
SELECT * FROM nobel
WHERE winner IN ('Theodore Roosevelt',
'Woodrow Wilson',
'Jimmy Carter',
'Barack Obama')
2021年02月21日 12点02分 23
level 5
多角星 楼主
Nobel Quiz/zh
2021年02月21日 14点02分 24
level 5
多角星 楼主
SELECT within SELECT Tutorial/zhJump to navigationJump to search
此教程教我們在SELECT查詢中使用別一個SELECT查詢,進行一些更複雜的查詢。
練習
1.
列出每個國家的名字 name,當中人口 population 是高於俄羅斯'Russia'的人口。
world(name, continent, area, population, gdp)
参考答案
SELECT name FROM world
WHERE population >
(SELECT population FROM world
WHERE name='Russia')
2.
列出歐州每國家的人均GDP,當中人均GDP要高於英國'United Kingdom'的數值。
人均GDP人均GDP即是 gdp除以population
参考答案
SELECT name FROM world
WHERE gdp/population >
(SELECT gdp/population FROM world
WHERE name = 'United Kingdom')
AND continent = 'Europe'
3.
在阿根廷Argentina 及 澳大利亞 Australia所在的洲份中,列出當中的國家名字 name 及洲分 continent 。按國字名字順序排序
参考答案
SELECT name, continent FROM world
WHERE continent in
(SELECT continent FROM world
WHERE name in ('Argentina','Australia'))
ORDER BY name
4.
哪一個國家的人口比加拿大Canada的多,但比波蘭Poland的少?列出國家名字name和人口population 。
参考答案
SELECT name, population FROM world
WHERE population >
(SELECT population FROM world
WHERE name = 'Canada')
AND population <
(SELECT population FROM world
WHERE name = 'Poland')
5.
Germany德國(人口8000萬),在Europe歐洲國家的人口最多。Austria奧地利(人口850萬)擁有德國總人口的11%。
顯示歐洲的國家名稱name和每個國家的人口population。以德國的人口的百分比作人口顯示。
小數位數百分號 %您可以使用函數 CONCAT 增加的百分比符號。
参考答案
SELECT name,
CONCAT(ROUND(population/(SELECT population FROM world WHERE name='Germany')*100,0),'%')
FROM world
WHERE continent = 'Europe'
練習SQL中較重要的功能--群組函數,按此到下一個教程。
如要練習一些較少用的SQL功能,看下去。
我們可以用ALL 這個詞對一個列表進行>=或>或<或<=充當比較。例如,你可以用此查詢找到世界上最大的國家(以人口計算):
SELECT name FROM world WHERE population >= ALL(SELECT population FROM world WHERE population>0)
你需在子查詢的條件中使用 population>0,因為有些國家的記錄中,人口是沒有填入,只有 null值。
6.
哪些國家的GDP比Europe歐洲的全部國家都要高呢? [只需列出 name 。] (有些國家的記錄中,GDP是NULL,沒有填入資料的。)
参考答案
SELECT name FROM world
WHERE GDP >
ALL(SELECT GDP FROM world
WHERE continent = 'Europe'
AND GDP > 0 )
我們可以在子查詢,參閱外部查詢的數值。我們為表格再命名,便可以分別內外兩個不同的表格。
7.
在每一個州中找出最大面積的國家,列出洲份 continent, 國家名字 name 及面積 area。 (有些國家的記錄中,AREA是NULL,沒有填入資料的。)
参考答案
SELECT continent, name, area FROM world x
WHERE area >= ALL
(SELECT area FROM world y
WHERE y.continent=x.continent
AND area>0)
8.
列出洲份名稱,和每個洲份中國家名字按子母順序是排首位的國家名。(即每洲只有列一國)
参考答案
SELECT continent, name FROM world x
WHERE x.name = (SELECT name FROM world y
WHERE y.continent=x.continent
ORDER BY name LIMIT 1)
2021年02月26日 15点02分 25
1