一、简单查询
目的:编写简单查询语句,理解笛卡尔积、选择、投影的概念及其在SQL中的实现
1.查询员工表与部门表的笛卡尔积
2.在上述操作的基础上完成连接的选择操作和投影操作
3.对查询数据进行排序操作
3.编写查询语句,完成对员工信息、部门情况等的基本查询
3.1查询工资超过2500的员工基本信息
select *
from emp
where nvl(sal,0)>=2500;
对比验证:
3.2查询月总收入超过2500的员工基本信息
select *
from emp
where nvl(sal,0)+nvl(comm,0)>=2500;
对比验证:
3.3查询工资超过2500的员工的姓名及其所在部门名称
select ename,dname
from emp,dept
where emp.deptno=dept.deptno and nvl(sal,0)>=2500 ;
对比验证:
3.4查询所有员工的工资、奖金、总收入,并按部门编号排序
select deptno as "部门编号",sal "工资",comm "奖励",(sal+comm)"总收入"
from emp
order by deptno asc;
对比验证:
3.5查询全公司的平均工资、最高工资、最低工资和总奖金数。
select avg(sal) as"平均工资",max(sal)"最高工资",min(sal)"最低工资",sum(comm)"总奖厉数"
from emp;
对比验证:
二、分组查询
1.查询各部门的平均工资和最高月收入和总资金数
select dept.deptno,dname,avg(sal),max(sal),sum(comm)
from emp,dept
where emp.deptno=dept.deptno
group by dept.deptno,dname;
对比验证:
2.查询部门号大于10且平均工资大于1500的部门名称、部门平均工资和部门人数
select dept.deptno,dname,avg(sal),count(ename)
from emp,dept
where emp.deptno=dept.deptno
group by dept.deptno,dname
having dept.deptno>10 and avg(sal)>1500;
对比验证:
三、增删改语句
1.添加数据
在部门表中添加一个部门,编号为60,名称为 TRAIN 地点在 LOUDI
insert into dept(deptno,dname,loc)
values(60,'TRAIN','LOUDI')
2.修改数据
将刚才添加了部门的编号修改为80
update dept
set deptno=80
where deptno=50
3.删除数据
删除刚才添加了部门
delete from dept
where deptno=80