oracle sql面试题2
一.简单SQL查询:
1):统计每个部门员工的数目
select dept,count(*) from employee group by dept;
2):统计每个部门员工的数目大于一个的记录
select dept,count(*) from employee group by dept 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;
4):查询哪个部门没有员工
select e.empno, d.deptno
from emp e, dept d
where e.deptno(+) = d.deptno
and e.deptno is null;
or
select *
from dept d
where not exists(select 1 from emp e
where e.deptno = d.deptno
);
二.复杂SQL查询
有3个表(15分钟):(SQL)
Student 学生表 (学号,姓名,性别,年龄,组织部门)
Course 课程表 (编号,课程名称)
Sc 选课表 (学号,课程编号,成绩)
表结构如下:
1) 写一个SQL语句,查询选修了’JAVA’的学生学号和姓名(3分钟)
答:SQL语句如下:
select stu.sno, stu.sname
from student stu, course c, sc
where stu.sno = sc.sno
and sc.cno = c.cno
and c.cname=’JAVA’;
2) 写一个SQL语句,查询’a’同学选修了的课程名字(3分钟)
答:SQL语句如下:
select stu.sname, c.cname
from student stu, course c, sc
where stu.sno = sc.sno
and sc.cno = c.cno
and stu.sname = ’a’;
3) 写一个SQL语句,查询选修了5门课程的学生学号和姓名(9分钟)
答:SQL语句如下:
select stu.sno, stu.sname
from student stu
where (select count(*) from sc where sno=stu.sno) = 5;
三. 在SQL中删除重复记录的方法:(用到rowid (oracle伪列))
1)通过建立临时表来实现
SQL>create table temp_emp as (select distinct * from employee)
SQL>truncate table employee; (清空employee表的数据)
相关文档:
分页的宗旨是控制查询出来的数据个数,下面这条语句对于Oracle分页已经足够了。
(注:该语句只能用于Oracle)
SELECT * from (SELECT temp.*, ROWNUM RN from (SELECT * from USERS ORDER BY ID DESC) temp) WHERE RN > 0 AND RN <= 10
SELECT *
from (SELECT AA.*, ROWNUM RN ......
1. 当前系统日期、时间
select getdate()
2. dateadd 在向指定日期加上一段时间的基础上,返回新的 datetime 值
例如:向日期加上2天
select dat ......
SQL语句复制表的方法
(2009-08-29 13:41:54)
标签:
sql
分类:计算机知识
如果目的表已经存在:
insert into DATAHR.DBO.GBITEM
select * from DEMO.DBO.GBITEM
如果目的表不存在:
select * into DATAHR.DBO.GBITEM
from DEMO.DBO.GBITEM
跨库导入
select * into [zk_news].[dbo].[News1] from [zk_media].[ ......
我们在平常的系统开发中常常会遇到像无限级分类这样的树型结构数据,现提供一个可用的数据库存储过程,可以完成树型结构数据的排序。
环境:windows7+Sql Server 2008
说明:下面代码已经转换成Sql server2000的脚本,处理效果如下,看sortname字段结果,代码经过测试。
创建树型表
CREATE TABLE [dbo].[categories](
......
我们在工作中希望能看见自己运行的DML语句的运行报告,例如select,delete,update,megre和insert语句运行后的情况,以用来监视和调优语句。我们通常在sql*plus中使用set autotrace on开启。
那autotrace是如何安装的呢?thomas kyte的大作中给出了详细的方法和解释:
& ......