当前位置:网站首页>Sqlzoo question brushing record-2

Sqlzoo question brushing record-2

2022-06-09 07:28:00 Amelia who loves learning

  1. List per capita in each country in Europe GDP, Per capita GDP It is higher than that of Britain ’United Kingdom’ The value of .
select name from world
  where GDP/population>
     (select GDP/population from world
      where name='United Kingdom') and continent='Europe';
  1. In Argentina Argentina And Australia Australia In the state of , List the names of the countries name And zhoufen continent . Sort by country name .
select name,continent from world
  where continent in 
      (select continent from world 
       where name in ('Argentina','Australia'))
order by name;
  1. Which country has a larger population than Canada Canada More , But more than Poland Poland The less ? List country names name And population population .
select name,population from world
  where population>(Select population from world 
       where name = 'Canada')
      and
      population<(select population from world
       where name = 'Poland');
  1. Germany Germany ( Population 8000 Ten thousand ), stay Europe European countries have the largest population .Austria Austria ( Population 850 Ten thousand ) Of the total population of Germany 11%. Displays the European country name name And the population of each country population. 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),'%') from world
where continent='europe';
  1. Which countries GDP Than Europe All the countries in Europe are tall ? [ Just list name .] ( In the records of some countries ,GDP yes NULL, No information is filled in .)
select name from world 
  where GDP>
      (select max(GDP) from world
       where continent = 'Europe');
  1. Find the largest country in each state , List the States continent, The name of the country name And area area. ( In the records of some countries ,AREA yes NULL, No information is filled in .)
SELECT continent, name, area FROM world x
  WHERE area>= ALL
    (SELECT area FROM world y
        WHERE y.continent=x.continent
          AND area>0)
  1. Find the continent , All of them are less than or equal to 25000000 Population . In these States , List country names name,continent Chaufen and population Population .
select name,continent,population from world x 
  where 25000000>=all(select population from world y 
                      where y.continent=x.continent and population>0);
  1. The population of some countries is that of all other countries on the continent 3 Times or more . List The name of the country name and Zhoufen continent.
select name,continent from world x
  where population/3>=all (select population from world y
                           where x.continent=y.continent and x.name<>y.name);
  1. The population of some countries is that of all other countries on the continent 3 Times or more . List The name of the country name and Zhoufen continent.
select name,continent from world x
  where population/3>=all (select population from world y
                           where x.continent=y.continent
                               and x.name != y.name
                               and y.population>0);
  1. Show the total population of the world .
SELECT sum(population) FROM world;
  1. List all the States , Each one only once .
select distinct continent from world;
  1. Find Africa (Africa) Of GDP Sum .
select sum(GDP) from world
  where continent = 'Africa';
  1. How many countries have at least a million (1000000) The area of .
select count(name) from world 
  where area>=1000000;
  1. (‘France’,‘Germany’,‘Spain’)(“ France ”,“ Germany ”,“ Spain ”) What is the total population of ?
select sum(population) from world
  where name in ('France','Germany','Spain');
  1. For every continent , Show the number of States and countries .
select distinct continent,count(continent) from world
  group by continent;
  1. For every continent , Show States and at least 1000 Ten thousand people (10,000,000) Number of exporting countries .
select continent,count(continent) from world
  where population >= 10000000
group by continent;
  1. List at least 100 Million (1 Billion )(100,000,000) The state of the population .
select continent from world 
  group by continent
    having sum(population)>100000000;
  1. The first example lists players whose last names are ’Bender’ The goal data of . * Indicates to list all fields of a table , It simplifies writing matchid, teamid, player, gtime Sentence . Modify the SQL To list Event number matchid And player names player , The player represents the German team Germany Goal scored . To find out the German players , To check : teamid = ‘GER’.
SELECT matchid,player FROM goal 
  WHERE teamid = 'GER';
  1. From the above query , You can see Lars Bender’s In the event 1012 Score a goal .. Now we want to know which team is playing in this event . Pay attention to goal Fields in the table matchid , Is the corresponding table game The field of id. We can use the form game Find out the events in 1012 Information about . Show only events 1012 Of id, stadium, team1, team2.
SELECT id,stadium,team1,team2 FROM game g
  where g.id = 1012;
  1. We can use JOIN To do the above two steps at the same time .SELECT *
    FROM game JOIN goal ON (id=matchid), Sentence FROM Means to join two tables game and goal Data for . Sentence ON Indicates how to find out game Each column in the should be paired goal Which column in – goal Of id Must pair game Of matchid . In short , Namely ON (game.id=goal.matchid) following SQL List each player who scores ( From goal form ) And venue name ( From game form ), Modify it to display the name of each German player who scores a goal , Team name , Venue and date .
SELECT player,teamid,stadium,mdate
  FROM game JOIN goal ON (id=matchid)
    where teamid = 'GER';
  1. List the names of the players Mario (player LIKE ‘Mario%’) There are goals Team 1 team1, Team 2 team2 and Player name player.
select team1,team2,player from goal 
  join game on (id=matchid)
    where player like 'Mario%';
  1. form eteam Stored the data of each national team , Including the coach . You can use statements goal JOIN eteam on teamid=id Come together JOIN form goal To form eteam. List the first in each game 10 Minutes gtime<=10 There are players who score player, Team teamid, Coach coach, Goal time gtime.
SELECT player, teamid, coach, gtime
  FROM goal  join eteam on teamid = id
 WHERE gtime<=10;
  1. We should put them together JOIN form game And forms eteam, You can use game JOIN eteam ON (team1=eteam.id) or game JOIN eteam ON (team2=eteam.id) Note the field id It's also a table game And forms eteam The field of , You should point out clearly eteam.id Not just id.
select mdate, teamname from game join eteam on (game.team1=eteam.id)
  where eteam.coach = 'Fernando Santos';
  1. List venues 'National Stadium, Warsaw’ Our goal scorer .
select player from game join goal on matchid = id
  where stadium = 'National Stadium, Warsaw';
  1. The following example finds Germany - Greece Germany-Greece The goal of the top eight competition of
    Modify it , Just list all events , The name of the player who shot into the German goal .
select distinct player from game g1 join goal g2 on matchid = id
  where (g1.team1='GER'and teamid !='GER')
    or (g1.team2='GER'and teamid !='GER');
  1. List team names teamname And the total number of goals scored by the team
SELECT distinct teamname, count(player)
  FROM eteam JOIN goal ON id=teamid
 group BY teamname;
  1. List the name of the venue and the number of goals scored at the venue .
select distinct stadium,count(gtime) from game join goal on matchid=id
  group by stadium;
  1. Every polish ’POL’ In the event of participation , List the event number matchid, date date And goal numbers .
SELECT matchid,mdate,count(team1)
  FROM game JOIN goal ON matchid = id 
 WHERE (team1 = 'POL' OR team2 = 'POL')
group by matchid,mdate;
  1. Every German ’GER’ In the event of participation , List the event number matchid, date date And Germany's goal numbers .
select matchid,mdate,count(teamid) from game join goal on matchid=id
  where (team1 = 'GER' or team2 = 'GER') and teamid = 'GER'
group by matchid,mdate;
  1. 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.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 mdate, 
  team1, SUM(CASE WHEN teamid = team1 THEN 1 ELSE 0 END) AS score1,
  team2, SUM(CASE WHEN teamid = team2 THEN 1 ELSE 0 END) AS score2
  FROM game LEFT OUTER JOIN goal
  on game.id = goal.matchid
  GROUP BY mdate, id, team1, team2;
原网站

版权声明
本文为[Amelia who loves learning]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/160/202206090721584527.html