数据分析中常用的SQL语句

主要目的

汇总一些分析型SQL操作,为数据分析师们做一些基本的命令总结。包括关系型以及非关系型中的一些数据库,MySQL-Oracle--Hbase-Hive-Spark SQL

数据分析中常用的SQL语句

关系型数据库SQL–MySQLOrcale

基本查看
--查看数据库,表名和表结构-表数据和总数
show status
show databases;
use   your_database;
show tables;
DESC your_table;
查询单列或者多列
select user_birthday from user
查看行以及行数
select * from your_table limit 3;
计数:
select count(*) from your_table
排序

1.只排一列

select user_birthday from user order by user_birthday;
select user_birthday from user order by user_birthday DESC;  

2.多列排序,升序ASC是默认的

select * from user order by user_id,user_birthday;
select * from user order by user_id DESC,user_birthday;
select * from user order by user_id ,user_birthday DESC;
筛选以及过滤

过滤行以及查找 where in not like

select user_birthday from user where id>3;
select user_birthday from user where id like '3%';
去重
 select distinct  user_birthday from user
字符串和数值操作

拼接:select name||id as tiele from tableA ;
求和:select sum(field1) as sumvalue from tableA
平均:select avg(field1) as avgvalue from tableA
最大:select max(field1) as maxvalue from tableA
最小:select min(field1) as minvalue from tableA

分组和汇总
select   count(*)       from tableA  group by  sex;
select   id,count(*)    from tableA  group by  sex;
过滤分组
select   id,count(*)  from tableA  group by  sex having count(*)>2;
嵌套查询
select   name,sex,id  from tableA where id  in (select   id  from tableB where id >3)
联结查询
--以列为单位对表进行联结
select   name,sex,id  from tableA ,tableB  where tableA.id = tableB.id;
select   name,sex,id  from tableA  inner join    tableB  on tableA.id = tableB.id;
select   name,sex,id  from tableA ,tableB,tableC  where tableA.id = tableB.id and  tableC.hot = tableB.hot;
组合查询
--以行位单位对表进行操作
select   name,sex,id  from tableA   union       select   name,sex,id  from tableB  where tableB.id>3;
select   name,sex,id  from tableA   union all   select   name,sex,id  from tableB  where tableB.id>3;
使用索引以及存储过程

说明

  • join 是两张表做交连后里面条件相同的部分记录产生一个记录集,
  • union是产生的两个记录集(字段要一样的)并在一起,成为一个新的记录集

帮助系统

help
help indexindex

退出

exit

注意:
MySQL和Orcale有不一样的地方

除了多表关联查询,子查询以及表集合查询外,还有层次查询

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值