mysql----Nested SELECT Quiz

Nested SELECT quiz

bbc
name region area population gdp
Afghanistan South Asia 652225 26000000  
Albania Europe 28728 3200000 6656000000
Algeria Middle East 2400000 32900000 75012000000
Andorra Europe 468 64000  
Bangladesh South Asia 143998 152600000 67144000000
United Kingdom Europe 242514 59600000 2022824000000
...

1. Select the code that shows the name, region and population of the smallest country in each region

 SELECT region, name, FROM bbc x WHERE population <= ALL (SELECT population FROM bbc y WHERE y.region=x.region AND population>0)
 SELECT region, name, population FROM bbc WHERE population <= ALL (SELECT population FROM bbc WHERE population>0)
 SELECT region, name, population FROM bbc x WHERE population <= ALL (SELECT population FROM bbc y WHERE y.region=x.region AND population>0)
 SELECT region, name, population FROM bbc x WHERE population = ALL (SELECT population FROM bbc y WHERE y.region=x.region AND population>0)
 SELECT region, name, population FROM bbc x WHERE population <= ALL (SELECT population FROM bbc y WHERE y.region=x.region AND population<0)

2. Select the code that shows the countries belonging to regions with all populations over 50000

 SELECT name,region,population FROM bbc x WHERE 50000 < ALL (SELECT population FROM bbc y WHERE population>0)
 SELECT name,region,population FROM bbc x WHERE 50000 < ALL (SELECT population FROM bbc y WHERE x.region=y.region AND y.population>0)
 SELECT name,region,population FROM bbc x WHERE 50000 = ALL (SELECT population FROM bbc y WHERE x.region=y.region AND y.population>0)
 SELECT name,region,population FROM bbc x WHERE 50000 > ALL (SELECT population FROM bbc y WHERE x.region=y.region AND y.population>0)
 SELECT name,region,population FROM bbc x WHERE 500000 < ALL (SELECT population FROM bbc y WHERE x.region=y.region AND y.population>0)

3. Select the code that shows the countries with a less than a third of the population of the countries around it

SELECT name, region FROM bbc x
 WHERE population < ALL (SELECT population/3 FROM bbc y WHERE y.region = x.region AND y.name != x.name)
SELECT name, region FROM bbc x
 WHERE population = ALL (SELECT population/3 FROM bbc y WHERE y.region = x.region AND y.name != x.name)
SELECT name, region FROM bbc x
 WHERE population > ALL (SELECT population/3 FROM bbc y WHERE y.region = x.region AND y.name != x.name)
 SELECT name, region FROM bbc x WHERE population < ALL (SELECT population*3 FROM bbc y WHERE y.region = x.region AND y.name != x.name)
 SELECT name, region FROM bbc x WHERE population < ALL (SELECT population/3 FROM bbc y WHERE y.name != x.name)

4. Select the result that would be obtained from the following code:

SELECT name FROM bbc
 WHERE population >
       (SELECT population
          FROM bbc
         WHERE name=‘United Kingdom‘)
   AND region IN
       (SELECT region
          FROM bbc
         WHERE name = ‘United Kingdom‘)
Table-A
Andorra
Albania
Austria
Bulgaria
Table-B
France Europe
Germany Europe
Russia Europe
Turkey Europe
Table-C
France
Germany
Andorra
Albania
Table-D
France
Germany
Russia
Turkey
Table-E
France
Germany
Russia
Turkey
Brazil
United States of USA
Canada

5. Select the code that would show the countries with a greater GDP than any country in Africa (some countries may have NULL gdp values).

SELECT name FROM bbc
 WHERE gdp > ALL (SELECT MAX(gdp) FROM bbc WHERE region = ‘Africa‘ AND gdp=0)
SELECT name FROM bbc
 WHERE gdp > (SELECT MAX(gdp) FROM bbc WHERE region = ‘Africa‘)
SELECT name FROM bbc
 WHERE gdp > ALL (SELECT MIN(gdp) FROM bbc WHERE region = ‘Africa‘)
SELECT name FROM bbc
 WHERE gdp > ALL (SELECT gdp FROM bbc WHERE region = ‘Africa‘)
SELECT name FROM bbc
 WHERE gdp > ALL (SELECT gdp FROM bbc WHERE region = ‘Africa‘ AND gdp<>NULL)

6. Select the code that shows the countries with population smaller than Russia but bigger than Denmark

SELECT name FROM bbc
 WHERE population < (SELECT population FROM bbc WHERE name=‘Denmark‘)
   AND population > (SELECT population FROM bbc WHERE name=‘Russia‘)
SELECT name FROM bbc
 WHERE population < (SELECT population FROM bbc WHERE name=‘Russia‘)
   AND population > (SELECT population FROM bbc WHERE name=‘Denmark‘)
SELECT name FROM bbc
 WHERE population = (SELECT population FROM bbc WHERE name=‘Russia‘)
   AND population > (SELECT population FROM bbc WHERE name=‘Denmark‘)
SELECT name FROM bbc
 WHERE population > (SELECT population FROM bbc WHERE name=‘Russia‘)
   AND population > (SELECT population FROM bbc WHERE name=‘Denmark‘)
SELECT name FROM bbc
 WHERE population < (SELECT population FROM bbc WHERE name=‘Russia‘
   AND population > (SELECT population FROM bbc WHERE name=‘Denmark‘)

7. >Select the result that would be obtained from the following code:

SELECT name FROM bbc
 WHERE population > ALL
       (SELECT MAX(population)
          FROM bbc
         WHERE region = ‘Europe‘)
   AND region = ‘South Asia‘
Table-A
Afghanistan
Bhutan
Nepal
Sri Lanka
The Maldives
Table-B
Bangladesh
India
Pakistan
Table-C
China
India
Table-D
Brazil
Bangladesh
China
India
Table-E
France
Germany
Russia
Trukey
时间: 2024-10-07 15:51:34

mysql----Nested SELECT Quiz的相关文章

MySQL中select * for update锁表的范围

MySQL中select * for update锁表的问题 由于InnoDB预设是Row-Level Lock,所以只有「明确」的指定主键,MySQL才会执行Row lock (只锁住被选取的资料例) ,否则MySQL将会执行Table Lock (将整个资料表单给锁住). 举个例子: 假设有个表单products ,里面有id跟name二个栏位,id是主键. 例1: (明确指定主键,并且有此笔资料,row lock) SELECT * FROM products WHERE id='3' F

mysql查select基本入门

查询要素: 查哪张表的数据? 查哪些列? select * from class    -----表示查询所有行和列的数据 *代表所有列,表名后不加where条件,则选所有行,因此取所有行和列 例: 实验一: 取部分列,所有行,取所有人的姓名和工资 mysql> select sname,salary from class; 查id>10的人的所有列 mysql> select * from class where id>10; 实验二: 取部分行部分列 取id<10的人,取

(笔记)Mysql命令select:当前选择(连接)的数据库

select命令表示当前选择(连接)的数据库. select命令格式:mysql> select database(); MySQL中SELECT命令类似于其他编程语言里的print或者write,你可以用它来显示一个字符串.数字.数学表达式的结果等等.如何使用MySQL中SELECT命令的特殊功能呢? 1) 显示MYSQL的版本mysql> select version(); +-----------------------+ | version()               | +---

Mysql中select into from用法

原文:http://www.111cn.net/database/mysql/60705.htm 在mysql中用select into from一直报错, 错误:#1327 - Undeclared variable: target_test select into from要求目标表target_table不存在,因为在插入时会自动创建 后来在网上看资料说Mysql不支持select into from语句,所以用其他方法来替代select into from语句: Create targe

MySQL 使用SELECT ... FOR UPDATE 做事务写入前的确认(转)

Select…For Update语句的语法与select语句相同,只是在select语句的后面加FOR UPDATE [NOWAIT]子句. 该语句用来锁定特定的行(如果有where子句,就是满足where条件的那些行).当这些行被锁定后,其他会话可以选择这些行,但不能更改或删除这些行,直到该语句的事务被commit语句或rollback语句结束为止. MySQL  使用SELECT ... FOR UPDATE 做事务写入前的确认 以MySQL 的InnoDB 为例,预设的Tansactio

MySQL中SELECT+UPDATE处理并发更新问题解决方案

这篇文章主要介绍了MySQL中SELECT+UPDATE处理并发更新问题解决方案分享,需要的朋友可以参考下. 问题背景 假设MySQL数据库有一张会员表vip_member(InnoDB表),结构如下: 当一个会员想续买会员(只能续买1个月.3个月或6个月)时,必须满足以下业务要求: 如果end_at早于当前时间,则设置start_at为当前时间,end_at为当前时间加上续买的月数 如果end_at等于或晚于当前时间,则设置end_at=end_at+续买的月数 续买后active_statu

全面掌握Mysql及select支持的运算操作

转载请注明出处:http://blog.csdn.net/lhy_ycu/article/details/45725317 花了2天时间整理了一下Mysql中的常用命令及select支持的运算操作,希望这篇博客对大家有所帮助. 内容提纲 1.登录mysql 2.查看mysql信息 3.查看所有数据库 4.打开指定数据库 5.李处指定数据库中的所有表 6.查看指定表的结构 7.查看建表语句 8.建库建表 9.修改指定表结构 10.查看数据库的编码方式 11.DML语句(CRUD)操作 12.注释方

【Mysql】select

描述 The SELECT statement is used to select data from a database. The result is stored in a result table, called the result-set. 大意:select用来从数据库中挑选数据 有以下两种形式 SELECT column_name,column_name FROM table_name; 和 SELECT * FROM table_name; 下面是测试代码 #include <

MySQL:select command denied to user for table ‘proc‘案例

原文:MySQL:select command denied to user for table 'proc'案例 使用EMS MySQL Manager Pro(3.4.0.1)连接MySQL 5.6.20时,报错:"SELECT command denied to user xxx@xxx.xxx.xxx.xxx for table 'proc' 很是纳闷,后面使用同样的权限,发现使用命令工具mysql -h xxxx -u username -p连接时不会报错.个人猜测是因为EMS MyS