--显示员工的编号,姓名,工资,工资级别,所在部门的名称;(使用emp表)
SELECT * FROM emp;
SELECT * FROM salgrade;
SELECT * FROM dept;
SELECT e.empno, e.ename, e.sal, s.grade, d.dname
FROM emp e, salgrade s, dept d
WHERE e.sal BETWEEN s.losal AND s.hisal
AND e.deptno = d.deptno;
--现要求查询出每个雇员的姓名,工资,部门名称,工资在公司的等级(salgrade),及领导姓名及工资所在公司的等级
SELECT e1.empno,
e1.ename,
e1.sal,
s1.grade 员工工资等级,
d.dname,
e2.ename,
e2.sal 领导工资,
s2.grade 领导工资等级
FROM emp e1, salgrade s1, salgrade s2, dept d, emp e2
WHERE e1.sal BETWEEN s1.losal AND s1.hisal
AND e1.deptno = d.deptno
AND e1.mgr = e2.empno
AND e2.sal BETWEEN s2.losal AND s2.hisal
ORDER BY e1.sal;
--外联接
SELECT d.*, e.* FROM dept d, emp e WHERE d.deptno(+) = e.deptno; -- 右外联接(一)
SELECT d.*, e.* FROM dept d, emp e WHERE d.deptno = e.deptno(+); --左外联接(一)
SELECT d.*, e.* FROM dept d RIGHT JOIN emp e ON d.deptno = e.deptno; -- 右外联接(二)
SELECT d.*, e.* FROM dept d LEFT JOIN emp e ON d.deptno = e.deptno; -- 左外联接(二) --查询员工编号,姓名,部门编号,部门名称(左外连接)
SELECT e.empno, e.ename, e.deptno, d.dname
FROM emp e
LEFT JOIN dept d
ON e.deptno = d.deptno;
SELECT e.empno, e.ename, e.deptno, d.dname
FROM emp e, dept d
WHERE e.deptno = d.deptno(+);
--查询员工编号,姓名,部门编号,部门名称(右外连接)
SELECT e.empno, e.ename, e.deptno, d.dname
FROM emp e
RIGHT JOIN dept d
ON e.deptno = d.deptno;
SELECT e.empno, e.ename, e.deptno, d.dname
FROM emp e, dept d
WHERE e.deptno(+) = d.deptno;
--查询30号部门的员工
SELECT * FROM emp;
SELECT * FROM emp e JOIN dept d USING (deptno) WHERE deptno = 30; --查询员工编号,姓名,部门编号,部门名称,职务编号,职务名称并且薪资大于8000 按照薪资降序排列
SELECT e.employee_id,
e.last_name,
d.department_id,
d.department_name,
e.salary,
j.job_id,
j.job_title
FROM employees e
INNER JOIN departments d
ON e.department_id = d.department_id
INNER JOIN jobs j
ON e.job_id = j.job_id
WHERE e.salary > 8000
ORDER BY e.salary DESC;
COMMIT;
--查询所有部门的编号,部门名称,部门经理ID,部门经理姓名,部门所在的城市,国家,地区
SELECT * FROM departments;
SELECT * FROM locations;
SELECT * FROM employees;
SELECT * FROM countries;
SELECT d.department_id 部门编号,
d.department_name 部门名称,
d.manager_id 部门经理id,
e.last_name 部门经理姓名,
l.city 部门所在城市,
c.country_name 部门所在国家,
l.state_province 部门所在地区
FROM departments d
LEFT JOIN employees e
ON d.manager_id = e.manager_id
LEFT JOIN locations l
ON d.location_id = l.location_id
LEFT JOIN countries c
ON l.country_id = c.country_id
ORDER BY d.department_id;
--查询谁的工资比'ABEL’
select * from employees;
SELECT first_name || last_name NAME, salary
FROM employees
WHERE salary >= (SELECT salary FROM employees WHERE last_name = 'Abel')
ORDER BY salary;
----查询工资比7654高,同时与7788从事相同工作的全部雇员信息(emp)
SELECT * FROM emp;
SELECT *
FROM emp
WHERE sal > (SELECT sal FROM emp WHERE empno = 7654)
AND job = (SELECT job FROM emp WHERE empno = 7788);
--部门名称,部门员工数,部门平均工资,部门的最低收入雇员的姓名
SELECT d.dname, e.deptno, tmp.c, tmp.a, tmp.m, e.ename
FROM dept d,
emp e,
(SELECT e.deptno, COUNT(e.deptno) c, AVG(sal) a, MIN(sal) m
FROM emp e GROUP BY e.deptno) tmp
WHERE d.deptno = e.deptno
AND e.sal = tmp.m;
--求出每个部门的最低工资的雇员信息
SELECT *
FROM emp e, (SELECT MIN(sal) m FROM emp GROUP BY deptno) tmp
WHERE e.sal = tmp.m;
SELECT *
FROM emp e
WHERE e.sal in (SELECT MIN(sal) m FROM emp GROUP BY deptno);
----查询工资与10部门中的任意一个人相等的员工信息
select * from emp;
SELECT * FROM emp WHERE sal in (SELECT sal FROM emp WHERE deptno = 10);
SELECT * FROM emp WHERE sal = ANY(SELECT sal FROM emp WHERE deptno = 10);
--查询所有是部门经理的员工(employees)
SELECT * FROM employees;
SELECT *
FROM employees
WHERE employee_id IN (SELECT manager_id FROM employees) ORDER BY employee_id;
SELECT *
FROM employees e1
WHERE EXISTS(SELECT manager_id FROM employees e2 WHERE e1.employee_id = e2.manager_id) ORDER BY employee_id;
----查询所有不是部门经理的员工(employees)
SELECT *
FROM employees
WHERE employee_id <> ALL(SELECT manager_id FROM employees WHERE manager_id IS NOT NULL ) ORDER BY employee_id;
SELECT *
FROM employees
WHERE employee_id NOT IN (SELECT manager_id FROM employees WHERE manager_id IS NOT NULL) ORDER BY employee_id;
SELECT *
FROM employees e1
WHERE NOT EXISTS(SELECT manager_id FROM employees e2 WHERE e1.employee_id = e2.manager_id) ORDER BY employee_id;
--查询所有员工人数不少于3人的部门信息(employees)
SELECT * FROM departments;
SELECT * FROM employees;
SELECT d.*,tmp.c
FROM departments d INNER JOIN
(SELECT department_id,COUNT(employee_id) c FROM employees GROUP BY department_id) tmp
ON tmp.c >= 3 AND d.department_id = tmp.department_id;
----查询姓名,职位,薪资在按照 职位,薪资 去重复之后存在的数据
SELECT ename,job,sal FROM emp WHERE exists(select distinct job,sal from emp);
--查询是本部门入职最早的但不是部门经理的员工
SELECT * FROM employees;
--1.查询本部门入职最早的员工
SELECT MIN(hire_date) FROM employees GROUP BY department_id;
--2.查询不是部门经理的员工
SELECT *
FROM employees e1
WHERE NOT EXISTS(SELECT manager_id FROM employees e2 WHERE e1.employee_id = e2.manager_id) ORDER BY employee_id;
--3.结合1、2给出本部门入职最早的但不是部门经理的员工
SELECT *
FROM employees e1
WHERE NOT EXISTS
(SELECT manager_id
FROM employees e2
WHERE e1.employee_id = e2.manager_id)
AND hire_date IN
(SELECT MIN(hire_date) FROM employees GROUP BY department_id)
ORDER BY employee_id;
--查询所有工资超过本部门平均工资的员工
--1. 查询各个部门的平均工资
SELECT department_id,AVG(salary) avgsa FROM employees GROUP BY department_id;
--2.
SELECT e.*, tmp.avgsa
FROM employees e,
(SELECT department_id, AVG(salary) avgsa
FROM employees
GROUP BY department_id) tmp
WHERE e.department_id = tmp.department_id
AND e.salary > tmp.avgsa; --查询所有换过职务的员工
SELECT e.*,jh.* FROM employees e,job_history jh WHERE e.employee_id = jh.employee_id;
SELECT e.*, jh.*
FROM employees e
INNER JOIN job_history jh
ON e.employee_id = jh.employee_id;

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

  1. Orcle 查询语句

    首先,以超级管理员的身份登录oracle       sqlplus sys/bjsxt as sysdba   --然后,解除对scott用户的锁       alter user scott ac ...

  2. Orcle基本语句(三)

    COMMIT; --查询表内所有内容 SELECT * FROM stu_info; --查询部分列,并赋予别名 SELECT stu_id 学生标号,stu_name 学生姓名 FROM stu_i ...

  3. 数据库 数据库SQL语句四

    多表查询 等值连接 --查询员工信息,员工号,姓名,月薪,部门名称 select e.empno,e.ename,d.dname from emp e,dept d where e.deptno=d. ...

  4. Orcle基本语句(六)

    -- Created on 2017/1/5 by ADMINISTRATOR DECLARE -- Local variables here i ; v_name ) := '张晓风'; v_age ...

  5. ORCLE基本语句(二)

    select语句的基本语法: [ORDER BY < COLUMN1, COLUMN2, COLUMN3...> [ASC 或 DESC]]

  6. Orcle常用语句

    在SQLPlus界面的操作语句: 查看\设置每行内显示的字符数:show\set linesize [linesize] 查看\设置一次显示的行数:show\set pagesize [pagesiz ...

  7. Orcle基本语句(一)

    --创建学生信息表,约束放在所有列定义之后 CREATE TABLE stu_info( stu_id INTEGER, stu_name NVARCHAR2() NOT NULL, stu_sex ...

  8. Orcle基本语句(五)

    --分页查询---begin --sqlserver top --mysql limit --oracle rownum(伪列),oracle中伪列(rownum,rowid) --查询工资为前五的信 ...

  9. JavaScript-分支语句与函数

    一.分支语句-if语句 四种if语句: 1.if(判断条件) { 满足条件时需执行的语句 } 2.if(判断条件) { 满足条件时需执行的语句 } else { 不满足条件时需执行的语句 } 3.if ...

随机推荐

  1. window.onload与$(document).ready()区别

    2013-12-08 17:11:34 window.onload一次只能执行一个程序,而$(document).ready()可以按照先后顺序执行多个程序. eg: function one(){ ...

  2. python——socket网络编程

    一.OSI七层模型

  3. 如何解决CDR x8安装时显示“已停止工作”

    相信很多朋友在安装cdr X8时会遇到这样一种情况,能装上去,但不能运行,弹出提示说CorelDRAW X8(64-bit)已停止工作.同样的X8安装包,在不同的电脑上测试有的没有问题,有的则会出现以 ...

  4. Delphi_OD_代码_调试_Delphi反调试技术(以OD为例附核心原代码) (转)

    1.程序窗口[chuang kou]句柄[ju bing]检测原理:用FindWindow函数[han shu]查找[cha zhao]具有相同窗口[chuang kou]类名和标题的窗口[chuan ...

  5. python3-day1-python简介及入门

    python简介及入门 python简介 Python的创始人为Guido van Rossum.1989年圣诞节期间,在阿姆斯特丹,Guido为了打发圣诞节的无趣,决心开发一个新的脚本解释程序,做为 ...

  6. ajax-向服务器发送请求

    ajax-向服务器发送请求 1.将请求发送到服务器,使用XMLHttpRequest对象的 open() 和 send() 方法.     xmlhttp. open(method,url,async ...

  7. Python多线程、进程入门1

    进程是资源的一个集合, 1.一个应用程序,可以有多进程和多线程 2.默认一个程序是单进程单线程 IO操作使用多线程提高并发 计算操作使用多进程提高并发 进程与线程区别 1.线程共享内存空间,进程的内存 ...

  8. 山东省选 郁闷的小J

    小J是国家图书馆的一位图书管理员,他的工作是管理一个巨大的书架.虽然他很能吃苦耐劳,但是由于这个书架十分巨大,所以他的工作效率总是很低,以致他面临着被解雇的危险,这也正是他所郁闷的. 具体说来,书架由 ...

  9. 【java】之joda-time的使用

    Joda-Time Home:http://joda-time.sourceforge.net/ JavaDoc:http://joda-time.sourceforge.net/apidocs/in ...

  10. [git]问题list

    1. fast-forward和non fast-forward分别代表什么概念? 2. 在git中文件index是个什么概念? 3. stage/index/cache三者有什么关系? 4. git ...