SQL1999与1992的区别:
1992连接表的写法:
select ename,dname from emp,dept where emp.deptno = dept.deptno;
1999连接表的写法:
select ename,dname from emp join dept on(emp.deptno = dept.deptno);
select ename,dname from emp join dept using(deptno); using(deptno)意思是emp表中的deptno等
于dept表的deptno。等同于上一种写法
找出职员的上司的名字并打印出来:
写法1:
select e1.ename,e2.ename from emp e1, emp e2
where e1.mgr = e2.empno;:
写法2:
select e1.ename,e2.ename from emp e1 join emp e2
on(e1.mgr = e2.empno);:
写法3:
select e1.ename,e2.ename from emp e1 left join emp e2
on (e1.mgr = e2.empno);左外连接,将左边的表,本例中为e1中未与e2连接配对的记录也显示出来
全外连接:
select ename,dname from emp e full join dept d
on(e.deptno = d.deptno);左右两边的表的多余记录都显示出来
select deptno,avg_sal,grade from
(select deptno,avg(sal) avg_sal from emp group by deptno) t
join salgrade s on(t.avg_sal between s.losal and s.hisal)
求部门中哪些人薪水最高:
select ename,sal from emp
join (select max(sal) max_sal,deptno from emp group by deptno) t
on(emp.sal = t.max_sal and emp.deptno = t.deptno);
求部门平均薪水的等级:
select deptno,avg_sal,grade from
(select deptno,avg(sal) avg_sal from emp group by deptno) t
join salgrade s on(t.avg_sal between s.losal and s.hisal);
求部门平均的薪水等级:
select deptno,avg(grade) from
(select deptno,ename, grade from emp join salgrade s on(emp.sal between s.losal and
s.hisal))t
group by deptno
求部门中哪些是经理人:
select ename from emp where empno in (select distinct mgr from emp);
不准用组函数,求薪水的最高值:
select distinct sal from emp where sal not in
(select distinct e1.sal from emp e1 join emp e2 on (e1.sal<e2.sal));
求平均薪水最高的部门的部门编号:
select deptno,avg_sal from
(select avg(sal) avg_sal,deptno from emp group by deptno)
where avg_sal =
(select max(avg_sal) from (select avg(sal) avg_sal,deptno from emp group by deptno))
求平均薪水最高的部门的部门名称:
select dname from dept where deptno =
(
select deptno,avg_sal from
(select avg(sal) avg_sal,deptno from emp group by deptno)
where avg_sal =
(select max(avg_sal) from (select avg(sal) avg_sal,deptno from emp group by deptno))
)
求平均薪水的等级最低的部门的部门名称:
select dname,t1.deptno,grade,avg_sal from
(
select deptno,grade,avg_sal from
(select deptno,avg(sal) avg_sal from emp group by deptno) t
join salgrade s on (t.avg_sal between s.losal and s.hisal)
) t1
join dept on (t1.deptno = dept.deptno)
where t1.grade =
(
select min(grade) from
(
select deptno,grade,avg_sal from
(select deptno,avg(sal) avg_sal from emp group by deptno) t
join salgrade s on (t.avg_sal between s.losal and s.hisal)
)
)
创建一个视图:
create view v$_dept_avg_sal_info as
select deptno,grade,avg_sal from
(select deptno,avg(sal) av_sal from emp group by deptno) t
join salgrade s on(t.avg_sal between s.losal and s.hisal)
创建视图需要权限,因此必须以超级管理员登录并赋予权限:
conn sys/sys as sysdba;
grant create table,create view to scott;
因此上面代码可以简化为:
select dname,t1.deptno,grade,avg_sal from
v$_dept_avg_sal_info t1
join dept on (t1.deptno = dept.deptno)
where t1.grade =
(
select min(grade) from v$_dept_avg_sal_info
)
求比普通员工的最高薪水还要高的经理人的名称:
select ename from emp
where empno in (select distinct mgr from emp where mgr is not null
and
sal >
(
select max(sal) from emp where empno not in
(select distinct mgr from emp where mgr is not null)
)
select ename from(select rownum r,ename from emp) where r>10;
求出薪水最高的五个人的名字:
错误做法:
select ename ,sal from emp where rownum<=5 order by sal desc;这样写的话是先取出再排序,因此取出的是薪水最低的5个人的名字。
正确写法:
select ename,sal from (select ename,sal from emp order by sal desc) where rownum <= 5;
求出薪水排在第6~10位的职员的名字:
select ename,sal from
(select ename,sal,rownum r from
(select ename,sal from emp order by sal desc) )
where r>=6 and r<=10;
有三张表:
S(SNO,SNAME)代表(学号,学生姓名)
C(CNO,CNAME,CTEACHER)代表(课程编号,课程名字,任课老师)
SC(SNO,CNO,CSCORE)代表(学号,课程编号,课程成绩)
问题:
1.求没选过黎明老师的课的学生的名字
2.列出两门以上(含两门)不及格的学生的姓名和平均成绩
3.即学过1号课程又学过2号课程的学生的姓名。
答案:
1.select sname from s join sc on(s.sno = sc.sno) join c on (c.cno = sc.cno)
where c.cteacher <> 'liming'
2.select sname from s where sno in
(select sno from sc where cscore<60 group by sno having count(*)>=2)
3.selece sname from s where sno in
(select sno from sc where cno = 1 and sno in(select sno from sc where cno = 2) )