数据库高级查询2

分组查询(group by)

分组查询:

1.查询每个部门的最高工资

select deptno,max(sal) from emp group by deptno;

2.查询每个职位的平均工资

select deptno,avg(sal) from emp group by deptno;

3.查询每个部门的人数

select deptno,count(*) from emp group by deptno;

4.查询工资大于1000的员工,每个部门的最大工资

select deptno,max(sal) from emp where sal>1000 group by deptno;

多字段分组查询:group by 字段1,字段2;

1.查询每个部门下每个主管的手下人数

select deptno, mgr, count(*) from emp where mgr is not null group by deptno,mgr;

2.查询emp表中每个部门的编号,人数,工资总和,最后根据人数进行升序排序,如果人数一致,根据工资总和降序排序

select deptno,count(*),sum(sal) from emp group by deptno order by count(*) asc,sum(sal) desc;

3.查询工资在1000-3000之间的员工信息,每个部门的编号,平均工资,最低工资,最高工资,根据平均工资进行升序排序排列

select deptno,avg(sal),min(sal),max(sal) from emp where sal between 1000 and 3000 group by deptno order by avg(sal)

4.查询含有上级领导的员工,每个职业的人数,工资的总和,平均工资,最低工资,最后根据人数进行降序排列,如果人数一致,根据平均工资进行升序排列

select job, count(*),avg(sal),min(sal) from emp where mgr is not null group by job order by count(*) desc,avg(sal) asc;

各种关键字的顺序

select * from 表名  where .... group...having... order by... limit...

    having(结合group by使用)

having一般要结合分组查询和聚合函数使用,用于给聚合函数的内容添加条件

聚合函数的条件不能写在where后面

普通字段的条件写在where后面,聚合函数的条件写在having后面

1.查询每个部门的平均工资,要求平均工资大于2000(c是别名的用法)

select deptno,avg(sal) c from emp group by deptno having c >2000;

2.查询每个分类的平均单价,要求平均单价低于100

select category_id ,avg(price) a from t_item group by category_id having a<100;

3.查询category_id分类为238和917的两个分类的平均单价

select category_id,avg(price) from t_item where category_id in(238,917) group by category_id;

4.查询emp表中每个部门的平均工资高于2000的部门编号,部门人数,平均工资,最后根据平均工资降序排列

select deptno,count(*),avg(sal) a from emp group by deptno having a>2000 order by a desc;

5.查询emp表中工资在1000-3000之间的员工,每个部门编号,工资总和,平均工资,过滤掉平均工资低于2000的部门,按照平均工资进行升序排序

select deptno,sum(sal), avg(sal) a from emp where sal between 1000 and 3000 group by deptno having a>=2000 order by a asc;

6.查询emp表中每年入职的人数

select extract(year from hiredate) year,count(*) from emp group by year;

7.查询每个部门的最高平均工资

select deptno,avg(sal) from emp group by deptno order by avg(sal) limit 0,1;

子查询(嵌套查询)

子查询可以写在where或having后面当做查询条件的值

写在from后面,当做一张新表(但是必须要有别名)

select ename from (select * from emp where sal>1000) newtable;

写在创建表的时候

create table emp_20 as (select * from emp where deptno=20);

1.查询emp表中工资最高的员工信息

select * from emp where sal=(select max(sal) from emp);

2.查询emp表中工资大于平均工资的所有员工的信息

select * from emp where sal>(select avg(sal) from emp);

3.查询工资高于20号部门最大工资的员工信息

select * from emp where sal>(select max(sal) from emp where deptno=20);

4.查询工资高于20号部门最大工资的员工信息

select * from emp where sal>(select avg(sal) from emp);

5.查询和Jones相同工资的其他员工信息

select * from emp where job=(select job from emp where ename='jones' and ename!='jones');

6.查询工资最低的员工的同事们的信息(同事=相同job)

select * from  emp where job=(select job from emp where sal=(select min(sal) from emp)) and sal !=(select min(sal) from emp);

7.查询最晚入职的员工信息

select * from emp where hiredate=(select max(hiredate) from emp);

8.查询名字为King的部门编号和部门名称(需要用到dept表)

select deptno,dname from dept where deptno=(select deptno from emp where ename='king');

9.查询有员工的部门信息(编号和名称)

select deptno ,dname from dept where deptno in (select distinct deptno from emp);

10.查询平均工资最高的部门信息

select * from dept where deptno in (select deptno from emp group by deptno having avg(sal)=(select avg(sal) from emp group by deptno order by avg(sal) desc limit 0,1));

关联查询

同时查询多张表的数据称为关联查询

1.查询每一个员工的名称和其对应的部门名称

select e.ename,d.dname from emp e,dept d where e.deptno=d.deptno;

2.查询在new york工作的所有员工的信息

select  e.* from emp  e,dept  d  where e.deptno=d.deptno and  d.loc='new york';

笛卡尔积

如果关联查询不写关联关系则查询到的数据是两张表的乘积,这个乘积称为笛卡尔积,笛卡尔是一种错误查询方式的结果,工作切记不要出现.

    等值连接和内连接

等值连接:

select * from A,B where A.x=B.x and A.age=18;

内连接:

select * from A join B on A.x=B.x where A.age=18;(将关联关系写在on后面)

1.查询每个员工的名称和其对应的部门名称

select e.ename,d.dname from emp e join dept d on e.deptno=d.deptno;

外连接

使用外连接查询得到的数据层除了两张表的交集数据以外和另外一张主表的全部数据,哪个表为主表通过left/rigth控制, left以join左边表为主表 rigth以join右边表为主表

1.查询所有员工的名称和其对应的部门名称

select e.ename,d.dname from emp e left join dept d on e.deptno=d.deptno;

你可能感兴趣的:(数据库高级查询2)