学习oracle第二天,学习了大量理论知识,还是需要靠练习记住它们。今天,继续昨天的小demo,应用上所学知识,来实现一些sql查询语句的用法。
先上代码:
create table emp(empno number(4,0) primary key,ename varchar2(10),
job varchar2(9),mgr number(4,0),hiredate date,sal number(7,2),comm
number(7,2),deptno number(2,0));
--1
insert into emp(empno,ename,job,mgr,hiredate,sal,comm,deptno) values
(7369,'smith','clerk',7902,to_date('1980-12-17','yyyy/mm/dd'),
800.00,'',20);
commit;
--2
insert into emp(empno,ename,job,mgr,hiredate,sal,comm,deptno) values
(7499,'allen','salesman',7698,to_date('1981-2-20','yyyy/mm/dd'),
1600.00,'300.00',20);
commit;
--3
insert into emp(empno,ename,job,mgr,hiredate,sal,comm,deptno) values
(7521,'ward','salesman',7698,to_date('1981-2-22','yyyy/mm/dd'),
1250.00,'500.00',30);
commit;
--4
insert into emp(empno,ename,job,mgr,hiredate,sal,comm,deptno) values
(7566,'jones','manager',7839,to_date('1981-4-2','yyyy/mm/dd'),
2975.00,'',20);
commit;
--5
insert into emp(empno,ename,job,mgr,hiredate,sal,comm,deptno) values
(7654,'martin','salesman',7698,to_date('1981-9-28','yyyy/mm/dd'),
1250,'1400.00',30);
commit;
--6
insert into emp(empno,ename,job,mgr,hiredate,sal,comm,deptno) values
(7698,'blake','manager',7839,to_date('1981-5-1','yyyy/mm/dd'),
2850,'',30);
commit;
--7
insert into emp(empno,ename,job,mgr,hiredate,sal,comm,deptno) values
(7782,'clark','manager',7839,to_date('1981-6-9','yyyy/mm/dd'),
2450,'',10);
commit;
--8
insert into emp(empno,ename,job,mgr,hiredate,sal,comm,deptno) values
(7788,'scott','analyst',7566,to_date('1987-4-19','yyyy/mm/dd'),
3000,'',20);
commit;
--9
insert into emp(empno,ename,job,hiredate,sal,comm,deptno) values
(7839,'king','president',to_date('1981-11-17','yyyy/mm/dd'),
5000,'',10);
commit;
--10
insert into emp(empno,ename,job,mgr,hiredate,sal,comm,deptno) values
(7844,'turner','salesman',7698,to_date('1981-9-8','yyyy/mm/dd'),
5000,'0.00',30);
commit;
--11
insert into emp(empno,ename,job,mgr,hiredate,sal,comm,deptno) values
(7876,'adams','clerk',7788,to_date('1987-5-23','yyyy/mm/dd'),
1100,'',20);
commit;
--12
insert into emp(empno,ename,job,mgr,hiredate,sal,comm,deptno) values
(7900,'james','clerk',7698,to_date('1981-12-3','yyyy/mm/dd'),
950,'',30);
commit;
--13
insert into emp(empno,ename,job,mgr,hiredate,sal,comm,deptno) values
(7902,'ford','analyst',7566,to_date('1981-12-3','yyyy/mm/dd'),
3000,'',20);
commit;
--14
insert into emp(empno,ename,job,mgr,hiredate,sal,comm,deptno) values
(7934,'miller','clerk',7782,to_date('1982-1-23','yyyy/mm/dd'),
1300,'',10);
commit;
select empno as "员工ID",ename as "员工姓名", job as "职位",mgr as "员工管理者ID",
hiredate as "入职日期",sal as "薪资",comm as "绩效",deptno as "员工所在部门的ID"
from emp;
alter session set nls_date_format='yyyy/mm/dd';
create table dept(deptno number(2,0),dname varchar2(14),loc varchar2(13));
--1
insert into dept(deptno,dname,loc) values(10,'accounting','new york');
commit;
--2
insert into dept(deptno,dname,loc) values(20,'research','dallas');
commit;
--3
insert into dept(deptno,dname,loc) values(30,'sales','chicago');
commit;
--4
insert into dept(deptno,dname,loc) values(40,'operations','boston');
commit;
接下来,我们逐一完成一些功能:
1)已知员工的每月收入为:薪资+绩效*0.8,如果绩效为null,则表示绩效为 0。查询员工的姓名以及月收入(列名为money),并按照月收入升序排列。
查询代码和结果如图所示:
2)查询各个管理者属下员工的最低工资,其中最低工资不能低于800,且没有管理者的员工不计算在内。
查询代码和结果如图所示:
3)查询各部门的平均绩效,如果绩效为null,则按数值0进行统计
查询代码和结果如图所示:
4)查询所有部门的名称、所在地、员工数量以及平均工资
查询代码和结果如图所示:
5)查询员工的编号、姓名、部门编码、部门名称以及部门所在城市。要求:把没有员工的部门也查出来。
查询代码和结果如图所示:
demo完成到这,未来还有更多的功能和实现,敬请期待!