SQL_2

SQL_2_第1张图片
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;

你可能感兴趣的:(SQL_2)