1、聚合函数,avg求平均值:
select avg(字段名) from 表名 where 条件;
select avg(all 字段名) from 表名 where 条件; #同上
select avg(distinct 字段名) from 表名 where 条件; #distinct只对不同的值求平均值
2、聚合函数,sum求和:
select sum(字段名) from 表名 where 条件;
3、聚合函数,max最大值:
select max(字段名) from 表名 where 条件;
4、聚合函数,min最小值:
select min(字段名) from 表名 where 条件;
5、聚合函数,count求行数:
select count(字段名) from 表名 where 条件;
6、查询前10条数据:
select t.列名,t.* from 表名 t where rownum<=10 order by t.列名 asc; #order by asc升序排序
select t.列名,t.* from 表名 t where rownum<=10 order by t.列名 desc; #order by desc降序排序
还有一种写法,先按照升序排序,再查询出前10列(效率比上一种低,有说法这种排序比较准确,个人实际执行后查询结果是一样的):
select * from (select t.列名 from 表名 t order by t.列名 asc)where rownum<=10 ;
7、左连接,左表都展示,右表仅展示可以匹配左表的列,其余列显示空:
select * from 表名A a left join 表名B b on a.列名 = b.列名;
8、右连接,右表都展示,左表仅展示可以匹配右表的列,其余列显示空:
select * from 表名A a right join 表名B b on a.列名 = b.列名;
9、内连接,仅展示左表、右表可以匹配的列:
select * from 表名A a inner join 表名B b on a.列名 = b.列名;
10、全连接,展示所有的行:
select * from 表名A a full outer join 表名B b on a.列名 = b.列名;
11、dual表查日期:
select sysdate from dual;