--显示员工的编号,姓名,工资,工资级别,所在部门的名称;(使用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. C#动态编译并执行代码

    先来张运行时截图: using System; using System.Collections.Generic; using System.ComponentModel; using System. ...

  2. sql server 怎么实现mysql中group_concat,列转行,列用分隔符拼接字符串

    create table tb(id int, value varchar(10)) insert into tb values(1, 'aa') insert into tb values(1, ' ...

  3. asp.net中使用基于角色role的Forms验证

    http://www.cnblogs.com/yao/archive/2006/06/24/434783.html asp.net中使用基于角色role的Forms验证,大致经过几下四步:1.配置系统 ...

  4. 为模版设计师而生的Twig(上)-Twig使用指南

    原文地址:http://my.oschina.net/veekit/blog/268828 1. 概要 模板是一个简单的文本文件.它可以生成任何基于文本的格式(HTML.XML.CSV等).它不具有特 ...

  5. AFNetwork ATS 网络层改造

    最近一直做项目的ATS改造,期间遇到了种种问题,各种坑都记录下来, 比如iOS版本.afnetwork版本.证书(是否为自签证书).域名验证.TLS版本等等,我们项目更复杂,还使用了域名到IP映射的路 ...

  6. MYSQL-用户权限的验证过程

    知识点 因为MySQL是使用User和Host两个字段来确定用户身份的,这样就带来一个问题,就是一个客户端到底属于哪个host. 如果一个客户端同时匹配几个Host,对用户的确定将按照下面的优先级来排 ...

  7. 116、Android获取Manifest中<meta-data>元素的值

    在AndroidManifest.xml中,<meta-data>元素可以作为子元素, 被包含在<activity>.<application> .<serv ...

  8. 【菜鸟学习jquery源码】数据缓存与data()

    前言 最近比较烦,深圳的工作还没着落,论文不想弄,烦.....今天看了下jquery的数据缓存的代码,参考着Aaron的源码分析,自己有点理解了,和大家分享下.以后也打算把自己的jquery的学习心得 ...

  9. 如何在Chrome下Debug Mocha的测试

    简介 经过前两篇文章的介绍,相信读者对Mocha应该有一定的认知了,本文重点讲述如何在Chrome下Debug Mocha Test, 方便你在测试fail的时候troubleshooting. 关键 ...

  10. Python基础篇【第7篇】: 面向对象(1)

    面向对象技术简介 相近对象,归为类 在人类认知中,会根据属性相近把东西归类,并且给类别命名.比如说,鸟类的共同属性是有羽毛,通过产卵生育后代.任何一只特别的鸟都在鸟类的原型基础上的.面向对象就是模拟了 ...