基本查询语句

-- 实验四 数据库SQL语言基础编程
-- 实验目的:
--  掌握数据库查询语句的编写方法
--  掌握利用查询语言完成基本查询
--  掌握利用SQL语句完成数据的添加、删除、修改操作
-- 实验内容:
-- 一、简单查询
-- 编写简单查询语句,理解笛卡尔积、选择、投影的概念及其在SQL中的实现
 
--  查询员工表与部门表的笛卡尔积
     select empno,ename,dname
     from emp,dept
     where emp.deptno=dept.deptno;

--  在上述操作的基础上完成连接的选择操作和投影操作
     select empno,ename,dname
     from emp
     JOIN dept ON emp.deptno=dept.deptno;

     select empno,ename,dname
     from EMP
     LEFT JOIN dept ON emp.deptno=dept.deptno;

     select empno,ename,dname
     from emp
     RIGHT JOIN dept ON emp.deptno=dept.deptno;

     select empno,ename,dname
     from emp
     FULL JOIN dept ON emp.deptno=dept.deptno;

     select count(empno),
            dname
     from emp
     FULL JOIN dept ON emp.deptno=dept.deptno
     GROUP BY emp.deptno,dname;
--  --  对查询数据进行排序操作
      select count(empno),
             nvl(dname,'OTHER')
       from emp
       FULL JOIN dept ON emp.deptno=dept.deptno
       GROUP BY emp.deptno,dname;

-- -- 编写查询语句,完成对员工信息、部门情况等的基本查询
-- --  查询工资超过2500的员工基本信息
 
       select * from emp  where  sal>2500
     
 
-- --  查询月总收入超过2500的员工基本信息
       
         select * from emp
         where  sal+nvl(comm,0) >2500;
               
-- --  查询工资超过2500的员工的姓名及其所在部门名称
     
       select empno,
              ename,
              sal,
              nvl(comm,0) as comm,
              sal+nvl(comm,0) incoming
        from emp
        where sal>2500
       ORDER BY incoming desc,ename asc;
-- --  查询所有员工的工资、奖金、总收入,并按部门编号排序
select empno,
              ename,
              sal,
              comm,
              sal+nvl(comm,0) incoming,
              deptno
        from emp
        order by deptno;
-- --  查询全公司的平均工资、最高工资、最低工资和总奖金数。
  select sum(sal) sum_sal,
         max(sal) max_sal,
         min(sal) min_sal,
         sum(sal+nvl(comm,0)) sum_incoming,
         max(sal+nvl(comm,0)) max_incoming,
         min(sal+nvl(comm,0)) min_incomingg
   from emp;
--  -- 二、分组查询
-- -- 编写分组查询语句,理解分组查询的意义
  select MAX(empno),
         MIN(ename),
         SUM(sal),
         AVG(nvl(comm,0)) as avg_comm,
         avg(sal+nvl(comm,0)) avg_incoming,
         deptno
   from emp
   GROUP BY deptno
   ORDER BY deptno;
-- -- 编写分组查询语句,实现分组查询的应用
select empno,
       ename,
       sal,
       comm,
       sal+nvl(comm,0) incoming,
       deptno
 from emp
 ORDER BY deptno;


select MAX(empno),
         MIN(ename),
         SUM(sal),
         AVG(nvl(comm,0)) as avg_comm,
         avg(sal+nvl(comm,0)) avg_incoming,
         deptno
   from emp
   where sal+nvl(comm,0)>2000
   GROUP BY deptno
   ORDER BY deptno;
      
select MAX(empno),
         MIN(ename),
         SUM(sal),
         AVG(nvl(comm,0)) as avg_comm,
         avg(sal+nvl(comm,0)) avg_incoming,
         deptno
   from emp
   GROUP BY deptno
   HAVING avg(sal+nvl(comm,0))>2000
   ORDER BY deptno;
      
-- -- 三、增删改语句
-- -- 添加数据

insert into emp values(1234,'tang','teacher',null,'01-SEP-96',null,null,null);
 insert into emp values(empno,ename,job,hiredate,sal) values(1234,'tang','teacher','01-SEP-96','980');
-- -- 修改数据
update emp set sal=1080 where empno=1234;
update emp set (sal=980,comm=100) where empno=1234;
-- -- 删除数据
delete from emp where empno=1234;
delete from emp where empno=20;

posted on 2018-11-30 09:43  努力努力的夏天  阅读(201)  评论(0编辑  收藏  举报

导航