
DAY3 练习
UsingnestedSELECT/zh: https://sqlzoo.net/wiki/Using_nested_SELECT/zh
SELECT within SELECT Tutorial/zh:https://sqlzoo.net/wiki/SELECT_within_SELECT_Tutorial/zh
The nobel table can be used to practice more subquery./zh: https://sqlzoo.net/wiki/The_nobel_table_can_be_used_to_practice_more_subquery./zh
NestedSELECTQuiz/zh: https://sqlzoo.net/wiki/Nested_SELECT_Quiz/zh
--Using nested SELECT/zh--1select namefrom worldwhere continent = (select continentfrom worldwhere name = 'Brazil');--2selectname,continentfrom worldwhere continent in ((select continentfrom worldwhere name = 'Brazil'),(select continentfrom worldwhere name = 'Mexico'));/*另一种解法*/selectname,continentfrom worldwhere continent in (select continentfrom worldwhere name = 'Brazil'or name = 'Mexico');--3select population/(select populationfrom worldwhere name = 'United Kingdom')from worldwhere name = 'China';--4select namefrom worldwhere population > all(select populationfrom worldwhere continent = 'Europe');--SELECT within SELECT Tutorial--1select namefrom worldwhere population > (select populationfrom worldwhere name = 'Russia');--2select namefrom worldwhere continent = 'Europe'and gdp/population > (select gdp/populationfrom worldwhere name = 'United Kingdom');--3selectname,continentfrom worldwhere continent in((select continentfrom worldwhere name = 'Argentina'),(select continentfrom worldwhere name = 'Australia'))order by name;--4selectname,populationfrom worldwhere population between(select populationfrom worldwhere name = 'Poland')and(select populationfrom worldwhere name = 'Canada');/*介于两个值之间用between and ,大的值在and前,小的值在and后*/--5selectname,concat(round(population/(select populationfrom worldwhere name = 'Germany')*100,0),'%')from worldwhere continent = 'Europe';/*没有直接用给出的德国人口数*/--6select namefrom worldwhere gdp > all(select gdpfrom worldwhere continent = 'Europe'and gdp is not null);/*判空请用 is not null,不要使用!=和<>永远会返回0行,却不会提示语法错误的,所以我们要牢记:默认情况下做比较条件时使用关键字“is null”和“is not null”*//*另一种解法*/select namefrom worldwhere gdp > all(select gdpfrom worldwhere continent = 'Europe'and gdp > 0);--7selectcontinent,name,areafrom world xwhere area >=all(select areafrom world ywhere y.continent = x.continentand area is not null);/*此题用到了关联子查询,关联子查询首先依靠子查询中的关联语句,从主查询中选取关联属性的第一个值,进入子查询中,然后将子查询结果返回主查询,判断where条件,依次类推,直到主查询中关联属性的值选取完毕。*/--8selectcontinent,namefrom world xwhere name <= all(select namefrom world ywhere y.continent = x.continent);--9selectname,continent,populationfrom world xwhere 25000000 >= all(select populationfrom world ywhere y.continent = x.continent);/*这道题看英文,中文翻译的不是Humanlanguage啊*/--10selectname,continentfrom world xwhere population/3 >= all(select populationfrom world ywhere y.continent = x.continentand y.name != x.name);/*这道题别忘了把所选国家自己排除掉!*/--The nobel table can be used to practice more subquery./zh--1selectwinner,yrfrom nobelwhere yr in (select yrfrom nobelwhere winner = 'International Committee of the Red Cross')and subject = 'Literature';/*这道题刚开始报错,想了好久,获奖可能不止一次,所以年份可能不唯一,这里要用in,而不是=,请注意in和=的区别*/--2select winnerfrom nobelwhere yr in (select yrfrom nobelwhere winner = 'Toshihide Maskawa')and subject = 'physics'and winner != 'Toshihide Maskawa';/*别忘了把Toshihide Maskawa排除!*/--3select winnerfrom nobel xwhere subject = 'Economics'and yr <= all(select yrfrom nobel ywhere y.subject = x.subject);--4select distinct yrfrom nobelwhere subject = 'physics'and yr not in (select yrfrom nobelwhere subject = 'chemistry');/*别忘了年份去重*/--5selectyr,subject,winnerfrom nobelwhere yr in(select yrfrom nobelgroup by yrhaving count(winner) > 12);/*可能不止一年里得奖人数超过12人,所以要用in*/--6selectwinner,yr,subjectfrom nobelwhere winner in (select winnerfrom nobelgroup by winnerhaving count(winner) >1)order by winner, yr;/*可能不止一人,获奖超过1次,所以用in*/
end
文章转载自EGG IXD,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




