SQLzoo刷题答案

本文提供了SQLzoo中多个教程的练习题解答,包括诺贝尔奖数据查询、SQL子查询、聚合函数、JOIN操作等,涵盖了各种复杂的数据查询场景。
摘要由CSDN通过智能技术生成

SELECT from Nobel Tutorial

  1. Change the query shown so that it displays Nobel prizes for 1950.
SELECT yr, subject, winner FROM nobel
WHERE yr = 1950
  1. Show who won the 1962 prize for Literature.
SELECT winner FROM nobel
WHERE yr = 1962
   AND subject = 'Literature'
  1. Show the year and subject that won ‘Albert Einstein’ his prize.
SELECT yr,subject FROM nobel
WHERE winner='Albert Einstein'
  1. Give the name of the ‘Peace’ winners since the year 2000, including 2000.
SELECT winner FROM nobel
WHERE subject='Peace'
          and yr>=2000`
  1. 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')
  1. 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')
  1. Show the winners with first name John
SELECT winner FROM nobel
WHERE winner like concat ('John','%')
  1. Show the year, subject, and name of Physics winners for 1980 together with the Chemistry winners for 1984.
SELECT yr, subject,winner FROM nobel
WHERE subject='Physics' and yr='1980'
   or (subject='Chemistry' and yr='1984')
  1. Show the year, subject, and name of winners for 1980 excluding Chemistry and Medicine
SELECT yr, subject,winner FROM nobel
WHERE yr='1980'
  and (subject!='Chemistry')
  and (subject!='Medicine')
  1. 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 yr, subject,winner FROM nobel
WHERE (subject='Medicine' and (yr<1910))
   or (subject='Literature' and (yr>=2004))
  1. Find all details of the prize won by PETER GRÜNBERG
SELECT * FROM nobel
WHERE winner='PETER GRÜNBERG'
  1. Find all details of the prize won by EUGENE O’NEILL
SELECT * FROM nobel
WHERE winner='EUGENE O''NEILL' /*使用两个单引号代替一个,为转义字符*/
  1. 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 /*先按年份的降序,再按获奖者的升序*/
  1. Show the 1984 winners and subject ordered by subject and winner name; but list Chemistry and Physics last.
在这里插入代码片

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')
  1. 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')
  1. 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
  1. 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')
  1. Show the name and the population of each country in Europe. Show the population as a percentage of the population of Germany.
在这里插入代码片
  1. 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')
  1. 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)
  1. List each continent and the name of the country that comes first alphabetically.
SELECT x.continent,x.name FROM world AS x
WHERE x.name<=ALL
    (SELECT y.name FROM world AS y WHERE x.continent=y.continent)
  1. 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.找出所有国家人口不超过25000000的州
SELECT x.name,x.continent,x.population FROM world AS x
WHERE 25000000>=ALL (
         SELECT population FROM world y
         WHERE x.continent=y.continent
           AND population>0)
  1. 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 (
            
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值