1
Oracle 一些 面试题目
一.简单 SQL
SQL
SQL
SQL
查询:
1 )统计每个部门员工的数目
select dept _id , count(*) from s_emp group by dept _id ;
2 )统计每个部门员工的数目大于一个的记录
select dept _id , count(*) from s_emp
group by dept _id having count(*)>1;
3 )统计工资超过 1200 的员工所在部门的名称
select e.first_name, salary, d.name
from s_emp e , s_dept d
where e.dept_id = d.id
and salary > 1200;
二 . 在 SQL 中删除重复记录的方法 : (用到 rowid (oracle 伪列 ) )
1 )通过建立临时表来实现
SQL>create table temp_emp
as (select distinct * from employee)
SQL>truncate table employee; ( 清 空 employee 表的 数 据)
SQL> rename te mp_emp to employee ; ( 将 表重命名)
2 )通过 rowid ,效率更高。
SQL>delete from employee where rowid not in (
select max(t1.rowid) from employee t1 group by
t1.emp_id,t1.emp_name,t1.salary);-- 这 里用 min(rowid)
也可以。
三 . TOP N 问题: (用到 rownum (oracle 伪列 ) )
--rownum 只能使用 <= 或 < 的关系比较运算符
select * from s_ emp where rownum <= 2;
-- 查询公司工资最高的 3 个人
/*select * from emp where rownum <= 3
order by sal desc;*/ 错误的
select * from (select * from emp order by sal desc)