版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、Displaying Data from Multiple TablesObjectivesAfter completing this lesson, you should be able to do the following:Write SELECT statements to access data from more than one table using equijoins and non-equijoinsJoin a table to itself by using a self-joinView data that generally does not meet a join
2、 condition by using outer joinsGenerate a Cartesian product of all rows from two or more tablesObtaining Data from Multiple TablesEMPLOYEES DEPARTMENTS Types of JoinsJoins that are compliant with the SQL:1999 standard include the following:Cross joinsNatural joinsUSING clauseFull (or two-sided) oute
3、r joinsArbitrary join conditions for outer joinsJoining Tables Using SQL:1999 SyntaxUse a join to query data from more than one table:SELECTtable1.column, table2.columnFROMtable1NATURAL JOIN table2 |JOIN table2 USING (column_name) |JOIN table2 ON (table1.column_name = table2.column_name)|LEFT|RIGHT|
4、FULL OUTER JOIN table2 ON (table1.column_name = table2.column_name)|CROSS JOIN table2;Creating Natural JoinsThe NATURAL JOIN clause is based on all columns in the two tables that have the same name.It selects rows from the two tables that have equal values in all matched columns.If the columns havin
5、g the same names have different data types, an error is returned.SELECT department_id, department_name, location_id, cityFROM departmentsNATURAL JOIN locations ;Retrieving Records with Natural JoinsCreating Joins with the USING ClauseIf several columns have the same names but the data types do not m
6、atch, the NATURAL JOIN clause can be modified with the USING clause to specify the columns that should be used for an equijoin.Use the USING clause to match only one column when more than one column matches.Do not use a table name or alias in the referenced columns.The NATURAL JOIN and USING clauses
7、 are mutually exclusive.Joining Column NamesEMPLOYEES DEPARTMENTS Foreign keyPrimary keySELECT employees.employee_id, employees.last_name, departments.location_id, department_idFROM employees JOIN departmentsUSING (department_id) ;Retrieving Records with the USING ClauseQualifying Ambiguous Column N
8、amesUse table prefixes to qualify column names that are in multiple tables.Use table prefixes to improve performance.Use column aliases to distinguish columns that have identical names but reside in different tables.Do not use aliases on columns that are identified in the USING clause and listed els
9、ewhere in the SQL statement.SELECT e.employee_id, e.last_name, d.location_id, department_idFROM employees e JOIN departments dUSING (department_id) ;Using Table AliasesUse table aliases to simplify queries.Use table aliases to improve performance.Creating Joins with the ON ClauseThe join condition f
10、or the natural join is basically an equijoin of all columns with the same name.Use the ON clause to specify arbitrary conditions or specify columns to join.The join condition is separated from other search conditions.The ON clause makes code easy to understand.SELECT e.employee_id, e.last_name, e.de
11、partment_id, d.department_id, d.location_idFROM employees e JOIN departments dON (e.department_id = d.department_id);Retrieving Records with the ON ClauseSelf-Joins Using the ON ClauseMANAGER_ID in the WORKER table is equal to EMPLOYEE_ID in the MANAGER table.EMPLOYEES (WORKER)EMPLOYEES (MANAGER)Sel
12、f-Joins Using the ON ClauseSELECT e.last_name emp, m.last_name mgrFROM employees e JOIN employees mON (e.manager_id = m.employee_id);SELECT e.employee_id, e.last_name, e.department_id, d.department_id, d.location_idFROM employees e JOIN departments dON (e.department_id = d.department_id)AND e.manage
13、r_id = 149 ;Applying Additional Conditionsto a JoinSELECT employee_id, city, department_nameFROM employees e JOIN departments dON d.department_id = e.department_id JOIN locations lON d.location_id = l.location_id;Creating Three-Way Joins with the ON ClauseNon-EquijoinsEMPLOYEESJOB_GRADESSalary in th
14、e EMPLOYEES table must be between lowest salary and highest salary in the JOB_GRADEStable.SELECT e.last_name, e.salary, j.grade_levelFROM employees e JOIN job_grades jON e.salary BETWEEN j.lowest_sal AND j.highest_sal;Retrieving Records with Non-EquijoinsOuter JoinsEMPLOYEESDEPARTMENTSThere are no e
15、mployees in department 190. INNER Versus OUTER JoinsIn SQL:1999, the join of two tables returning only matched rows is called an inner join.A join between two tables that returns the results of the inner join as well as the unmatched rows from the left (or right) tables is called a left (or right) o
16、uter join.A join between two tables that returns the results of an inner join as well as the results of a left and right join is a full outer join.SELECT e.last_name, e.department_id, d.department_nameFROM employees e LEFT OUTER JOIN departments dON (e.department_id = d.department_id) ;LEFT OUTER JO
17、INSELECT e.last_name, e.department_id, d.department_nameFROM employees e RIGHT OUTER JOIN departments dON (e.department_id = d.department_id) ;RIGHT OUTER JOINSELECT e.last_name, d.department_id, d.department_nameFROM employees e FULL OUTER JOIN departments dON (e.department_id = d.department_id) ;F
18、ULL OUTER JOINCartesian ProductsA Cartesian product is formed when:A join condition is omittedA join condition is invalidAll rows in the first table are joined to all rows in the second tableTo avoid a Cartesian product, always include a valid join condition.Generating a Cartesian ProductCartesian product: 20 x 8 = 160 rowsEMPLO
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 减压渣油加氢脱硫项目可行性研究报告
- 2026年广东轻工职业技术学院单招职业倾向性考试题库附答案详解(夺分金卷)
- 2026年广东茂名农林科技职业学院单招综合素质考试题库含答案详解(达标题)
- 2026年山西运城农业职业技术学院单招职业适应性考试题库附答案详解(综合卷)
- 2026年广东舞蹈戏剧职业学院单招职业技能考试题库及答案详解(网校专用)
- 2026年平顶山文化艺术职业学院单招职业适应性测试题库带答案详解(巩固)
- 2026年广西安全工程职业技术学院单招综合素质考试题库带答案详解(模拟题)
- 2026年嵩山少林武术职业学院单招职业倾向性测试题库带答案详解(培优a卷)
- 2026年山西经贸职业学院单招职业技能考试题库有完整答案详解
- 2026年广东理工职业学院单招职业倾向性考试题库及答案详解(必刷)
- 我心中的老师班会课件
- 低空经济试题及答案
- 养老院安全生产教育培训内容
- 设备设施停用管理制度
- 山东高考英语语法单选题100道及答案
- 职业道德与法治知识点总结中职高教版
- 2025年绿色低碳先进技术示范工程实施方案-概述及范文模板
- 2025上半年广西现代物流集团社会招聘校园招聘149人笔试参考题库附带答案详解
- 事故后企业如何进行危机公关与赔偿管理
- 2025年春新人教PEP版英语三年级下册全册教案
- OptixOSN3500智能光传输设备业务配置手册
评论
0/150
提交评论