学习Oracle的分组函数和子查询

一,分组函数:

分组函数是作用于一组数据,并对一组数据返回一个值。

例如 AVG,COUNT,MAX,MIN,SUM,STDDEV这些都是常见的分组函数。

 

count(exp)  返回exp不为空的记录总数。

count(distinct exp) 返回exp非空且不重复的记录总数。

 

在组函数中使用NVL函数:

NVL函数使分组函数无法忽略空值。

 

例如:

SELECT AVG(NVL(commeseon_con,0))  FROM  employess;

 

分组数据:group by 子句

使用group by 子句可以使表中数据分为若干组。

 

(1).在select 列表中所有未包含在组函数中的列都应该包含在group by 子句中,例如:

select department_id ,department_name,avg(salary)

from employees

group by department_id,department_name;

 

(2).包含在group by子句中的列不比包含在select列表中,例如:

select   avg(salary)

from employees

group by department_id

 

(3)不能再where子句中使用组函数,可以在having子句中使用子函数

 

 

过滤分组:having子句

使用having分组

1,行以被分组

2,使用了组函数

3,满足having子句条件的分组呗显示出来

 

例如:

select department_id,max(salary)

from employees

group by department_id

having  max(salarg)>10000;

 

嵌套组函数:

 

举个例子吧:

select max(avg(salarg)) from employees  group by department_id;

 

 

二,子查询

子查询在主查询之前一次执行完成,子查询的结果被主查询使用。

例如:

select last_name

from employees

where salary>

       (select salary

        from employees

         where last_name='aNc'

         );

注意:

1,子查询要包含在括号内

2,将子查询放在比较条件的右侧

3,单行操作符对应单行子查询,多行操作符对应多行子查询。

 

单行子查询:

1,只返回一行

2,使用单行操作符 :=    ,  <  , <= , > , >= , <>

 

例如:

select last_name,job_id,salary

from employees

where job_id =

                            (select job_id

                              from employees

                             where employee_id = 111)

and salary >

                         (select salary

                          from employees

                          where employee_id = 121) ;

在子查询中也可以使用组函数,例如:

select last_name,job_id,salary

from employees

where  salary=

                          (select min(salary)

                            from employees

                            );

 

子查询中的having子句

1,首先执行子查询

2,向主查询的having子句返回结果

例如:

select department_id,min(salary)

from employees

group by department_id

having min(salary) >

                              (select salary

                               from employees

                                where department_id =33);

 

非法使用子查询

例如:

select department_id,min(salary)

from employees

group by department_id

having min(salary) =

                              (select salary

                               from employees

                              group by department_id); //子查询返回多个值

子查询如果为空值,就不返回任何值。

 

 

 

 

多行子查询:

1,返回多行

2,使用多行比较操作符  例如:in   all   any

 

 

在多行子查询中用any

select employees_id,last_name,job_id,salary

from employees

where salary <  any

                                       (select salary

                                         from employees

                                         where job_id='sadsa')

and  job_id<>'sadsa';

 

在多行子查询中用all

select employees_id,last_name,job_id,salary

from employees

where salary <  all

                                       (select salary

                                         from employees

                                         where job_id='sadsa')

and  job_id<>'sadsa';

 

 

 

向表中插入空值是

 

1,隐式方式:在列明表中省略该列的值

例如: insert into department(department_id,department_name)  values(1,'asdas');

 

2,显示方式:在values子句中指定空值,

例如:insert into department   values(100,'asda',null,null);

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值