sqlzoo练习答案--SELECT within SELECT Tutorial

This tutorial looks at how we can use SELECT statements within SELECT statements to perform more complex queries.

name continent area population gdp
Afghanistan Asia 652230 25500100 20343000000
Albania Europe 28748 2831741 12960000000
Algeria Africa 2381741 37100000 188681000000
Andorra Europe 468 78115 3712000000
Angola Africa 1246700 20609294 100990000000
...

1、

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

world(name, continent, area, population, gdp)
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 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‘) order by name

5、

Germany (population 80 million) has the largest population of the countries in Europe. Austria (population 8.5 million) has 11% of the population of Germany.

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

Decimal places

Percent symbol %

select name,CONCAT(ROUND(100*population/(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 gdp > 0 and continent=‘Europe‘)

7、Find the largest country (by area) in each continent, show thecontinent, 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)

8、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)

9、Find the continents where all countries have a population <= 25000000. Then find the names of the countries associated with these continents. Show namecontinent and population.

SELECT name, continent, population FROM world x
  WHERE 25000000>=ALL (SELECT population FROM world 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 name,continent from world x where x.population/3 >= all(select population from world y where x.continent=y.continent and x.name!=y.name and y.population>0)
时间: 2024-12-23 06:22:59

sqlzoo练习答案--SELECT within SELECT Tutorial的相关文章

SQL笔记1:SELECT及SELECT高级应用

T-SQL笔记1:SELECT及SELECT高级应用 本章摘要 1:安装AdventureWorks 2:基本运算符和表达式 3:between 4:like 5:escape 6:TOP 7:GROUP BY 7.1:GROUP BY ALL 7.2:HAVING 8:SELECT字句技术 8.1:使用DISTINCT消除重复值 8.2:返回拼接的结果 8.3使用INTO字句 9:子查询 9.1:子查询类型 9.2:代替表达式的查询 9.3:多层嵌套 10:比较使用 EXISTS 和 IN 的

PHP MySQL Select 之Select

从数据库表中选取数据 SELECT 语句用于从数据库中选取数据. 语法 SELECT column_name(s) FROM table_name 注释:SQL 语句对大小写不敏感.SELECT 与 select 等效. 为了让 PHP 执行上面的语句,我们必须使用 mysql_query() 函数.该函数用于向 MySQL 发送查询或命令. 例子 下面的例子选取存储在 "Persons" 表中的所有数据(* 字符选取表中所有数据): <?php $con = mysql_con

Oracle中insert into select和select into的用法(转)

原文地址:http://hi.baidu.com/huahua035/item/87d5e71e6a7d31f187ad4ea5 两张表进行数据的拷贝,最常用的拷贝语句是: insert into select  和 select into from 但是请绝对的注意: 在Oracle中select into from不可以使用-----原因很简单:select into是PL/SQL language 的赋值语句!如果使用则Oracle会抛出0RA-00905:missing keyword的

select *和select 全部

select *和select 全部字段 在查询上效果是一样的,速度也是一样的. 不过理论上来说select *反而会快点. 因为 1.select 全部字段在数据传输上消耗会更多,如果几百个字段这个速度还真不一定能忽略. 2.数据库查询会在执行前分析查询字段,如果是select *那么就不需要这步了. 注:对于做ORM结构的话,通用代码实例化一般为字段全部取出,这时select *在维护性上会稍好一些,不过select *多表时字段名可能有冲突这个得注意.

sqlzoo练习答案--SELECT from Nobel Tutorial/zh

nobel 諾貝爾獎得獎者 我們繼續練習簡單的單一表格SQL查詢. 這個教程是有關諾貝爾獎得獎者的: nobel(yr, subject, winner) yr subject winner 1960 Chemistry Willard F. Libby 1960 Literature Saint-John Perse 1960 Medicine Sir Frank Macfarlane Burnet 1960 Medicine Peter Madawar ... yr: 年份subject:

sqlzoo练习答案--SELECT names/zh

name continent Afghanistan Asia Albania Europe Algeria Africa Andorra Europe Angola Africa .... name:國家名稱continent:洲份 1. 你可以用WHERE name LIKE 'B%'來找出以 B 為開首的國家.%是萬用字元,可以用代表任何字完. 找出以 Y 為開首的國家. SELECT name FROM world WHERE name LIKE 'Y%' 2.找出以 Y 為結尾的國家.

sqlzoo练习答案--SELECT basics/zh

這個教程介紹SQL語言.我們會使用SELECT語句.我們會使用WORLD表格 name continent area population gdp Afghanistan Asia 652230 25500100 20343000000 Albania Europe 28748 2831741 12960000000 Algeria Africa 2381741 37100000 188681000000 Andorra Europe 468 78115 3712000000 Angola Af

大连廿四2016暑假集训day1-T3(quick select&amp;linear select)

3 kth3.1 Description给定 n 个不超过 10^9 的正整数,请线性时间选择算法 (linear select)求其中的第 k 大值.3.2 Input第一行两个整数 n,k. 第二行 n 个整数,表示题目中的那 n 个正整数.3.3 Output一行,表示答案.3.4 Sample Input10 3 2 4 7 3 5 6 9 6 1 83.5 Sample Output73.6 Constraints一共 10 个测试点,每个测试点 10 分,只有当你的答案与标准答案完全

sqlzoo练习答案--SUM and COUNT

World Country Profile: Aggregate functions This tutorial is about aggregate functions such as COUNT, SUM and AVG. An aggregate function takes many values and delivers just one value. For example the function SUM would aggregate the values 2, 4 and 5