多动手才有深刻领会
#-----创建员工表----------
create table emp(
empid int, #员工 ID
empno int, #员工编号
ename varchar(10), #员工姓名
job varchar(9), #职位
mgr int, #上级编号
hiredate date, #雇佣日期
sal float(7,2), #薪水
comm float(7,2), #佣金
deptno int #所在部门
);
--提示:工资 = 薪金 + 佣金
#------创建部门表-----------
create table dept(
depid int, #部门ID
deptno int, #部门编号
dname varchar(14), #部门名称
loc varchar(13) #地点
);
#-----------------插入对应数据------------------------------------------
insert into emp values(1,7369,'smith','clerk',7902,'1980-12-17',800.00,null,20);
insert into emp values(2,7499,'allen','salesman',7698,'1981-2-20',1600.00,300.00,30);
insert into emp values(3,7521,'ward','salesman',7698,'1981-2-22',1250.00,500.00,30);
insert into emp values(4,7566,'jones','manager',7839,'1981-4-2',2975.00,null,20);
insert into emp values(5,7654,'martin','salesman',7698,'1981-9-28',1250.00,1400.00,30);
insert into emp values(6,7698,'blark','manager',7839,'1981-5-1',2850.00,null,30);
insert into emp values(7,7782,'clark','manager',7839,'1981-6-9',2450.00,null,10);
insert into emp values(8,7788,'scott','analyst',7566,'1987-4-19',3000.00,null,20);
insert into emp values(9,7839,'king','president',null,'1981-11-17',5000.00,null,10);
insert into emp values(10,7844,'turner','salesman',7698,'1981-9-8',1500.00,0.00,30);
insert into emp values(11,7876,'adams','clerk',7788,'1987-5-23',1100.00,null,20);
insert into emp values(12,7900,'jams','clerk',7698,'1981-12-3',950.00,null,30);
insert into emp values(13,7902,'ford','analyst',7566,'1981-12-3',3000.00,null,20);
insert into emp values(14,7934,'miller','clerk',7782,'1982-1-23',1300.00,null,10);
insert into dept values(1,10,'accounting','new york'),
(2,20,'research','dallas'),
(3,30,'sales','chicago'),
(4,40,'operation','boston');
#---------------------综合查询练习------------------
1. 找出EMP表中的姓名(ENAME)第三个字母是A 的员工姓名。
select ename from emp where ename like '__A%'; //注意此处是两个下划线//
2. 找出EMP表员工名字中含有A 和N的员工姓名。
select ename from emp where ename like '%A%' and ename like '%N%'
3. 找出所有有佣金的员工,列出姓名、工资、佣金,显示结果按工资从小到大,佣金从大到小。
select ename,sal+comm va,comm from emp where comm is not null ORDER BY va,comm desc
4. 列出部门编号为20的所有职位。
select job from emp where deptno=20
5. 列出不属于SALES 的部门。
select dname from dept where not in (sales);
# ✖为啥不能用 no in?由于in适用于查有多改一样的数据时,而此处部门中sales只有一个。
select*from dept where dept.dname !='SALES'; #✔
6. 显示工资不在1000 到1500 之间的员工信息:名字、工资,按工资从大到小排序。
select ename,sal+IFNULL(comm,0) money from emp GROUP BY ename having money not BETWEEN 1000 and 1500 ORDER BY money desc
7. 显示职位为MANAGER 和SALESMAN,年薪在15000 和20000 之间的员工的信息:名字、职位、年薪。
select ename,job,(sal+IFNULL(comm,0))*12 money from emp where job in ('manager','salesman') GROUP BY ename having money BETWEEN 15000 and 20000
8.列出各个部门的MANAGER(经理)的最低薪金。
select d.dname,min(e.sal) from dept d,emp e where e.deptno=e.deptno and e.job='manager' GROUP BY d.dname
9.列出所有员工的年工资,按年薪从低到高排序。
select ename,(sal+IFNULL(comm,0))*12 money from emp ORDER BY money
10.列出各种工作的最低工资。
select job,min(sal+IFNULL(comm,0)) from emp GROUP BY job
函数相关:
11.以首字母大写的方式显示所有员工的姓名?
select concat(UPPER(substr(ename,1,1)),SUBSTR(ename,2)) from emp;
12.显示正好为5个字符的员工的姓名?
select ename from emp where CHAR_LENGTH(ename)=5
13.显示不带有"R"的员工的姓名?
select ename from emp where ename not like '%R%'
14.显示所有员工姓名的前三个字符?
select SUBSTR(ename,1,3) from emp
15.显示所有员工的姓名,用a替换所有"A"?
select REPLACE(ename,'a','A') from emp
选作:
1.列出至少有一个员工的所有部门。
select d.dname,count(e.id) count from dept d,emp e where d.deptno=e.deptno GROUP BY d.dname
2.列出薪金比“SMITH”多的所有员工。
select sal from emp where ename='smith'
select * from emp e,(select sal from emp where ename='smith') a where e.sal>a.sal;
3.列出所有员工的姓名及其直接上级的姓名。
select a.ename,b.ename from emp a LEFT JOIN emp b on a.mgr=b.empno
4.列出受雇日期早于其直接上级的所有员工。
select a.ename,b.ename,a.hiredate,b.hiredate from emp a LEFT JOIN emp b on a.mgr=b.empno where a.hiredate<b.hiredate
5.列出部门名称和这些部门的员工信息,同时列出那些没有员工的部门
select d.dname,e.* from dept d LEFT JOIN emp e on e.deptno=d.deptno
6.列出所有“CLERK”(办事员)的姓名及其部门名称。
select e.ename,d.dname from emp e,dept d where e.deptno=d.deptno and e.job='clerk'
7.列出最低薪金大于1500的各种工作。
select job,min(sal) ms from emp GROUP BY job HAVING ms>1500
8.列出在部门“SALES”(销售部)工作的员工的姓名,假定不知道销售部的部门编号。
select deptno from dept where dname='sales';
select e.ename from emp e,(select deptno from dept where dname='sales') a where e.deptno=a.deptno
9.列出薪金高于公司平均薪金的所有员工。
select avg(sal) from emp
select e.* from emp e,(select avg(sal) sal from emp) a where e.sal>a.sal;
10.列出与“SCOTT”从事相同工作的所有员工。
select job from emp where ename='scott'
select e.ename from emp e,(select job from emp where ename='scott') a where e.job=a.job
11.列出薪金等于部门30中员工的薪金的所有员工的姓名和薪金。
select sal from emp where deptno=30
select ename,sal from emp where sal=any(select sal from emp where deptno=30 )
12.列出薪金高于在部门30工作的所有员工的薪金的员工姓名和薪金。
select ename,sal from emp where sal>all(select sal from emp where deptno=30 )
13.列出在每个部门工作的员工数量、平均工资和平均服务期限。
select d.dname,count(e.id),avg(sal+IFNULL(comm,0)),avg(DATEDIFF(SYSDATE(),hiredate)) from emp e,dept d where e.deptno=d.deptno GROUP BY d.deptno
14.列出所有员工的姓名、部门名称和工资。
SELECT e.ename,d.dname,e.sal+IFNULL(e.comm,0) money from emp e,dept d where e.deptno=d.deptno
15.列出所有部门的详细信息和部门人数。
select d.*,count(e.id) from emp e,dept d where e.deptno=d.deptno GROUP BY d.id