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;
时间: 2024-11-08 21:19:29

Orcle基本语句(四)的相关文章

Orcle基本语句(六)

1 -- Created on 2017/1/5 by ADMINISTRATOR 2 DECLARE 3 -- Local variables here 4 i INTEGER := 12; 5 v_name VARCHAR(15) := '张晓风'; 6 v_age NUMBER(3) := 28; 7 v_sex VARCHAR(4) := '男'; 8 v_height NUMBER(3) := 150; 9 v_age1 NUMBER(3); 10 11 BEGIN 12 v_age1

orcle常用语句

--1.创建临时表空间 create temporary tablespace user_temp tempfile 'D:\ORACLE\KARL\ORADATA\ORCL\AUTOMONITORV5_temp.DBF' size 50m autoextend on next 50m maxsize 10240m extent management local; --2.创建表空间 create tablespace AUTOMONITORV5 logging datafile 'D:\ORA

Mysql 语句执行顺序

转载自:http://blog.csdn.net/jintao_ma/article/details/51253356 1.这样一个问题,作为一个开发人员需要掌握数据库的哪些东西?  在开发中涉及到数据库,基本上只用到了sql语句,如何写sql以及对其进行优化就比较重要,那些mysql的厚本书籍针对的是DBA,我们只需要学习其中的sql就可以了. 2.既然会写sql是目标,那么怎么才能写好sql.学习下面几点: 1)Mysql的执行顺序,这个是写sql的核心,之前遇到的一些错误就是因为对其不了解

VBA基础知识———常用语句

语句一:if判断语句 Sub 判断1() '单条件判断 If Range("a1").Value > 0 Then Range("b1") = "正数" Else Range("b1") = "负数或0" End If End Sub Sub 判断2() '多条件判断 If Range("a1").Value > 0 Then Range("b1") =

房上的猫:for循环,跳转语句与循环结构,跳转语句进阶

一.for循环 1.定义:  for循环语句的主要作用是反复执行一段代码,直到满足一定条件为止 2.组成部分:  (1)初始部分:设置循环的初始状态  (2)循环体:重复执行的代码  (3)迭代部分:下一次循环开始前要执行的部分,在while循环结构中它作为循环体的一部分,进行循环次数的累加  (4)循环条件:判断是否继续循环的条件    注:在for循环中,这几个部分是必不可少的,不然循环就会出现错误 3.语法:  for(表达式1;表达式2;表达式3){   //循环体  }  解析:   

java语言基础与go语言基础,循环语句的区别

java语言  循环语句 for(表达式1(参数初始化):表达式2(条件判断):表达式3(更新循环变量)){ 语句循环操作: } 首先;参数的初始化,接下来条件判断,(条件成立),再执行循环操作,然后更新循环变量,又来做条件判断(条件成立),又执行循环操作:以此循环. 一,循环的三要素 1,循环变量 2,循环条件 3,循环体 二,do-while循环(先循环,再判断,至少循环一次) do{ 语句: }while(条件); 三,while循环(先判断,再循环,有可能一次也不循环) while(条件

C 运算符、表达式和语句

1.一个示例程序 示例程序 //一个对长跑运动员有用的程序 #include<stdio.h> #define S_PER_H 3600 #define S_PER_M 60 #define S_PER_K 0.62137 int main(void) { double distk,distm;//跑过的路程,公里.英里 double rate;//以英里/每小时的单位平均速度 int min,sec;//跑步用的分钟数和秒钟数 int time;//用秒表示跑步时间 double mtime

Python 操作文件模拟SQL语句功能

Python操作文件模拟SQL语句功能 一.需求 当然此表你在文件存储时可以这样表示 1,Alex Li,22,13651054608,IT,2013-04-01 现需要对这个员工信息文件,实现增删改查操作 1. 可进行模糊查询,语法至少支持下面3种: 1. select name,age from staff_table where age > 22 2. select * from staff_table where dept = "IT" 3. select * from

SQL语言的四种类型和ORACLE运算符

sql语句四种类型: 数据定义语言(DDL) 数据操作语言(DML) 数据控制语言(DCL) 事务控制语言(TCL) Data Definition Language(DDL) DDL使我们有能力创建或删除表格.也可以定义索引(键),规定表之间的链接,以及施加表间的约束. CREATE DATABASE - 创建新数据库   ALTER DATABASE - 修改数据库   CREATE TABLE - 创建新表   ALTER TABLE - 变更(改变)数据库表   DROP TABLE -