SQL_2

Fig. 1

1. select countrycode, avg(population) from world.city group by countrycode having avg(population)>=100000; 

2. select countrycode, avg(population) from world.city where population>=100000 group by countrycode;

3. select a.district from (select district, country from world.city where population>=100000) as a;

4. select name,ID, (select name from world.country world.country.code = world.countrycode) worle_name from world.city;

5.select a.id,a.name,b.name, b.gnp from

 (select id,name,countrycode from world.city where population>100000) as a

inner join #还可以left join,right join,但返回值有区别

(select code,name,gnp from world.country) as b

on a.countrycode = b.code;

6. select countrycode from world.city

union

select code from world.code;

#alter table world.country modify column population bigint; 修改数据格式

7. use world;

create table if not exists  world.xiaoming_test1 as

select a.*,b.*from #注意是否有重名,如有,要重命名

(select * from world.city) as a

left join 

(select * from world.country) as b

on a.countrycode = b.code;

left join 

(select * from world.language) as  c

on a.countrycode = c.countrycode;

8. drop talbe if exists world.xiaoming_test1;

9. create table world.xiaoming_test2

(id int,

countrycode char(5),

population int,

gnp float,

language char(15),

priamary key(id) #主键不能重复,如有,换或者多个,或没有主键

);

10. Insert table world.xiaoming_test2

select a.id,a.name as name_a, a.countrycode, c.gnp, c.language #注意是否有重名,如有,要重命名

(select * from world.city) as a

left join 

(select * from world.country) as b

on a.countrycode = b.code;

11. alter table 

left join 

(select * from world.language) as  c

on a.countrycode = c.countrycode;

12. alter table world.xiaoming_test2

add new_column char(15);

13. alter table world.xiaoming_test2

drop gnp;

14. update table world.xiaoming_test2

set population = 1

where gnp is null;

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,418评论 0 10
  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 9,786评论 0 23
  • pyspark.sql模块 模块上下文 Spark SQL和DataFrames的重要类: pyspark.sql...
    mpro阅读 9,487评论 0 13
  • 上节重点难点回顾: 1. 数据类型 2. 列属性 primary key unique not null defa...
    张鑫泽_2109阅读 503评论 0 0
  • 介绍多表查询等复杂SQL语句。 关系数据库的查询结果都是一个结果表(也是关系) 集聚函数 基本语法 统计元组个数C...
    zealscott阅读 846评论 0 0