標籤:

SQL練習(一)

SQL練習(一)

SELECT from WORLD Tutorial

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. 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 worldWHERE population>=200000000

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 worldWHERE population>=200000000

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 from world where continent=South America

5.Show the name and population for France, Germany, Italy

select name,population from world where name in (France,Germany,Italy)

6.Show the countries which have a name that includes the word United

select name from world where name like %United%

7.Two ways to be big: A country is big if it has an area of more than 3 million sq km or it has a population of more than 250 million.

select name,population,area from world where population>=250000000 or area >=3000000

8.Exclusive OR (XOR). 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 population>=250000000 and area<3000000 or population<250000000 and area >= 3000000

9.ROUND()函數

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.

select name,round(population/1000000,2),round(gdp/1000000000,2) from worldwhere continent=South America

10.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/1000,0)*1000 from world where gdp>=1000000000000

11.Greece has capital Athens.

Each of the strings Greece, and Athens has 6 characters.

SELECT name, capital from worldWHERE LENGTH(name)=LENGTH(capital)

12.Show the name and the capital where the first letters of each match. Dont 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

13.Equatorial Guinea and Dominican Republic have all of the vowels (a e i o u) in the name. They dont 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.

SELECT name FROM worldWHERE name LIKE %a% AND name LIKE %e%AND name LIKE %i%AND name LIKE %o%AND name LIKE %u%AND name NOT LIKE %a%a%AND name NOT LIKE % %

SELECT from Nobel Tutorial

1.Change the query shown so that it displays Nobel prizes for 1950.

SELECT yr, subject, winner FROM nobelWHERE yr = 1950

2.Show who won the 1962 prize for Literature.

SELECT winner FROM nobelWHERE yr = 1962 AND subject = Literature

3.Show the year and subject that won Albert Einstein his prize.

SELECT yr,subject FROM nobel WHERE winner=Albert Einstein

4.Give the name of the Peace winners since the year 2000, including 2000.

SELECT winner FROM nobel WHERE subject=Peace AND yr>=2000

5.Show all details (yr, subject, winner) of the Literature prize winners for 1980 to 1989 inclusive.

SELECT yr,subject,winner FROM nobel WHERE yr<=1989 AND yr>=1980 AND subject=Literature

6.Show all details of the presidential winners:

  • Theodore Roosevelt
  • Woodrow Wilson
  • Jimmy Carter
  • Barack Obama

7.Show the winners with first name John

SELECT winner FROM nobel WHERE winner LIKE John%

8.Show the year, subject, and name of Physics winners for 1980 together with the Chemistry winners for 1984.

SELECT * FROM nobel WHERE subject=Physics AND yr=1980 OR subject=Chemistry AND yr=1984

9.Show the year, subject, and name of winners for 1980 excluding Chemistry and Medicine

SELECT * FROM nobel WHERE subject NOT IN (Chemistry,Medicine) AND yr=1980

10.Show year, subject, and name of people who won a Medicine prize in an early year (before 1910, not including 1910) together with winners of a Literature prize in a later year (after 2004, including 2004)

SELECT * FROM nobel WHERE subject=Medicine AND yr<1910 OR subject=Literature AND yr>=2004

11.Find all details of the prize won by PETER GRüNBERG

SELECT * FROM nobel WHERE winner=PETER GRüNBERG

12.You cant put a single quote in a quote string directly. You can use two single quotes within a quoted string.

Find all details of the prize won by EUGENE ONEILL

SELECT * FROM nobel WHERE winner="EUGENE ONEILL"

13.Knights in order

List the winners, year and subject where the winner starts with Sir. Show the the most recent first, then by name order.

SELECT winner,yr,subject FROM nobel WHERE winner LIKE Sir%ORDER BY yr DESC,winner

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) ASC,subject,winner

SELECT within SELECT Tutorial

1.List each country name where the population is larger than that of Russia.

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

2.Show the countries in Europe with a per capita GDP greater than United Kingdom.

SELECT name FROM world WHERE GDP/population>(SELECT GDP/population FROM world WHERE name=United Kingdom) AND continent=Europe

3.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 worldWHERE continent=(SELECT continent FROM world WHERE name=Argentina)OR continent=(SELECT continent FROM world WHERE name=Australia)ORDER BY name

4.Which country has a population that is more than Canada but less than Poland? Show the name and the population.

SELECT name,population FROM world WHERE population<(SELECT population FROM world WHERE name=Poland)AND population>(SELECT population FROM world WHERE name=Canada)

5.Show the name and the population of each country in Europe. Show the population as a percentage of the population of Germany.

SELECT name, concat(round(population/(SELECT population FROM world WHERE name=Germany)*100, 0), %) as populationFROM worldWHERE continent=Europe;

6.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 GDP>0 AND continent=Europe)AND continent<>Europe

7.Find the largest country (by area) in each continent, show the continent, the name and the area

SELECT x.continent, x.name, x.area FROM world as xWHERE x.area >= ALL(SELECT y.area FROM world as yWHERE y.continent=x.continentAND y.area>0);

8.List each continent and the name of the country that comes first alphabetically

SELECT x.continent,x.name FROM world AS xWHERE x.name<=ALL(SELECT y.name FROM world AS y WHERE x.continent=y.continent)ORDER BY name

9.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 x.name,x.continent,x.population FROM world AS x WHERE 25000000>ALL(SELECT y.population FROM world AS y WHERE x.continent=y.continent AND population>0)

10.Some countries have populations more than three times that of any of their neighbours (in the same continent). Give the countries and continents.

SELECT x.name,x.continent FROM world AS x WHERE x.population>=ALL(SELECT y.population*3 FROM world AS y WHERE x.continent=y.continent and x.name!=y.name)

SUM and COUNT

1.Show the totalpopulationof the world.

SELECT SUM(population)FROM world

2.List all the continents - just once each.

SELECT DISTINCT continent FROM world

3.Give the total GDP of Africa

SELECT SUM(gdp) from world WHERE continent=Africa

4.How many countries have an area of at least 1000000

SELECT COUNT(name) FROM world WHERE area>=1000000

5.What is the total population of (Estonia, Latvia, Lithuania)

SELECT SUM(population) FROM world WHERE name=Estonia OR name=Latvia OR name=Lithuania

6.For each continent show the continent and number of countries.

SELECT continent,COUNT(name) FROM world GROUP BY continent

7.For each continent show the continent and number of countries with populations of at least 10 million.

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

8.List the continents that have a total population of at least 100 million.

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

JOIN

1.Modify it to show the matchid and playername for all goals scored by Germany. To identify German players, check for: teamid = GER

SELECT matchid,player FROM goal WHERE teamid = GER

2.Show id, stadium, team1, team2 for just game 1012

SELECT DISTINCT game.id,game.stadium,game.team1,game.team2 FROM game INNER JOIN goal ON game.id=goal.matchidWHERE game.id=1012

3.Modify it to show the player, teamid, stadium and mdate for every German goal.

SELECT goal.player,goal.teamid,game.stadium,game.mdateFROM game INNER JOIN goal ON game.id=goal.matchidWHERE goal.teamid=GER

4.Use the same JOIN as in the previous question.

Show the team1, team2 and player for every goal scored by a player called Mario player LIKE Mario%

SELECT game.team1,game.team2,goal.player FROM game INNER JOIN goal ON game.id=goal.matchid WHERE goal.player LIKE Mario%

5.The table eteam gives details of every national team including the coach. You can JOIN goal to eteam using the phrase goal JOIN eteam on teamid=id

Show player, teamid, coach, gtime for all goals scored in the first 10 minutes gtime<=10

SELECT goal.player, goal.teamid, eteam.coach,goal.gtimeFROM goal INNER JOIN eteam ON goal.teamid=eteam.id WHERE gtime<=10

6.List the the dates of the matches and the name of the team in which Fernando Santos was the team1 coach.

SELECT game.mdate,eteam.teamname FROM game INNER JOIN eteam ON eteam.id=game.team1 WHERE eteam.coach=Fernando Santos

7.List the player for every goal scored in a game where the stadium was National Stadium, Warsaw

SELECT goal.player FROM goal INNER JOIN game ON game.id=goal.matchidWHERE game.stadium=National Stadium, Warsaw

8.Show thenameof all players who scored a goal against Germany.

SELECT DISTINCT player FROM game JOIN goal ON matchid = id WHERE (team1=GER OR team2=GER) AND teamid!=GER

9.Show teamname and the total number of goals scored.

SELECT eteam.teamname, COUNT(goal.player)FROM eteam JOIN goal ON eteam.id=goal.teamidGROUP BY teamname

10.Show the stadium and the number of goals scored in each stadium.

SELECT game.stadium,COUNT(goal.player) AS number FROM game JOIN goal ON game.id=goal.matchid GROUP BY stadium

11.For every match involving POL, show the matchid, date and the number of goals scored.

SELECT goal.matchid,game.mdate, COUNT(goal.player) AS numberFROM game JOIN goal ON goal.matchid = game.idWHERE (game.team1 = POL OR game.team2 = POL)GROUP BY goal.matchid

12.For every match where GER scored, show matchid, match date and the number of goals scored by GER

SELECT goal.matchid,game.mdate, COUNT(goal.teamid) AS numberFROM game JOIN goal ON goal.matchid = game.idWHERE (game.team1 = GER OR game.team2 = GER) AND teamid=GERGROUP BY goal.matchid

13.Notice in the query given every goal is listed. If it was a team1 goal then a 1 appears in score1, otherwise there is a 0. You could SUM this column to get a count of the goals scored by team1.Sort your result by mdate, matchid, team1 and team2.

SELECT game.mdate,game.team1,SUM(CASE WHEN teamid=team1 THEN 1 ELSE 0 END) score1,game.team2,SUM(CASE WHEN teamid=team2 THEN 1 ELSE 0 END) score2FROM game LEFT JOIN goal ON matchid = idGROUP BY mdate, matchid, team1,team2

推薦閱讀:

學習SQL【6】-複雜查詢
數據分析系列——SQL 必知必會(二)
Mysql常用操作總結(上)
mysql資料庫為啥要做主從複製?

TAG:SQL | SQL語句 |