"Monaco-Ville"是合併國家名字 "Monaco" 和延伸詞"-Ville".
顯示國家名字,及其延伸詞,如首都是國家名字的延伸。
你可以使用SQL函數 REPLACE 或 MID.
SELECT name,REPLACE(capital,name,'')
FROM world
WHERE capital LIKE CONCAT(name,'_%')
相關函數:
- REPLACE(f, s1, s2) ,對f,用字符串s2替代s1,如果替代成'',相當於刪除s1字符 REPLACE('vessel','e','a') -> 'vassal'
- CONCAT 連接兩個字符串成一個字符
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: a XOR b 不同時滿足a,b 滿足a或者滿足b
Show the name
and per-capita GDP for those countries with a GDP of at least one trillion (1000000000000; that is 12 zeros). Round this value to the nearest 1000.
SELECT name,ROUND(gdp/population,-3)
FROM world
WHERE gdp>=1000000000000
ROUND(f,p) returns f rounded to p decimal places ,p是正數返回p位小數,p是負數爲標準化個位、十位、百位…
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
LEFT: LEFT(s,n) allows you to extract n characters from the start of the string s.提取字符串s的第n個字符
Find all details of the prize won by EUGENE O'NEILL
SELECT * FROM nobel
WHERE winner='EUGENE O\'NEILL'
在字符串中遇到'這種字符應用\進行轉義
List the name and continent of countries in the continents containing either Argentina or Australia. Order by name of the country.
SELECT name,continent
FROM world
WHERE continent IN (SELECT continent FROM world
WHERE name IN ('Argentina','Australia'))
ORDER BY name
因爲子查詢中返回的continent不止一行,所以第一個SELECT的判斷應該用IN 而不能等於
Which countries have a GDP greater than every country in Europe? [Give the name only.] (Some countries may have NULL gdp values)
SELECT name FROM world
WHERE gdp> ALL(SELECT gdp FROM world WHERE continent='Europe' AND gdp IS NOT NULL)
IS NOT NULL判斷不爲NULL ,IS NULL 判斷爲NULL
ALL可以和比較符號連用,用以比較單個數值和列表中所有的數值,不能a>3 all(b) 只能 a/3 >all(b)
Find the largest country (by area) in each continent, show the continent, the name and the area:
SELECT continent, name, area FROM world x
WHERE x.area >=
ALL(SELECT y.area FROM world y
WHERE y.continent=x.continent
AND area>0)
List each continent and the name of the country that comes first alphabetically.
SELECT continent,name FROM world x
WHERE x.name=(SELECT y.name FROM world y
WHERE y.continent=x.continent
ORDER BY name LIMIT 1)
利用重命名,外層SELECT和內層SELECT 實現自己和自己比較
ORDER BY name LIMIT 1按照name排序,返回一個 。
LIMIT n等價於LIMIT 0,n LIMIT i, n爲索引i開始(第一條是0),返回n條
Find the continents where all countries have a population <= 25000000. Then find the names of the countries associated with these continents. Show name, continent and population.
SELECT name, continent, population FROM world x
WHERE 25000000>= ALL
(SELECT y.population FROM world y
WHERE y.continent=x.continent)
Some countries have populations more than three times that of any of their neighbours (in the same continent). Give the countries and continents.
SELECT name,continent FROM world x
WHERE population>=ALL (SELECT 3*y.population FROM world y
WHERE y.continent=x.continent
AND y.name<>x.name)
SELECT mdate,team1,
SUM(CASE WHEN teamid=team1 THEN 1 ELSE 0 END)
score1,
team2,
SUM(CASE WHEN teamid=team2 THEN 1 ELSE 0 END)
score2
FROM game left JOIN goal ON matchid = id
GROUP BY mdate, matchid,team1,team2
CASE用法:
CASE WHEN condition1 THEN value1
WHEN condition 2 THEN value2
ELSE value3
END
Obtain the cast list for 'Casablanca'.
SELECT name FROM actor JOIN casting ON actorid=id
WHERE movieid=(SELECT id FROM movie
WHERE title='Casablanca')
Use COUNT and GROUP BY dept.name to show each department and the number of staff. Use a RIGHT JOIN to ensure that the Engineering department is listed
SELECT dept.name,COUNT(teacher.name) FROM teacher
RIGHT JOIN dept ON teacher.dept=dept.id
GROUP BY dept.name
COUNT(*) 統計包含NULL行,COUNT(字段名)不包含NULL,left join保證所有的部門都被列出來。
COALESCE
- COALESCE(x,y,z) = x if x is not NULL
- COALESCE(x,y,z) = y if x is NULL and y is not NULL
- COALESCE(x,y,z) = z if x and y are NULL but z is not NULL
- COALESCE(x,y,z) = NULL if x and y and z are all NULL