1. 程式人生 > >基本查詢語句

基本查詢語句

-- 實驗四 資料庫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;