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表的数据)
相关文档:
1. 当前系统日期、时间
select getdate()
2. dateadd 在向指定日期加上一段时间的基础上,返回新的 datetime 值
例如:向日期加上2天
select dat ......
经常会有时候要用到查询当天,近几天,本周,本月的相关数据,所以记录一下这些比较特殊的SQL语句~~
--当天
(usrRegTime >=CONVERT(varchar(10),getDate(),120)+' 00:00:00' and usrRegTime <=CONVERT(varchar (10),getDate(),120)+' 23:59:59')
--近三天
DateDiff(day,usrRegTime,getdate()) <=3
--本周
......
Sql Server 中一个非常强大的日期格式化函数
Select CONVERT(varchar(100), GETDATE(), 0): 05 16 2006 10:57AM
Select CONVERT(varchar(100), GETDATE(), 1): 05/16/06
Select CONVERT(varchar(100), GETDATE(), 2): 06.05.16
Select CONVERT(varchar(100), GETDATE(), 3): 16/05/06
Select CONVERT(varchar(100), GE ......
这几天负责一个家教门户网站的开发,基于cakephp框架。在培训机构表(schools)中存在一个字段subject用来存储另一个数据表
(subjects)中记录的id值,且存储形式为:'1,2,3,4,5'。但是在应用高级搜索过滤时页面select选项option的传值为
subjects的id值,需要判断查询表schools中subject字段存在此id,即查询显示此记录 ......