Orcle基本语句

Posted Mr_邓

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Orcle基本语句相关的知识,希望对你有一定的参考价值。

  1 --显示员工的编号,姓名,工资,工资级别,所在部门的名称;(使用emp表)
  2 SELECT * FROM emp;
  3 SELECT * FROM salgrade;
  4 SELECT * FROM dept;
  5 SELECT e.empno, e.ename, e.sal, s.grade, d.dname
  6   FROM emp e, salgrade s, dept d
  7  WHERE e.sal BETWEEN s.losal AND s.hisal
  8    AND e.deptno = d.deptno;
  9 --现要求查询出每个雇员的姓名,工资,部门名称,工资在公司的等级(salgrade),及领导姓名及工资所在公司的等级
 10 SELECT e1.empno,
 11        e1.ename,
 12        e1.sal,
 13        s1.grade 员工工资等级,
 14        d.dname,
 15        e2.ename,
 16        e2.sal   领导工资,
 17        s2.grade 领导工资等级
 18   FROM emp e1, salgrade s1, salgrade s2, dept d, emp e2
 19  WHERE e1.sal BETWEEN s1.losal AND s1.hisal
 20    AND e1.deptno = d.deptno
 21    AND e1.mgr = e2.empno
 22    AND e2.sal BETWEEN s2.losal AND s2.hisal
 23  ORDER BY e1.sal;
 24 --外联接
 25 SELECT d.*, e.* FROM dept d, emp e WHERE d.deptno(+) = e.deptno; -- 右外联接(一)
 26 SELECT d.*, e.* FROM dept d, emp e WHERE d.deptno = e.deptno(+); --左外联接(一)
 27 SELECT d.*, e.* FROM dept d RIGHT JOIN emp e ON d.deptno = e.deptno; -- 右外联接(二)
 28 SELECT d.*, e.* FROM dept d LEFT JOIN emp e ON d.deptno = e.deptno; -- 左外联接(二)
 29 
 30 --查询员工编号,姓名,部门编号,部门名称(左外连接)
 31 SELECT e.empno, e.ename, e.deptno, d.dname
 32   FROM emp e
 33   LEFT JOIN dept d
 34     ON e.deptno = d.deptno;
 35 SELECT e.empno, e.ename, e.deptno, d.dname
 36   FROM emp e, dept d
 37  WHERE e.deptno = d.deptno(+);
 38 --查询员工编号,姓名,部门编号,部门名称(右外连接)
 39 SELECT e.empno, e.ename, e.deptno, d.dname
 40   FROM emp e
 41  RIGHT JOIN dept d
 42     ON e.deptno = d.deptno;
 43 SELECT e.empno, e.ename, e.deptno, d.dname
 44   FROM emp e, dept d
 45  WHERE e.deptno(+) = d.deptno;
 46 --查询30号部门的员工
 47 SELECT * FROM emp;
 48 SELECT * FROM emp e JOIN dept d USING (deptno) WHERE deptno = 30;
 49 
 50 --查询员工编号,姓名,部门编号,部门名称,职务编号,职务名称并且薪资大于8000 按照薪资降序排列 
 51 SELECT e.employee_id,
 52        e.last_name,
 53        d.department_id,
 54        d.department_name,
 55        e.salary,
 56        j.job_id,
 57        j.job_title
 58   FROM employees e
 59  INNER JOIN departments d
 60     ON e.department_id = d.department_id
 61  INNER JOIN jobs j
 62     ON e.job_id = j.job_id
 63  WHERE e.salary > 8000
 64  ORDER BY e.salary DESC;
 65 COMMIT;
 66 --查询所有部门的编号,部门名称,部门经理ID,部门经理姓名,部门所在的城市,国家,地区
 67 SELECT * FROM departments;
 68 SELECT * FROM locations;
 69 SELECT * FROM employees;
 70 SELECT * FROM countries;
 71 SELECT d.department_id   部门编号,
 72        d.department_name 部门名称,
 73        d.manager_id      部门经理id,
 74        e.last_name       部门经理姓名,
 75        l.city            部门所在城市,
 76        c.country_name     部门所在国家,
 77        l.state_province  部门所在地区
 78   FROM departments d
 79   LEFT JOIN employees e
 80     ON d.manager_id = e.manager_id
 81   LEFT JOIN locations l
 82     ON d.location_id = l.location_id
 83     LEFT JOIN countries c
 84     ON l.country_id = c.country_id
 85  ORDER BY d.department_id;
 86 --查询谁的工资比‘ABEL’
 87 select * from employees;
 88 SELECT first_name || last_name NAME, salary
 89   FROM employees
 90  WHERE salary >= (SELECT salary FROM employees WHERE last_name = Abel)
 91  ORDER BY salary;
 92 ----查询工资比7654高,同时与7788从事相同工作的全部雇员信息(emp)
 93 SELECT * FROM emp;
 94 SELECT *
 95   FROM emp
 96  WHERE sal > (SELECT sal FROM emp WHERE empno = 7654)
 97    AND job = (SELECT job FROM emp WHERE empno = 7788);
 98 --部门名称,部门员工数,部门平均工资,部门的最低收入雇员的姓名
 99 SELECT d.dname, e.deptno, tmp.c, tmp.a, tmp.m, e.ename
100   FROM dept d,
101        emp e,
102        (SELECT e.deptno, COUNT(e.deptno) c, AVG(sal) a, MIN(sal) m
103        FROM emp e GROUP BY e.deptno) tmp
104  WHERE d.deptno = e.deptno
105  AND e.sal = tmp.m;
106 --求出每个部门的最低工资的雇员信息
107 SELECT *
108   FROM emp e, (SELECT MIN(sal) m FROM emp GROUP BY deptno) tmp
109  WHERE e.sal = tmp.m;
110  SELECT *
111   FROM emp e
112  WHERE e.sal in (SELECT MIN(sal) m FROM emp GROUP BY deptno);
113 ----查询工资与10部门中的任意一个人相等的员工信息
114 select * from emp;
115 SELECT * FROM emp WHERE sal in (SELECT sal FROM emp WHERE deptno =  10);
116 SELECT * FROM emp WHERE sal = ANY(SELECT sal FROM emp WHERE deptno =  10);
117 --查询所有是部门经理的员工(employees)
118 SELECT * FROM employees;
119 SELECT *
120   FROM employees
121  WHERE employee_id IN (SELECT manager_id FROM employees) ORDER BY employee_id;
122  SELECT *
123   FROM employees e1
124  WHERE  EXISTS(SELECT manager_id FROM employees e2 WHERE e1.employee_id = e2.manager_id) ORDER BY employee_id;
125 ----查询所有不是部门经理的员工(employees)
126 SELECT *
127   FROM employees
128  WHERE employee_id  <> ALL(SELECT manager_id FROM employees WHERE manager_id IS NOT NULL ) ORDER BY employee_id;
129  SELECT *
130   FROM employees
131  WHERE  employee_id NOT IN (SELECT manager_id FROM employees WHERE manager_id IS NOT NULL) ORDER BY employee_id;
132  SELECT *
133   FROM employees e1
134  WHERE NOT EXISTS(SELECT manager_id FROM employees e2 WHERE e1.employee_id = e2.manager_id) ORDER BY employee_id;
135  --查询所有员工人数不少于3人的部门信息(employees)
136  SELECT * FROM departments;
137  SELECT * FROM employees;
138  SELECT d.*,tmp.c
139    FROM departments d INNER JOIN
140         (SELECT department_id,COUNT(employee_id) c FROM employees GROUP BY department_id) tmp
141   ON tmp.c >= 3 AND d.department_id = tmp.department_id;
142  ----查询姓名,职位,薪资在按照 职位,薪资 去重复之后存在的数据
143  SELECT ename,job,sal FROM emp WHERE exists(select distinct job,sal from emp);
144 --查询是本部门入职最早的但不是部门经理的员工
145 SELECT * FROM employees;
146 --1.查询本部门入职最早的员工
147 SELECT MIN(hire_date) FROM employees GROUP BY department_id;
148 --2.查询不是部门经理的员工
149 SELECT *
150   FROM employees e1
151  WHERE NOT EXISTS(SELECT manager_id FROM employees e2 WHERE e1.employee_id = e2.manager_id) ORDER BY employee_id; 
152 --3.结合1、2给出本部门入职最早的但不是部门经理的员工
153 SELECT *
154   FROM employees e1
155  WHERE NOT EXISTS
156  (SELECT manager_id
157           FROM employees e2
158          WHERE e1.employee_id = e2.manager_id)
159    AND hire_date IN
160        (SELECT MIN(hire_date) FROM employees GROUP BY department_id)
161  ORDER BY employee_id;
162 --查询所有工资超过本部门平均工资的员工
163 --1. 查询各个部门的平均工资
164 SELECT department_id,AVG(salary) avgsa FROM employees GROUP BY department_id;
165 --2.
166 SELECT e.*, tmp.avgsa
167   FROM employees e,
168        (SELECT department_id, AVG(salary) avgsa
169           FROM employees
170          GROUP BY department_id) tmp
171  WHERE e.department_id = tmp.department_id
172    AND e.salary > tmp.avgsa;
173 
174 --查询所有换过职务的员工
175 SELECT e.*,jh.* FROM employees e,job_history jh WHERE e.employee_id = jh.employee_id; 
176 SELECT e.*, jh.*
177   FROM employees e
178  INNER JOIN job_history jh
179     ON e.employee_id = jh.employee_id;

 

以上是关于Orcle基本语句的主要内容,如果未能解决你的问题,请参考以下文章

Orcle基本语句

Orcle的sql语句优化

Orcle查询优化改写-----单表查询

orcle函数

excel导入到Orcle

Orcle 相关操作