Mysql select查询练习

Mysql select查询练习_第1张图片

 1、查询出部门编号为30的所有员工:

select * from emp where deptno=30;

 2、所有销售员的姓名、编号和部门编号:

select ename,empno,deptno from emp where job='销售员';

3、找出奖金高于工资的员工:

 select * from emp where comm > sai;

4、找出奖金高于工资60%的员工:

 select * from emp where comm > (sai*0.6);

5、找出部门编号为10中所有经理,和部门编号为20中所有销售员的详细资料:

select * from emp where (deptno=10 and job='经理') or (deptno=20 and job='销售员');

6、找出部门编号为10中所有经理,部门编号为20中所有销售员,还有即不是经理又不是销售员但其工资大于或等于20000的所有员工详细资料:

select * from emp where (deptno=10 and job='经理') or (deptno=20 and job='销售员') or (job not in ('经理','销售员') and sai>=20000 );

7、无奖金或奖金低于1000的员工:

select * from emp where comm is null or comm<1000;

8、查询名字由三个字组成的员工:

 select * from emp where length(ename)=9;

9、查询2000年入职的员工:

 select * from emp where year(hiredate)=2000;

10、查询所有员工详细信息,并按编号升序排序:

select * from emp order by deptno;

11、查询所有员工详细信息,按工资降序排序,如果工资相同则按入职日期升序排序:

select * from emp order by sai DESC , hiredate ASC;

12、查询每个部门的平均工资:

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

13、查询每个部门的雇员数量:

 select deptno,count(1) as 雇员数量 from emp group by deptno;

14、查询每种工作的最高工资、最低工资、人数:

select job,max(sai) as 最高工资,min(sai) as 最低工资,count(1) as 人数 from emp group by job;

Mysql select查询练习_第2张图片  

 1、查询每个部门的所属员工

select name,group_concat(ename) from emp3 e,dept3 d where e.dept_id=d.deptno group by

name;

2、查询研发部门的所属员工

  select name,group_concat(ename) from emp3 e,dept3 d where e.dept_id=d.deptno and

name="研发部" group by name;

3、查询研发部和销售部的所属员工

select name,group_concat(ename) from emp3, dept3 where emp3.dept_id=dept3.deptno and

name in ('研发部','销售部')group by name;

4、查询每个部门的员工数,并升序排序

select name,count(ename) from emp3 e,dept3 d where e.dept_id=d.deptno group by name order

by count(ename) asc;

5、查询人数大于等于3的部门,并按照人数降序排序

select name,count(ename) from emp3 e,dept3 d where e.dept_id=d.deptno group by name

having count(ename)>=3 order by count(ename) desc;
 

你可能感兴趣的:(mysql,数据库)