/1:查询没有上级领导的员工的编号,姓名,工资/
select e.employee_id,e.first_name,e.salary from employees e where e.manager_id is null
/2:查询emp表中没有奖金的员工的姓名,职位,工资,以及奖金/
select e.first_name,e.job_id,e.salary ,e.commission_pct from employees e
where e.commission_pct is null or e.commission_pct = 0
/3:查询emp表中含有奖金的员工的编号,姓名,职位,以及奖金/
select e.first_name,e.job_id,e.salary ,e.commission_pct from employees e
where e.commission_pct is not null or e.commission_pct <> 0
/4:查询含有上级领导的员工的姓名,工资以及上级领导的编号/
select e.first_name,e.salary,e.manager_id from employees e
where e.manager_id is not null
/5:查询emp表中名字以‘S’开头的所有员工的姓名/
select CONCAT(first_name,last_name) from employees
where CONCAT(first_name,last_name) like ‘s%’
/6:查询emp表中名字的最后一个字符是’S’的员工的姓名/
select CONCAT(first_name,last_name) from employees
where CONCAT(first_name,last_name) like ‘%s’
/7:查询倒数的第2个字符是‘E’的员工的姓名/
select CONCAT(first_name,last_name) from employees
where CONCAT(first_name,last_name) like ‘%e_’
/8:查询emp表中员工的倒数第3个字符是‘N’的员工姓名/
select CONCAT(first_name,last_name) from employees
where CONCAT(first_name,last_name) like ‘%n__’
/9:查询emp表中员工的名字中包含‘A’的员工的姓名/
select CONCAT(first_name,last_name) from employees
where CONCAT(first_name,last_name) like ‘%A%’
/10:查询emp表中名字不是以’K’开头的员工的所有信息/
select CONCAT(first_name,last_name) from employees
where CONCAT(first_name,last_name) not like ‘K%’
/11:查询emp表中名字中不包含‘A’的所有员工的信息/
select CONCAT(first_name,last_name) from employees
where CONCAT(first_name,last_name) not like ‘%A%’
/12:查询文员的员工人数量是多少(job= CLERK 的)/
select count(1) from employees where job_id=‘AD_VP’
/13:销售人员 job: AD_VP 的最高薪水/
select max(salary) from employees where job_id=‘AD_VP’
/14.最早和最晚入职时间/
select MIN(hiredate) ,max(hiredate)from employees
/15:查询emp表中员工的编号,姓名,职位, 工资,并且工资在10000~20000之间。/
select employee_id,first_name,job_id,salary from employees
where salary >= 10000 and salary <= 20000
/16:查询emp表中员工在20号部门,并且含有上级领导的员工的姓名,职位,
上级领导编号以及所属部门的编号/
select e.first_name,e.job_id,e.manager_id,e.department_id from employees e
where e.department_id=20 and e.manager_id is not null
/17:查询emp表中名字中包含’E’,并且职位不是AD_VP的员工的编号,姓名,职位,以及工资/
select CONCAT(e.first_name,e.last_name),e.department_id,e.job_id,e.salary from employees e
where CONCAT(e.first_name,e.last_name) like ‘%e%’ and e.job_id <> ‘AD_VP’
/18.查询emp表中10号部门或者20号部门中员工的编号,姓名,所属部门的编号/
select e.department_id,CONCAT(e.first_name,e.last_name),e.department_id from employees e
where e.department_id in (10,20)
/19.查询emp表中没有奖金或者名字的倒数第2个字母不是T的员工的编号,姓名,职位以及奖金/
select e.department_id,CONCAT(e.first_name,e.last_name),e.job_id,e.commission_pct from employees e
where CONCAT(e.first_name,e.last_name) not like ‘%t_’ or e.commission_pct is null
or e.commission_pct = 0
/20:查询工资高于3000或者部门编号是30的员工的姓名,职位,工资,入职时间以及所属部门的编号/
select CONCAT(e.first_name,e.last_name),job_id,salary,e.hiredate,e.department_id from employees e
where e.salary>3000 or e.department_id=30
/21:查询不是30号部门的员工的所有信息/
select * from employees where department_id <> 30 or department_id is null
/22:查询奖金不为空的员工的所有信息/
select * from employees where commission_pct is not NULL or commission_pct <> 0
/23:查询emp表中所有员工的编号,姓名,职位,根据员工的编号进行降序排列/
select e.employee_id,CONCAT(e.first_name,e.last_name),e.job_id
from employees e
order by e.employee_id desc
/24:查询emp表中部门编号是10号或者30号中,所有员工姓名,职务,工资,根据工资进行升序排列/
select CONCAT(e.first_name,e.last_name),e.job_id,e.salary from employees e
where department_id in (10,30)
order by e.salary
/25:查询emp表中所有的数据,然后根据部门的编号进行升序排列,如果部门编号一致,
根据员工的编号进行降序排列/
select * from employees e
order by e.department_id,e.employee_id DESC
/26:查询emp表中工资高于10000或者没有上级领导的员工的编号,姓名,工资,
所属部门的编号,以及上级领导的编号,根据部门编号进行降序排列,
如果部门编号一致根据工资进行升序排列。/
select e.employee_id,CONCAT(e.first_name,e.last_name),e.salary,
e.department_id,e.manager_id from employees e
where e.salary > 10000 or e.manager_id is null
order by e.department_id desc,e.salary
/27:查询emp表中名字中不包含S的员工的编号,姓名,工资,奖金,根据工资进行升序排列,
如果工资一致,根据编号进行降序排列/
select empno,ename,sal,comm from emp
where ename not like ‘%s%’
order by sal,empno desc
/28.求出emp表中员工的最高工资/
select count(*),max(sal),avg(sal),min(sal),sum(sal),sum(IFNULL(comm,0)) from emp
/29:查询工资在1000~3000之间每一个员工的编号,姓名,职位,工资/
select empno,ename,job,sal from emp
where sal BETWEEN 1000 AND 3000
/30:查询员工的编号是7369,7521/
select * from emp where empno in (7369,7521)
/31:查询emp表中,职位是ANALYST/
select * from emp where job=‘analyst’
/32:查询每个部门的最高工资/
select MAX(sal),deptno from emp group by deptno
/33:查询每个部门的人数/
select count(*),deptno from emp group by deptno
/34:查询工资大于1000的员工每个部门的最大工资/
select MAX(sal),deptno from emp
where sal > 1000 group by deptno
/35:查询每个领导(主管)的手下人数/
select mgr,count(1) from emp where mgr is not null group by mgr
/36:查询每个部门下每个主管的手下人数/
select deptno,mgr,count(1) from emp
where mgr is not null group by deptno,mgr
/37:查询emp表中每个部门的编号,人数,工资总和,最后根据人数进行升序排列,
如果人数一致,根据工资总和降序排列。/
select deptno,count(1),SUM(sal) from emp
group by deptno
order by count(1),sum(sal) desc
/38:查询工资在1000~3000之间的员工信息,每个部门的编号,平均工资,最低工资,最高工资,
根据平均工资进行升序排列。/
select deptno,AVG(sal),MIN(sal),max(sal)
from emp
where sal >= 1000 and sal <= 3000
group by deptno
order by avg(sal)
/*39:查询含有上级领导的员工,每个职业的人数,工资的总和,平均工资,最低工资,最后根据
人数进行降序排列,如果人数一致,根据平均工资进行升序排列 */
select count(1),SUM(sal),AVG(sal),min(sal)
from emp where mgr is not null
group by job
order by count(1) desc,avg(sal) asc
/40:查询每个部门的平均工资要求平均工资大于2000/
select deptno,avg(sal) from emp
group by deptno
having avg(sal) > 2000
/41:查询emp表中每个部门的平均工资高于2000的部门编号,部门人数,平均工资,
最后根据平均工资降序排序/
select deptno,count(1),avg(sal) a from emp
group by deptno having avg(sal) > 2000
order by a desc
/42:查询emp表中工资在1000-3000之间的员工,每个部门的编号,工资总和,平均工资,
过滤掉平均工资低于2000的部门,按照平均工资进行升序排序。/
select deptno,SUM(sal),AVG(sal) from emp
where sal BETWEEN 1000 AND 3000
group by deptno having avg(sal) >= 2000
order by avg(sal)
/43:查询emp表中不是以s开头,每个职位的名字,人数,工资总和,最高工资,过滤掉平均工资是
3000的职位,根据人数升序排序,根据工资总和降序排序/
select job,count(),SUM(sal),MAX(sal) from emp
where ename not like ‘s%’
group by job having avg(sal) <> 3000
order by count(),sum(sal) desc
注:group by是分组,order by 是排序
/44:查询emp表中每年入职的人数/
select year(hiredate),count(1) from emp group by year(hiredate)
/45:查询平均工资最高的部门编号以及该部门的平均工资/
select deptno,avg(sal)
from emp group by deptno
order by avg(sal) desc limit 1
/46:查询emp表中工资最高的员工信息/
select MAX(sal) from emp
select * from emp where sal=5000
select * from emp where sal = (
select max(sal) from emp
)
/47:查询emp表中工资大于平均工资的所有员工的信息/
select * from emp where sal > (
select avg(sal) from emp
)
/48:查询工资高于20号部门最大工资的员工信息/
select MAX(sal) from emp where deptno=20
select * from emp where sal>3000
select * from emp where sal > (
select MAX(sal) from emp where deptno=20
)
/49:查询和Jones相同工作的其他员工信息/
select * from emp where job in (
select job from emp where ename=‘jones’
) and ename <> ‘jones’
/50:查询工资最低的员工的同事们的信息/
select * from emp,(select job,ename from emp
order by sal asc limit 0,1) a
where emp.job = a.job and emp.ename <> a.ename
/51:查询名字为king的部门编号和部门名称/
select dept.deptno,dname from dept,emp
where dept.deptno=emp.deptno and ename=‘king’
/52:查询有员工的部门信息(编号和部门名称)/
select dept.deptno,dname from dept,(
select DISTINCT deptno
from emp where deptno is not null
) a where dept.deptno=a.deptno
/53:查询平均工资最高的部门信息/
select * from dept where deptno = (
select deptno from emp group by deptno
order by avg(sal) desc limit 0,1)
/54:每个部门的人数,根据人数排序/
select deptno,count(1) from emp
group by deptno
order by count(1)
/55:每个部门中,每个主管的手下人数/
select count(1),deptno,mgr from emp
where mgr is not null
group by deptno,mgr
/56.少于等于3个人的部门信息/
select deptno,dname,loc from dept where deptno in (
select deptno from emp
group by deptno having count(1) <= 3)
/57:只有一个下属的主管信息/
select emp.* from emp,(select count(1),mgr from emp
where mgr is not null
group by mgr
having count(1) = 1) a
where emp.empno = a.mgr
/58:查询员工信息和他的主管姓名/
select e.ename,m.ename from emp e left join emp m on e.mgr = m.empno
/59:查询emp表中所有员工的姓名以及该员工上级领导的编号,姓名,职位,工资/
select e.ename,m.empno,m.ename,m.job,m.sal from
emp e left join emp m on e.mgr = m.empno
/60:查询每个城市员工的工资总和/
select IFNULL(sum(emp.sal),0),loc
from dept left join emp
on dept.deptno = emp.deptno group by loc
/61.查看表结构/
CREATE TABLE temp(
id INT,
name VARCHAR(10),
age VARCHAR(4)
);
INSERT INTO temp VALUES(1,‘王阳’,18);
desc temp
/62.增加表temp的字段:birth/
alter table temp add birth date
/*63.删除表temp的字段:birth */
alter table temp drop birth
/64.修改字段age类型为varchar(20)/
alter table temp modify age varchar(20)
/65.按条件年龄为2,姓名为"阿三"的将id修改为1002,工资为2000/
update temp set id=1002,salary=2000
where age=2 and name=‘阿三’
/66.统计部门号为20和30的员工共有多少人/
select COUNT(1) from emp where deptno in (20,30)
/67.统计部门号不是20和30的所有员工个数/
select COUNT(*) from emp
where (deptno <> 20 and deptno <> 30) or
deptno is null
/68.查看emp表中的员工姓名和姓名的长度/
select ename,LENGTH(ename) from emp
/*69.将字段ename右对齐补上字符’ ′ , 整 体 长 度 为 20 ∗ / s e l e c t R P A D ( e n a m e , 20 , " ',整体长度为20*/ select RPAD(ename,20," ′,整体长度为20∗/selectRPAD(ename,20,"") from emp
/70.查询不是部门10的,底薪大于等于2000的员工姓名,底薪,部门号/
select ename,sal,deptno from emp
where deptno <> 10 and sal >= 2000
/71.查询姓名第三个字母为A或第一个字母为s的,部门编号为30的员工/
select * from emp where (ename like ‘__a%’ or ename like ‘s%’) and deptno=30
/72.查询姓名不是’KING’,‘CLARK’,'SMITH’的员工信息/
select * from emp where ename not in (
‘king’,‘clark’,‘smith’)
/73.查询底薪小于1500或者是大于2500的员工信息/
select * from emp where sal < 1500 or sal > 2500
/74.查询年薪大于等于228000员工,显示员工的姓名和年薪/
select e.first_name, e.salary * 12 from employees e where e.salary * 12 >= 228000
/75.查询emp表中有哪些职位/
select DISTINCT job from emp
/76.查询所有员工的年薪,姓名,部门号,按照年薪降序,部门编号升序/
select sal12,ename,deptno from emp
order by sal12 desc,deptno asc
/77.查询最高底薪,最早入职的时间/
select MAX(sal),MIN(hiredate) from emp
/78.查询每个部门中的最高底薪,最低奖金,
平均底薪,部门的总底薪,按照部门排序/
select MAX(sal),MIN(comm),AVG(sal),SUM(sal) from emp
group by deptno order by deptno
/79.统计底薪大于2000的员工所在部门的平均底薪/
select AVG(sal),deptno from emp where sal > 2000
group by deptno
/80.查询销售部门’sales’的所在地,以及部门内员工的姓名,入职日期。/
select ename,hiredate,loc from emp e,dept d
where e.deptno = d.deptno and d.dname=‘sales’
/81.查询sales部门和research部门的平均底薪/
select avg(sal),e.deptno from emp e,dept d
where e.deptno = d.deptno and d.dname in (‘sales’,‘research’)
group by e.deptno
/82.查询不是领导的员工姓名/
select ename from emp where
empno not in (select DISTINCT mgr from emp where mgr is not null )
/83.查询superman领导的名字/
select m.ename from emp e,emp m
where e.mgr = m.empno and e.ename=‘superman’
/84.查询StevenK_ing领导的下属的信息/
select * from employees e where e.manager_id=(
select employee_id from employees where CONCAT(first_name,last_name) = ‘StevenK_ing’);
/85.查询research部门的最高底薪,最低底薪,平均底薪/
select MAX(sal),MIN(sal),AVG(sal),emp.deptno
from emp,dept
where emp.deptno=dept.deptno and
dname=‘research’ group by emp.deptno
/86.查询薪水比本部门平均薪水高的员工信息/
select * from emp,(
select deptno,AVG(sal) s
from emp group by deptno
) a where emp.deptno = a.deptno and emp.sal>a.s
创建学生表:
drop table student
CREATE TABLE student(
sno INT PRIMARY KEY auto_increment,
sname VARCHAR(10),
sex VARCHAR(4),
birthday DATE,
dep VARCHAR(10)
);
INSERT INTO student
VALUES(NULL,‘刘思宇’,‘男’,‘1998-10-10’,‘计算机院’),(NULL,‘焦阳’,‘男’,‘1998-10-01’,‘土木工程’),(NULL,‘胡家胜’,‘男’,‘1998-09-10’,‘水环学院’);
INSERT INTO student VALUES(NULL,‘张宇’,‘男’,‘1997-10-08’,‘计算机院’);
创建课程表:
drop table course
CREATE TABLE course(
cno INT PRIMARY KEY auto_increment,
cname VARCHAR(10),
cval INT #学分
);
INSERT INTO course VALUES(DEFAULT,‘数据结构’,4),(DEFAULT,‘英语’,2);
学生和课程成绩关联表
CREATE TABLE sc(
sno INT,
cno INT,
score INT,
FOREIGN KEY (sno) REFERENCES student(sno),
FOREIGN KEY (cno) REFERENCES course(cno)
);
INSERT INTO sc VALUES(1,2,90),(2,1,50),(3,1,56),(4,1,12),(4,2,34);(4,1,12),(4,2,34);
INSERT INTO sc VALUES (4,2,78)
select * from student
/1.查询选修了’数据结构’的总人数/
select count(1) from sc where cno = (
select cno from course where cname=‘数据结构’
)
/2.查询’张宇’同学选修了的课程和成绩/
select cname,score from course c,sc
where c.cno = sc.cno and sc.sno = (
select sno from student where sname=‘张宇’
)
/3.查询挂科学分最多的同学姓名和院系/
select sname,dep from student,(select b.sno,sum(cval) v from (
select c.cno,c.cval,a.sno from course c,(
select sno,cno from sc where score < 60
) a where c.cno = a.cno
)b group by b.sno order by v desc limit 0,1) d
where student.sno = d.sno