Oracle数据库:oracle内连接inner join on,多表查询各种自链接、内连接、外连接的练习示例

2022找工作是学历、能力和运气的超强结合体,遇到寒冬,大厂不招人,可能很多算法学生都得去找开发,测开
测开的话,你就得学数据库,sql,oracle,尤其sql要学,当然,像很多金融企业、安全机构啥的,他们必须要用oracle数据库
这oracle比sql安全,强大多了,所以你需要学习,最重要的,你要是考网络警察公务员,这玩意你不会就别去报名了,耽误时间!
oracle系列文章:
【1】Oracle数据库:啥是oracle数据库?你为啥要学oracle?
【2】Oracle数据库:oracle 11g安装教程,已安装好的oracle各个文件夹的作用,oracle用户权限怎么样
【3】Oracle数据库:oracle启动,oracle客户端工具plsql安装教程和使用方法
【4】Oracle数据库:创建表空间,创建新用户,给用户分配对象、角色和系统权限,登录新用户建表
【5】Oracle数据库:链接配置,包括sqlnet.ora里面的transnames.ora配置数据库标识符SID,listener暂时简单了解
【6】Oracle数据库:net configureation assistant工具配置监听listener,配置本地网络访问服务器上的数据库
【7】Oracle数据库:oracle字符类型、数字类型、创建表表名的命名规则
【8】Oracle数据库:约束条件:主键约束、唯一约束、检查约束、非空约束、外键约束、默认值填写
【9】Oracle数据库:表的关系:一对多,一对一,多对多,一一继承和修改的关系,表不是重点,重点是数据和约束关系
【10】Oracle数据库:sql语言结构,数据查询语言DQL,select * from table;算术,别名,连接,去重等操作
【11】Oracle数据库:约束行限制where语句,判断条件,比较条件,字符串日期格式,in,like,escape,null语句
【12】Oracle数据库:逻辑运算and,or,not和各种运算的优先级控制
【13】Oracle数据库:排序order by语句,select from where order by的执行先后顺序,各种样例
【14】Oracle数据库:oracle函数,单行函数,多行函数,upper,lower,initcap,字符串函数
【15】Oracle数据库:数字函数,日期函数,round,trunc,mod,months_between,add_months,next_day,last_day,sysdate
【16】Oracle数据库:oracle数据类型转换to_char()日期和数字转字符,to_number()字符转数字,to_date()字符转日期函数
【17】Oracle数据库:oracle函数嵌套,nvl函数,nvl2函数,nullif函数,coalesce合并函数
【18】Oracle数据库:条件表达式case when then else end,decode函数,oracle单行函数练习示例
【19】Oracle数据库:oracle多表查询,等值连接,非等值连接,自连接的sql语句和规则
【20】Oracle数据库:oracle外连接left/right/full outer join on,oracle扩展的左右外连接展示符号(+)
【21】Oracle数据库:自然连接natural join,using语句,注意避免写交叉连接


文章目录

  • Oracle数据库:oracle内连接inner join on,多表查询各种自链接、内连接、外连接的练习示例
    • @[TOC](文章目录)
  • oracle内连接inner join on
  • 多表查询练习示例
  • 总结

oracle内连接inner join on


等值和非等值都可以


城市需要别的表格
还有部门名称需要一个表格

就要俩条件

SQL> select e.last_name,d.department_name,l.city from employees e,departments d, locations l where e.department_id = d.department_id and d.location_id = l.location_id and e.employee_id=202;LAST_NAME                 DEPARTMENT_NAME                CITY
------------------------- ------------------------------ ------------------------------
Fay                       Marketing                      Toronto

这老复杂了
其实这不复杂

可以用inner join试试,更复杂………………

SQL> select e.last_name,d.department_name,l.city from employees e inner join departments d on e.department_id = d.department_id inner join locations l on d.location_id = l.location_id where e.employee_id=202;LAST_NAME                 DEPARTMENT_NAME                CITY
------------------------- ------------------------------ ------------------------------
Fay                       Marketing                      Toronto

中间两次连接很烦,挺狗的啊感觉,谁发明的…………

只是帮助做等值连接和非等值链接的一种方法
不建议写这种
当然为了考试,也就了解一下

SQL> select e.last_name,d.department_name,l.city from employees e inner join departments d on e.department_id = d.department_id inner join locations l on d.location_id = l.location_id where e.employee_id>202;LAST_NAME                 DEPARTMENT_NAME                CITY
------------------------- ------------------------------ ------------------------------
Gietz                     Accounting                     Seattle
Higgins                   Accounting                     Seattle
Mavris                    Human Resources                London
Baer                      Public Relations               Munich

俩性能没区别

inner join中可以用using语句的

SQL> select e.last_name,d.department_name,l.city from employees e inner join departments d on e.department_id = d.department_id inner join locations l using(location_id) where e.employee_id>202;LAST_NAME                 DEPARTMENT_NAME                CITY
------------------------- ------------------------------ ------------------------------
Gietz                     Accounting                     Seattle
Higgins                   Accounting                     Seattle
Mavris                    Human Resources                London
Baer                      Public Relations               Munich
SQL> select e.last_name,department_name,l.city from employees e inner join departments using(department_id) inner join locations l using(location_id) where e.employee_id>202;LAST_NAME                 DEPARTMENT_NAME                CITY
------------------------- ------------------------------ ------------------------------
Gietz                     Accounting                     Seattle
Higgins                   Accounting                     Seattle
Mavris                    Human Resources                London
Baer                      Public Relations               Munich

也还行反正就这样吧

多表查询练习示例


复习
俩表
employee表和departments表
链接

SQL> select e.last_name,d.department_name from employees e,departments d where e.department_id= d.department_id;LAST_NAME                 DEPARTMENT_NAME
------------------------- ------------------------------
Whalen                    Administration
Fay                       Marketing
Hartstein                 Marketing
Tobias                    Purchasing

这是等值连接

SQL> select e.last_name,d.department_name from employees e inner join departments d on e.department_id= d.department_id;LAST_NAME                 DEPARTMENT_NAME
------------------------- ------------------------------
Whalen                    Administration
Fay                       Marketing
Hartstein                 Marketing
Tobias                    Purchasing
Colmenares                Purchasing

则是内连接,好说
on子句
inner join
可以用using语句

SQL> select e.last_name,department_name from employees e inner join departments d using(department_id);LAST_NAME                 DEPARTMENT_NAME
------------------------- ------------------------------
Whalen                    Administration
Fay                       Marketing
Hartstein                 Marketing
Tobias                    Purchasing

using语句挺简单的
舒服

查部门编号,地点编号,location_id,在departments表中有的

SQL> desc departments;
Name            Type         Nullable Default Comments
--------------- ------------ -------- ------- ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
DEPARTMENT_ID   NUMBER(4)                     Primary key column of departments table.
DEPARTMENT_NAME VARCHAR2(30)                  A not null column that shows name of a department. Administration,
Marketing, Purchasing, Human Resources, Shipping, IT, Executive, Public
Relations, Sales, Finance, and Accounting.
MANAGER_ID      NUMBER(6)    Y                Manager_id of a department. Foreign key to employee_id column of employees table. The manager_id column of the employee table references this column.
LOCATION_ID     NUMBER(4)    Y                Location id where a department is located. Foreign key to location_id column of locations table.                                                                                         

DEPARTMENT_ID 在本表中

下面连接条件俩表用department_id
行选择条件也可以加上

SQL> select e.job_id,d.location_id from employees e, departments d where e.department_id=d.department_id and  e.department_id=80;JOB_ID     LOCATION_ID
---------- -----------
SA_MAN            2500
SA_MAN            2500
SA_MAN            2500
SA_MAN            2500
SA_MAN            2500
SA_REP            2500
SA_REP            2500
SA_REP            2500
SA_REP            2500
SA_REP            2500
SA_REP            2500
SA_REP            2500
SA_REP            2500
SA_REP            2500
SA_REP            2500
SA_REP            2500
SA_REP            2500
SA_REP            2500
SA_REP            2500
SA_REP            2500JOB_ID     LOCATION_ID
---------- -----------
SA_REP            2500
SA_REP            2500
SA_REP            2500
SA_REP            2500
SA_REP            2500
SA_REP            2500
SA_REP            2500
SA_REP            2500
SA_REP            2500
SA_REP            2500
SA_REP            2500
SA_REP            2500
SA_REP            2500
SA_REP            250034 rows selected

你可以发现很多job_id是重复的
去重可以用distinct,放在select之后
对列选择

SQL> select distinct e.job_id,d.location_id from employees e, departments d where e.department_id=d.department_id and  e.department_id=80;JOB_ID     LOCATION_ID
---------- -----------
SA_REP            2500
SA_MAN            2500


employees表
departments表
locations表

2个链接表的条件
条件,有佣金,is not null

SQL> select e.last_name, d.department_name, l.city from employees e,departments d,locations l where e.department_id = d.department_id and d.location_id = l.location_id and e.commission_pct is not null;LAST_NAME                 DEPARTMENT_NAME                CITY
------------------------- ------------------------------ ------------------------------
Russell                   Sales                          Oxford
Partners                  Sales                          Oxford
Errazuriz                 Sales                          Oxford
Cambrault                 Sales                          Oxford
Zlotkey                   Sales                          Oxford
Tucker                    Sales                          Oxford
Bernstein                 Sales                          Oxford
Hall                      Sales                          Oxford
Olsen                     Sales                          Oxford
Cambrault                 Sales                          Oxford
Tuvault                   Sales                          Oxford
King                      Sales                          Oxford
Sully                     Sales                          Oxford
McEwen                    Sales                          Oxford
Smith                     Sales                          Oxford
Doran                     Sales                          Oxford
Sewall                    Sales                          Oxford
Vishney                   Sales                          Oxford
Greene                    Sales                          Oxford
Marvins                   Sales                          OxfordLAST_NAME                 DEPARTMENT_NAME                CITY
------------------------- ------------------------------ ------------------------------
Lee                       Sales                          Oxford
Ande                      Sales                          Oxford
Banda                     Sales                          Oxford
Ozer                      Sales                          Oxford
Bloom                     Sales                          Oxford
Fox                       Sales                          Oxford
Smith                     Sales                          Oxford
Bates                     Sales                          Oxford
Kumar                     Sales                          Oxford
Abel                      Sales                          Oxford
Hutton                    Sales                          Oxford
Taylor                    Sales                          Oxford
Livingston                Sales                          Oxford
Johnson                   Sales                          Oxford34 rows selected


模糊匹配like
好说
俩表

SQL> select e.last_name,d.department_name from employees e,departments d where e.department_id = d.department_id and e.last_name like '%a%';LAST_NAME                 DEPARTMENT_NAME
------------------------- ------------------------------
Whalen                    Administration
Fay                       Marketing
Hartstein                 Marketing
Baida                     Purchasing


城市locations表
名字employees表
departments表也要
仨表
内连接两次
挺烦

SQL> select e.last_name,d.department_name,l.city from employees e inner join departments d on e.department_id = d.department_id inner join locations l on d.location_id = l.location_id where l.city='Toronto';LAST_NAME                 DEPARTMENT_NAME                CITY
------------------------- ------------------------------ ------------------------------
Hartstein                 Marketing                      Toronto
Fay                       Marketing                      Toronto
SQL> select e.last_name,e.job_id,d.department_name,l.city from employees e inner join departments d on e.department_id = d.department_id inner join locations l on d.location_id = l.location_id where l.city='Toronto';LAST_NAME                 JOB_ID     DEPARTMENT_NAME                CITY
------------------------- ---------- ------------------------------ ------------------------------
Hartstein                 MK_MAN     Marketing                      Toronto
Fay                       MK_REP     Marketing                      Toronto


自连接

SQL> select emp.last_name "Emp",emp.employee_id "Emp#", manager.last_name "Manager",manager.employee_id "Manager#" from employees emp, employees manager where emp.manager_id = manager.employee_id;Emp                          Emp# Manager                   Manager#
------------------------- ------- ------------------------- --------
Kumar                         173 Cambrault                      148
Bates                         172 Cambrault                      148
Smith                         171 Cambrault                      148
Fox                           170 Cambrault                      148
Bloom                         169 Cambrault                      148
Ozer                          168 Cambrault                      148

经理和雇员都是雇员
所以雇员的雇员编号好搞
但是经理的雇员编号需要自连接,懂???

这个好好屡清楚,骚得很


左外,右外都行
最好把员工描述为左表
经理表用作右表

SQL> select emp.last_name "Emp",emp.employee_id "Emp#", manager.last_name "Manager",manager.employee_id "Manager#" from employees emp left outer join employees manager on emp.manager_id = manager.employee_id;Emp                          Emp# Manager                   Manager#
------------------------- ------- ------------------------- --------
Hutton                        175 Zlotkey                        149
Abel                          174 Zlotkey                        149
King                          100                           107 rows selected

你看king就出来了
刺激

和上一个语句的区别就是左外链接展示坐标孤儿数据


同一个部门的同事
员工的部门id相同
一个员工表,一个同事表

貌似很多结果

SQL> select e.last_name,e.department_id from employees e,employees c where e.department_id=c.department_id and e.employee_id !=c.employee_id;LAST_NAME                 DEPARTMENT_ID
------------------------- -------------
Higgins                             110
3192 rows selected

很多同事

查询雇员的薪资等级,A–F

employees表
departments表
job_grades表

SQL> select e.last_name,e.job_id,d.department_name,e.salary,j.gra from employees e, departments d, job_grades j where e.department_id = d.department_id and e.salary between j.losest_sal and j.highest_sal;LAST_NAME                 JOB_ID     DEPARTMENT_NAME                    SALARY GRA
------------------------- ---------- ------------------------------ ---------- ----------
King                      AD_PRES    Executive                        24000.00 E
Kochhar                   AD_VP      Executive                        17000.00 E
De Haan                   AD_VP      Executive                        17000.00 E
Russell                   SA_MAN     Sales                            14000.00 D
Partners                  SA_MAN     Sales                            13500.00 D

非等值链接

懂?

一个是入职之前的表,一个是之后的表

SQL> select e.last_name, e.hire_date from employees e, employees d where d.hire_date < e.hire_date and d.last_name='Davies';LAST_NAME                 HIRE_DATE
------------------------- -----------
Kochhar                   2005/9/21
Hunold                    2006/1/3
Ernst                     2007/5/21
Austin                    2005/6/25
Pataballa                 2006/2/5
Lorentz                   2007/2/7
Chen                      2005/9/28
Sciarra                   2005/9/30
Urman                     2006/3/7
Popp                      2007/12/7
Baida                     2005/12/24
Tobias                    2005/7/24
Himuro                    2006/11/15
Colmenares                2007/8/10
Fripp                     2005/4/10
Vollman                   2005/10/10
Mourgos                   2007/11/16
Nayer                     2005/7/16
Mikkilineni               2006/9/28
Landry                    2007/1/14LAST_NAME                 HIRE_DATE
------------------------- -----------
Markle                    2008/3/8
Bissot                    2005/8/20
Atkinson                  2005/10/30
Marlow                    2005/2/16
Olson                     2007/4/10
Rogers                    2006/8/26
Gee                       2007/12/12
Philtanker                2008/2/6
Stiles                    2005/10/26
Seo                       2006/2/12
Patel                     2006/4/6
Matos                     2006/3/15
Vargas                    2006/7/9
Errazuriz                 2005/3/10
Cambrault                 2007/10/15
Zlotkey                   2008/1/29
Tucker                    2005/1/30
Bernstein                 2005/3/24
Hall                      2005/8/20
Olsen                     2006/3/30
Cambrault                 2006/12/9LAST_NAME                 HIRE_DATE
------------------------- -----------
Tuvault                   2007/11/23
Smith                     2005/3/10
Doran                     2005/12/15
Sewall                    2006/11/3
Vishney                   2005/11/11
Greene                    2007/3/19
Marvins                   2008/1/24
Lee                       2008/2/23
Ande                      2008/3/24
Banda                     2008/4/21
Ozer                      2005/3/11
Bloom                     2006/3/23
Fox                       2006/1/24
Smith                     2007/2/23
Bates                     2007/3/24
Kumar                     2008/4/21
Hutton                    2005/3/19
Taylor                    2006/3/24
Livingston                2006/4/23
Grant                     2007/5/24
Johnson                   2008/1/4LAST_NAME                 HIRE_DATE
------------------------- -----------
Taylor                    2006/1/24
Fleaur                    2006/2/23
Sullivan                  2007/6/21
Geoni                     2008/2/3
Bull                      2005/2/20
Dellinger                 2006/6/24
Cabrio                    2007/2/7
Chung                     2005/6/14
Dilly                     2005/8/13
Gates                     2006/7/11
Perkins                   2007/12/19
Everett                   2005/3/3
McCain                    2006/7/1
Jones                     2007/3/17
Walsh                     2006/4/24
Feeney                    2006/5/23
OConnell                  2007/6/21
Grant                     2008/1/13
Fay                       2005/8/1781 rows selected


又是自连接

SQL> select e.last_name, e.hire_date, m.last_name, m.hire_date from employees e, employees m where e.manager_id = m.employee_id and e.hire_date < m.hire_date;LAST_NAME                 HIRE_DATE   LAST_NAME                 HIRE_DATE
------------------------- ----------- ------------------------- -----------
Kaufling                  2003/5/1    King                      2003/6/17
Raphaely                  2002/12/7   King                      2003/6/17
De Haan                   2001/1/13   King                      2003/6/17
Higgins                   2002/6/7    Kochhar                   2005/9/21
Baer                      2002/6/7    Kochhar                   2005/9/21
Mavris                    2002/6/7    Kochhar                   2005/9/21
Whalen                    2003/9/17   Kochhar                   2005/9/21
Greenberg                 2002/8/17   Kochhar                   2005/9/21
Austin                    2005/6/25   Hunold                    2006/1/3
Faviet                    2002/8/16   Greenberg                 2002/8/17
Bull                      2005/2/20   Fripp                     2005/4/10
Sarchand                  2004/1/27   Fripp                     2005/4/10
Marlow                    2005/2/16   Fripp                     2005/4/10
Everett                   2005/3/3    Vollman                   2005/10/10
Bell                      2004/2/4    Vollman                   2005/10/10
Ladwig                    2003/7/14   Vollman                   2005/10/10
OConnell                  2007/6/21   Mourgos                   2007/11/16
Feeney                    2006/5/23   Mourgos                   2007/11/16
Walsh                     2006/4/24   Mourgos                   2007/11/16
Vargas                    2006/7/9    Mourgos                   2007/11/16LAST_NAME                 HIRE_DATE   LAST_NAME                 HIRE_DATE
------------------------- ----------- ------------------------- -----------
Matos                     2006/3/15   Mourgos                   2007/11/16
Davies                    2005/1/29   Mourgos                   2007/11/16
Rajs                      2003/10/17  Mourgos                   2007/11/16
McEwen                    2004/8/1    Partners                  2005/1/5
Sully                     2004/3/4    Partners                  2005/1/5
King                      2004/1/30   Partners                  2005/1/5
Bates                     2007/3/24   Cambrault                 2007/10/15
Smith                     2007/2/23   Cambrault                 2007/10/15
Fox                       2006/1/24   Cambrault                 2007/10/15
Bloom                     2006/3/23   Cambrault                 2007/10/15
Ozer                      2005/3/11   Cambrault                 2007/10/15
Johnson                   2008/1/4    Zlotkey                   2008/1/29
Grant                     2007/5/24   Zlotkey                   2008/1/29
Livingston                2006/4/23   Zlotkey                   2008/1/29
Taylor                    2006/3/24   Zlotkey                   2008/1/29
Hutton                    2005/3/19   Zlotkey                   2008/1/29
Abel                      2004/5/11   Zlotkey                   2008/1/2937 rows selected

员工是在经理入职之前就入职的

别名随意了

OK,完成多表查询各种骚操作


总结

提示:重要经验:

1)
2)学好oracle,即使经济寒冬,整个测开offer绝对不是问题!同时也是你考公网络警察的必经之路。
3)笔试求AC,可以不考虑空间复杂度,但是面试既要考虑时间复杂度最优,也要考虑空间复杂度最优。

Oracle数据库:oracle内连接inner join on,多表查询各种自链接、内连接、外连接的练习示例相关推荐

  1. Oracle数据库:创建和删除视图view,简单和复杂视图,内建视图,topN分析,oracle分页查询

    Oracle数据库:创建和删除视图view,简单和复杂视图,内建视图,topN分析,oracle分页查询 2022找工作是学历.能力和运气的超强结合体,遇到寒冬,大厂不招人,可能很多算法学生都得去找开 ...

  2. Oracle实例和Oracle数据库(Oracle体系结构)

    --========================================== --Oracle实例和Oracle数据库(Oracle体系结构) --==================== ...

  3. java查询oracle数据库_Oracle数据库之java 从Oracle数据库到处数据到Elasticsearch全文检索库进行全文查询...

    本文主要向大家介绍了Oracle数据库之java 从Oracle数据库到处数据到Elasticsearch全文检索库进行全文查询,通过具体的内容向大家展现,希望对大家学习Oracle数据库有所帮助. ...

  4. 将oracle端口1521共享_oracle端口1521映射 怎么使用全端口映射实现外网访问内网oracle数据库 - Oracle - 服务器之家...

    oracle端口1521映射 怎么使用全端口映射实现外网访问内网oracle数据库 发布时间:2017-03-21 来源:服务器之家 内网使用nat123端口映射之全端口映射可以将内网oracle数据 ...

  5. empinfo Oracle数据库,Oracle数据库中相关技术详细操作

    1.Distinct消除重复行数据:SELECT DISTINCT DEPTNO FROM EMP 2.NULL操作 如果某条记录中有缺少的数据值,就是空值(NULL值).空值不等于0或者空格,空值是 ...

  6. linux下imp导入oracle数据库,Oracle数据库Linux下的导入IMP

    和相关篇的EXP相对应的用了如下的导入方法. [oracle@localhost ~]$ imp Import: Release 11.2.0.1.0 - Production on Fri Sep ...

  7. cmd下dir后导入oracle数据库,Oracle在dos命令下导出导入

    DOS下运行的命令,也可以加参数在SQL/PLUS环境下运行 简单例子实现 单表备份(前提库的结构是一样的) 导出: 开始钮->运行->输入CMD->进入DOS界面 EXP 用户名/ ...

  8. Oracle数据库--Oracle作业基础知识整合

    一.Oracle启动服务有哪些?说说它们各自的功能. Oracle ORCL VSS Writer Service:Oracle卷映射拷贝写入服务,VSS(Volume Shadow Copy Ser ...

  9. 不知道密码导出oracle数据库,Oracle数据库密码重置、导入导出库命令示例应用

    重置办法如下:打开CMD命令提示符,然后输入下面命令进行重置: 输入sqlplus /nolog,回车 SQL> conn /as sysdba 已连接: SQL>alter user s ...

最新文章

  1. 为.Net程序集添加资源
  2. java 集合 CopyOnWriteArrayList
  3. js根据数组中对象的多个属性值进行排序
  4. Windows 系统防火墙
  5. ChaiNext:比特币打穿4.2万美元,市场多空胶着
  6. 成长,责任,和公司的关系
  7. c+命名空间_了解C ++中的命名空间
  8. SUSE12Sp3安装配置.net core 生产环境-总汇(持续更新中...)
  9. 老罗Android视频教程第二季(全套视频+源码)
  10. 【svn】svn分支(branch)如何同步主干(trunk)的代码?
  11. Unity3d十二 3d主要引擎名称Ogre Unreal Unity Gamebryo Bigworld
  12. dnf时装补丁教程_【时装补丁制作】消灭伸手党!最详细的图文教程~
  13. 制作MIcroUSB转TTL下载器CH340
  14. 基于激光雷达的室内探测系统
  15. 2017年中兴捧月神算师算法精英挑战赛之阿尔法勒克斯特派(AlphaNext派)-----进来加群咯
  16. 技术总监/技术leader 职责与工作记录 第一天
  17. 计算机培训报名表 自我介绍,单招报名表自我介绍
  18. 【云服务器】阿里云部署项目、攻击CPU解决方案
  19. IBM AS400教程(完整版)
  20. python计算数学题_「每日一练」巧用python做小学的数学题

热门文章

  1. 易宝典——玩转O365中的EXO服务 之四十九 如何知道管理员进行了哪些操作
  2. 十六周项目一----冒泡排序
  3. 计算机怎么看显卡内存容量,显卡内存怎么看,显卡容量大小查看步骤
  4. 浅读C Primer Plus——C语起源
  5. Codeforces 1077B Disturbed People(思维题)
  6. Window10 IoT家庭物联网网关系统设计
  7. 64位 window 在notepad++中添加HexEditor工具
  8. 2014-07-23 .NET实现微信公众号接入
  9. Android 获取横竖屏状态
  10. 2022.6.14日新selenium写法