Oracle的sql语句

来源:互联网 发布:中国近现代报刊数据库 编辑:程序博客网 时间:2024/05/21 22:22

登录方法:

1.运行-》cmd

2.输入sqlplus /nolog

3.先以管理员的身份登录,oracle的默认的用户别锁着。SQL>connect / as sysdba ;

4.解锁。alter user [username]  account  unlock;

5.修改密码alter user [username] identified by [password];

 

 sql语句语法:

使用scott/tiger用户下的emp表和dept表完成下列练习,表的结构说明如下

  emp员工表(empno员工号/ename员工姓名/job工作/mgr上级编号/hiredate受雇日期/sal薪金/comm佣金/deptno部门编号)

  dept部门表(deptno部门编号/dname部门名称/loc地点)

  工资 薪金 佣金

  ------1.列出至少有一个员工的所有部门

  select * from dept

  where deptno in

  (select deptno from emp group by deptno having count(*)>1);

  ------2.列出薪金比“SMITH”多的所有员工。

  select * from emp

where sal>(select sal from emp where ename='SMITH');

 

------3.列出所有“CLERK”(办事员)的姓名及其部门名称。

  select dname,ename from dept a,emp b

  where a.deptno=b.deptno and job='CLERK';

select (select dname from dept where deptno=a.deptno) as dname ,ename

  from emp a

  where job='CLERK';

 

 

  ------4.列出最低薪金大于1500的各种工作。

  select job,min(sal) msal from emp

  group by job having min(sal)>1500;

  ------5.列出薪金高于公司平均薪金的所有员工。

select ename from emp where sal>(select avg(sal) from emp);

 

  ------6.列出在部门“SALES”(销售部)工作的员工的姓名,假定不知道销售部的部门编号。

  select ename from emp where deptno=(select deptno from dept where dname='SALES');

 

  ------7.列出与“SCOTT”从事相同工作的所有员工。

  select * from emp where job=(select job from emp where ename='SCOTT');

  ------8.列出薪金等于部门30中员工的薪金的所有员工的姓名和薪金。

  select * from emp where sal in

  (select sal from emp where deptno=30);

  或

  select * from emp where sal = any

(select sal from emp where deptno=30);

  ------9.列出所有员工的姓名、部门名称和工资。

  select ename,dname,sal+nvl(comm,0) from emp,dept where emp.deptno=dept.deptno;

 

  ------10.列出薪金高于在部门30工作的所有员工的薪金的员工姓名和薪金。

  --最大值>all

  select * from emp where sal>all

  (select sal from emp where deptno=30);

  --最小值<all

  select * from emp where sal < all

(select sal from emp where deptno=30);

 ------11.列出MANAGER(经理)的最低薪金。

  select min(sal) from emp where job='MANAGER' ;

------12.列出所有员工的年工资,按年薪从低到高排序。

  select ename,(sal+nvl(comm,0))*12 tot from emp order by tot;

总结

 

 

 

原创粉丝点击