SQLZOO练习网址:

SELECT basics/zh?

sqlzoo.net

SELECT basics

1. Modify it to show the population of Germany

SELECT population FROM world WHERE name = Germany

2.Show the name and the population for Sweden, Norway and Denmark

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

3.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

注意:BETWEEN AND 包含边界值


SELECT from WORLD

1.Read the notes about this table. 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.How to use WHERE to filter records. 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

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

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 like 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.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

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 (area>3000000 and population <250000000) or (area<3000000 and population >250000000)

9.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.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) as gdp
from world
where 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.Show per-capita GDP for the trillion dollar countries to the nearest $1000.

select name,round(gdp/population,-3) from world
where gdp>=1000000000000

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)

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.

题目解释:选取出第一个字元相同的name和capital,并排除name和capital相同的数据。

SELECT name, capital FROM world WHERE LEFT(name, 1) = LEFT(capital, 1) XOR name = capital

或者

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

注意:XOR(异或)——两个条件相同(同真或同假)即为假(0),两个条件不同即为真(1),null与任何条件做异或运算都为null

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 % %;


SELECT from Nobel

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

SELECT yr, subject, winner FROM nobel WHERE yr = 1950

2.Show who won the 1962 prize for Literature.

SELECT winner FROM nobel WHERE yr = 1962 AND subject = Literature

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

select yr,subject from nobelwhere winner =Albert Einstein

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

select winner from nobel where yr>=2000and subject =Peace

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 between 1980 and 1989 and subject=Literature

6.Show all details of the presidential winners:Theodore Roosevelt、Woodrow Wilson、Jimmy Carter

SELECT * FROM nobel WHERE winner IN (Theodore Roosevelt,Woodrow Wilson,Jimmy Carter)

7.Show the winners with first name John

select winner from nobel where winner like John%

8.Show the Physics winners for 1980 together with the Chemistry winners for 1984.

select yr,subject,winner from nobel
where (yr=1980and subject=physics) or (yr=1984and subject =chemistry

9.Show the winners for 1980 excluding the Chemistry and Medicine

select yr,subject,winner from nobel
where yr=1980 and subject not in(Chemistry,Medicine

10.Show 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 yr,subject,winner from nobel
where (yr<1910 and subject =Medicine) or (yr>=2004 and subject =Literature)

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

select yr,subject,winner from nobel where winner =PETER GRüNBERG

12.Find all details of the prize won by EUGENE ONEILL

select yr,subject,winner from nobel where winner =EUGENE ONEILL

注意:你不能把一个单引号直接的放在字元串中。但可连续使用两个单引号在字元串中当作一个单引号。

13.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.Show the 1984 winners 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


SELECT within SELECT

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 continent =Europeand ((gdp/population)>(select gdp/population from world where name =United Kingdom))

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 world
where continent in(select continent from world where name in(Argentina,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 =Canada)
and population <(select population from world where name=Poland)

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*100/(select population from world where name=Germany)),%)
from world
where 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 continent =Europe and gdp >0)

7. 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 area >= ALL (SELECT area FROM world y WHERE y.continent=x.continent AND area>0)

补充知识点:群组函数——ALL

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

select continent,name from world a
where a.name <= all(select b.name from world b where a.continent=b.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 a.name,a.continent,a.population from world a
where 25000000>= all(select b.population from world b where a.continent=b.continent )


SUM and COUNT

1. Show the total population of 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 (France,Germany,Spain)

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

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;


The JOIN operation

1.Modify it to show the matchid and player name for all goals scored by Germany.

SELECT matchid,player FROM goal WHERE teamid = GER

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

SELECT id,stadium,team1,team2 FROM game where id =1012

3.The code below shows the player (from the goal) and stadium name (from the game table) for every goal scored.

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

SELECT b.player, b.teamid, a.stadium, a.mdate FROM game a JOIN goal b ON (a.id=b.matchid)
WHERE b.teamid = GER;

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

select team1,team2,player from game join goal on id=matchid
where player like Mario%

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

SELECT player, teamid,coach,gtime FROM goal join eteam on teamid=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 b.mdate,a.teamname from eteam a join game b on b.team1=a.id
where a.coach=Fernando Santos

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

select player from ((eteam a join game b on a.id=b.team1) join goal c on b.id=c.matchid )
where b.stadium = National Stadium, Warsaw

8. The example query shows all goals scored in the Germany-Greece quarterfinal. Instead show the name of all players who scored a goal against Germany.

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

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

SELECT teamname,count(player) FROM eteam JOIN goal ON id=teamid
group by teamname
ORDER BY teamname

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

select stadium,count(player) FROM game JOIN goal ON id=matchid GROUP BY stadium;

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

SELECT matchid,mdate,count(team1) FROM game JOIN goal ON matchid = id
WHERE (team1 = POL OR team2 = POL)
group by matchid,mdate

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

select matchid,mdate,count(player) FROM game JOIN goal ON matchid = id
WHERE teamid = GER
group by matchid,mdate

13.List every match with the goals scored by each team as shown. This will use "CASE WHEN" which has not been explained in any previous exercises.

Sort your result by mdate, matchid, team1 and team2.

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


More JOIN operations

1.List the films where the yr is 1962 [Show id, title]

SELECT id,title FROM movie WHERE yr=1962

2.Give year of Citizen Kane.

select yr from movie where title =Citizen Kane

3. List all of the Star Trek movies, include the id, title and yr (all of these movies include the words Star Trek in the title). Order results by year.

select id,title,yr from movie where title like Star Trek%

4.What id number does the actress Glenn Close have?

select title from movie where id in(11768,11955,21191)

5.What is the id of the film Casablanca

SELECT id FROM actor WHERE name = Glenn Close;

6. What is the id of the film Casablanca

select id from movie where title=Casablanca

7.Obtain the cast list for Casablanca. Use movieid=11768, this is the value that you obtained in the previous question.

SELECT name FROM actor JOIN casting ON id=actorid WHERE movieid = 11768;

8.Obtain the cast list for the film Alien

select name from (movie m join casting c on m.id=c.movieid) join actor a on a.id=c.actorid
WHERE title = Alien

9.List the films in which Harrison Ford has appeared

select title from (movie m join casting c on m.id=c.movieid) join actor a on a.id=c.actorid
where name=Harrison Ford

10.List the films where Harrison Ford has appeared - but not in the starring role.

select title from (movie m join casting c on m.id=c.movieid) join actor a on a.id=c.actorid
where name=Harrison Ford and ord <>1

11.List the films together with the leading star for all 1962 films.

select title,name from (movie m join casting c on m.id=c.movieid) join actor a on a.id=c.actorid
where yr=1962 and ord=1

13. List the film title and the leading actor for all of the films Julie Andrews played in.

select title,name from (movie m join casting c on m.id=c.movieid) join actor a on a.id=c.actorid
where c.movieid in(select movieid from casting join actor on id=actorid where name =Julie Andrews)
and ord=1

14.Obtain a list, in alphabetical order, of actors whove had at least 30 starring roles.

SELECT name FROM casting JOIN actor ON actorid=actor.id
WHERE ord = 1
GROUP BY actor.id, name
HAVING COUNT(name) >= 30
ORDER BY name;

16. List all the people who have worked with Art Garfunkel.

select name from actor a join casting c on a.id=c.actorid
where movieid in
(select movieid from casting join actor on id= actorid where name = Art Garfunkel)
and name<>Art Garfunkel


Using Null

1.List the teachers who have NULL for their department.

select name from teacher where dept is null

3.Use a different JOIN so that all teachers are listed.

SELECT teacher.name, dept.name FROM teacher left JOIN dept ON (teacher.dept=dept.id)

4.Use a different JOIN so that all teachers are listed.

SELECT teacher.name, dept.name FROM teacher right JOIN dept ON (teacher.dept=dept.id)

5.Use COALESCE to print the mobile number. Use the number 07986 444 2266 there is no number given. Show teacher name and mobile number or 07986 444 2266

select name,coalesce(mobile,07986 444 2266) from teacher

6.Use the COALESCE function and a LEFT JOIN to print the name and department name. Use the string None where there is no department.

select t.name,coalesce(d.name,None) from teacher t left join dept d on t.dept=d.id

7. Use COUNT to show the number of teachers and the number of mobile phones.

select count(name),count(mobile) from teacher

8.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 d.name,count(t.name) from teacher t right join dept d on t.dept=d.id group by d.name

9.Use CASE to show the name of each teacher followed by Sci if the the teacher is in dept 1 or 2 and Art otherwise.

select name,(case when dept in(1,2) then Scielse Art end) from teacher

10. Use CASE to show the name of each teacher followed by Sci if the the teacher is in dept 1 or 2 show Art if the dept is 3 and None otherwise.

SELECT
teacher.name,
(CASE WHEN teacher.dept IN (1, 2) THEN Sci WHEN teacher.dept = 3 THEN Art ELSE None END)
FROM teacher;

推荐阅读:

相关文章