日期:2014-05-17  浏览次数:20766 次

oracle面试题,一个SQL语句,求解
一些简单的sql语句,比如说 现在数据库中有10记录,要求你写一个语句保留第二条和第五条记录,将其他的记录全部删除。

------解决方案--------------------
SQL code

--举例如下
SQL> delete emp
  2      where not exists (select 1 from
  3          (select rownum rn,empno from emp) t
  4          where t.empno=emp.empno and t.rn in(2,5));

已删除12行。

SQL> select * from emp;

     EMPNO ENAME                JOB                       MGR HIREDATE          
---------- -------------------- ------------------ ---------- --------------    
       SAL       COMM     DEPTNO                                                
---------- ---------- ----------                                                
      7499 Allen                SALESMAN                 7698 20-2月 -81        
      1600        300         30                                                
                                                                                
      7654 Martin               SALESMAN                 7698 28-9月 -81        
      1250       1400         30                                                
                                                                                

SQL> rollback;

回退已完成。

------解决方案--------------------
delete emp
where not exists (select 1 from
(select rownum rn,empno from emp) t
where t.empno=emp.empno and t.rn in(2,5));