Database Programming Sections 4 – Joins. Marge Hohly2 Overview  Oracle Proprietary Joins (8i and prior): Cartesian Product Equijoin Non-equijoin Outer.

Slides:



Advertisements
Similar presentations
SQL/PL SQL Oracle By Rana Umer. Quiz 2 Q1.Create a table called "Persons" that contains five columns: PersonID, LastName, FirstName, Address, and City.
Advertisements

Chapter 4 Joining Multiple Tables
Displaying Data from Multiple Tables
SQL Review Sections 1 - SQL and other basic statements.
Database Programming Sections 5 & 6 – Group functions, COUNT, DISTINCT, NVL, GROUP BY, HAVING clauses, Subqueries.
Notice that No where clause In the syntax.
Writing Basic SQL SELECT Statements. Capabilities of SQL SELECT Statements A SELECT statement retrieves information from the database. Using a SELECT.
18 Copyright © Oracle Corporation, All rights reserved. Advanced Subqueries.
5 Copyright © 2004, Oracle. All rights reserved. Displaying Data from Multiple Tables.
Database Programming Sections 4– Review of Joins, Group functions, COUNT, DISTINCT, NVL.
4 Copyright © Oracle Corporation, All rights reserved. Displaying Data from Multiple Tables.
Inner join, self join and Outer join Sen Zhang. Joining data together is one of the most significant strengths of a relational database. A join is a query.
Displaying Data from Multiple Tables. Obtaining Data from Multiple Tables Sometimes you need to use data from more than one table. In the example, the.
Objectives After completing this lesson, you should be able to do the following: Write SELECT statements to access data from more than one table using.
Objectives After completing this lesson, you should be able to do the following: Define subqueries Describe the types of problems that the subqueries.
1 Copyright © 2009, Oracle. All rights reserved. B Table Descriptions.
4 Copyright © Oracle Corporation, All rights reserved. Displaying Data from Multiple Tables.
4 Copyright © Oracle Corporation, All rights reserved. Displaying Data from Multiple Tables.
SQL Joins Oracle and ANSI Standard SQL Lecture 6.
4 Copyright © Oracle Corporation, All rights reserved. Displaying Data from Multiple Tables.
Chapter 9 Joining Data from Multiple Tables
Sections 3 – Joins & Hierarchical Queries
SQL advanced select using Oracle 1 7. Multiple Tables: Joins and Set Operations 8. Subqueries: Nested Queries.
Copyright © 2004, Oracle. All rights reserved. Lecture 6 Displaying Data from Multiple Tables ORACLE.
(with Microsoft SQL Server) Svetlin Nakov Telerik Corporation
Basisdata Pertanian. After completing this lesson, you should be able to do the following:  Write SELECT statements to access data from more than one.
Database Programming Sections 6 –Subqueries, Single Row Subqueries, Multiple-column subqueries, Multiple-row Subqueries, Correlated Subqueries 11/2/10,
Chapter 4Introduction to Oracle9i: SQL1 Chapter 4 Joining Multiple Tables.
Multiple Table Queries (Inner Joins) Week 3. Objective –Write SELECT statements to display data from more than one table using inner joins.
Join, Subqueries and set operators. Obtaining Data from Multiple Tables EMPLOYEES DEPARTMENTS … …
4 Copyright © Oracle Corporation, All rights reserved. Displaying Data from Multiple Tables.
Copyright © 2004, Oracle. All rights reserved. D ISPLAYING D ATA FROM M ULTIPLE T ABLES.
Manipulating Large Data Sets. Lesson Agenda ◦ Manipulating data using subqueries ◦ Specifying explicit default values in the INSERT and UPDATE statements.
4 Copyright © 2007, Oracle. All rights reserved. Manipulating Large Data Sets.
Copyright © 2004, Oracle. All rights reserved. MANIPULATING LARGE DATA SETS Oracle Lecture 10.
5 Copyright © 2004, Oracle. All rights reserved. Displaying Data from Multiple Tables.
5 Copyright © 2004, Oracle. All rights reserved. Displaying Data from Multiple Tables.
Intermediate SQL: Aggregated Data, Joins and Set Operators.
SQL1-ch5 顯示多個表格的資料. 題號  80 題: 34 、 57 、 71 、 72  140 題: 18 、 25 、 62 、 97 、 115 、 131.
Database Programming Section 15 – Oracle Proprietary Join Syntax and Review 1.
4 Copyright © Oracle Corporation, All rights reserved. Displaying Data from Multiple Tables.
Displaying Data from Multiple Tables (SQL99 Syntax with examples)
CSED421 Database Systems Lab Join. Human Resources (HR) schema.
DML Part 1 Yogiek Indra Kurniawan. All Files Can Be Downloaded at : Menu : “Perkuliahan”
Database Programming Sections 3 – Oracle Joins. Marge Hohly2 Obtaining Data from Multiple Tables: Using Joins.
6 Copyright © 2007, Oracle. All rights reserved. Retrieving Data Using Subqueries.
In this session, you will learn to: Query data by using joins Query data by using subqueries Objectives.
Join CSED421 Database Systems Lab. Joining Tables Use a join to query data from more than one table. SELECTtable1.column, table2.column FROMtable1, table2.
Database Programming Sections 6 –Subqueries, Single Row Subqueries, Multiple-row Subqueries, Correlated Subqueries.
 MySQL  DDL ◦ Create ◦ Alter  DML ◦ Insert ◦ Select ◦ Update ◦ Delete  DDL(again) ◦ Drop ◦ Truncate.
5-1 Copyright © 2004, Oracle. All rights reserved. DISPLAYING DATA FROM MULTIPLE TABLES OUTER JOIN.
4 Copyright © 2004, Oracle. All rights reserved. Displaying Data from Multiple Tables.
4 Copyright © Oracle Corporation, All rights reserved. Displaying Data from Multiple Tables.
SQL Introduction. Table of Contents 1. SQL and T-SQL Languages 2. The “SoftUni” Database Schema 3. Introducing the SELECT SQL Statement  SQL Operators.
6 Copyright © 2006, Oracle. All rights reserved. Retrieving Data Using Subqueries.
Joins, Subqueries, CTE and Indices
Displaying Data from Multiple Tables
Oracle Join Syntax.
Displaying Data from Multiple Tables
Displaying Data from Multiple Tables
Displaying Data from Multiple Tables
Displaying Data from Multiple Tables Using Joins
Structured Query Language (II) (結構化查詢語言)
Sections 4– Review of Joins, Group functions, COUNT, DISTINCT, NVL
Oracle Join Syntax.
Oracle Join Syntax.
Displaying Data from Multiple Tables
Using Subqueries to Solve Queries
Displaying Data from Multiple Tables
Displaying Data from Multiple Tables
Presentation transcript:

Database Programming Sections 4 – Joins

Marge Hohly2 Overview  Oracle Proprietary Joins (8i and prior): Cartesian Product Equijoin Non-equijoin Outer join Self join  SQL: 1999 Compliant Joins: Cross joins Natural joins Using clause Full or two sided outer joins Arbitrary join conditions for outer joins

Marge Hohly3 Cross Join  ANSI/ISO SQL: 1999 syntax for achieving of a Cartesian product  Syntax: SELECT * FROM employees CROSS JOIN departments; Last week: SELECT * FROM employees, departments;

Marge Hohly4 Natural Join  ANSI/ISO SQL: 1999 Join equivalent of an equijoin  Join on all common columns ie. columns with same name and data type  SYNTAX: SELECT field1, field2 FROM table1 NATURAL JOIN table2 WHERE fieldn = value;

Marge Hohly5 Example  SELECT event_id, song_id, cd_number FROM d_play_list_items NATURAL JOIN d_track_listings WHERE event_id = 105;

Marge Hohly6 Example Natural Join  SELECT employee_id, last_name, department_id, location_id FROM employees NATURAL JOIN departments;

Marge Hohly Use an equijoin between the DJs on Demand database tables, d_songs and d_types. Display the type code, description and title. Limit the rows returned to those type codes between 70 and 80.

Marge Hohly8 Join Using  ANSI/ISO SQL: 1999 join condition used to join two tables on only one column  Typically used where NATURAL JOIN cannot be used because there are other common columns with same name and different data types  No table name or alias can be used on the referenced column anywhere in the statement

Marge Hohly9 Join Using  SYNTAX: SELECT field1, field2, field3 FROM table1 JOIN table2 USING(column name);  Example: SELECT e.employee_id, e.last_name, d.location_id FROM employees e JOIN departments d USING(department_id);

Marge Hohly10 Example  SELECT e.employee_id, e.last_name, d.location_id FROM employees e JOIN departments d USING(department_id);

Marge Hohly11 JOIN ON  ANSI/ISO: 1999 join clause that may be used to specify the condition or columns used:  Syntax: SELECT field1, field2, field3 FROM table1 JOIN table2 ON(table1.fieldx=table2.fieldy); SELECT e.last_name emp, m.last_name mgr FROM employees e JOIN employees m ON(e.manager_id = m.employee_id);  SELECT e.last_name as "EMP", w.last_name as "MGR“ FROM employees e JOIN employees w ON (e.manager_id = w.employee_id) WHERE e.last_name like 'H%'; Where clause can limit results.

Marge Hohly12 Example of JOIN ON  SELECT e.last_name, e.department_id, d.department_name FROM employees e JOIN departments d ON (e.department_id = d.department_id);

Marge Hohly13 Summary/Comparison

Marge Hohly14 Examples Join DJs on Demand d_play_list_items, d_track_listings, and d_cds tables with the JOIN USING syntax. Include the song ID, CD number, title, and comments in the output. 3. Display the city, department name, location ID, and department ID for departments 10, 20, and 30 for the city of Seattle. 6. Display job title, employee first name, last name, and for all employees that are stock clerks. 9. (Use Join On) Query and display manager ID, department ID, department name, first name, and last name for all employees in departments 80, 90, 110, and 190.

Marge Hohly15 Three-way Joins with the ON clause  A three-way join is the join of three tables  Syntax: SELECT employee_id, city, department_name FROM employees e JOIN departments d ON (d.department_id = e.department_id) JOIN locations l ON (d.location_id=l.location_id);

Marge Hohly16 Example  SELECT employee_id, city, department_name FROM employees e JOIN departments d ON (d.department_id = e.department_id) JOIN locations l ON (d.location_id=l.location_id);

Marge Hohly17 Revised example  SELECT e.employee_id, l.city, d.department_name FROM employees e, departments d, locations l WHERE e.department_id = d.department_id AND d.location_id = l.location_id

Marge Hohly18 INNER JOINS  An inner join returns only those rows that match the join condition SELECT e.last_name, e.department_id, d.department_name FROM employees e JOIN departments d ON (e.department_id = d.department_id);

Marge Hohly19 OUTER JOIN  Outer joins return those rows that match the join condition and those that do not  There are three ANSI/ISO SQL: 1999 outer joins: LEFT OUTER JOIN RIGHT OUTER JOIN FULL OUTER JOIN

Marge Hohly20 LEFT OUTER JOIN  SELECT e.last_name, e.department_id, d.department_name FROM employees e LEFT OUTER JOIN departments d ON(e.department_id=d.department_id);  SQL 99 equivalent: SELECT e.last_name, e.department_id, d.department_name FROM employees e, departments d WHERE e.department_id=d.department_id(+);  This statement will return those employees who do not have a department_id

Marge Hohly21 RIGHT OUTER JOIN  SELECT field1, field2.... FROM table1 a RIGHT OUTER JOIN table2 b ON (a.field=b.field); or USING(field name);  SELECT e.last_name, e.department_id, d.department_name FROM employees e RIGHT OUTER JOIN departments d ON(e.department_id=d.department_id);  This statement will return those departments who do not have any employees in them

Marge Hohly22 FULL OUTER JOIN  The FULL OUTER JOIN returns both matched and all unmatched rows  Syntax: SELECT field1, field2, field3 FROM table1 a FULL OUTER JOIN table2 b ON a.field=b.field; SELECT e.last_name, e.department_id, d.department_name FROM employees e FULL OUTER JOIN departments d ON(e.department_id=d.department_id);  There is no direct comparable Oracle specific join

Marge Hohly23 Example  Construct a join to display a list of Global Fast Foods customers whether or not they have placed an order as yet and all the customers who have placed orders.  SELECT c.first_name, c.last_name, o.order_number,o.order_date, o.order_total FROM f_customers c LEFT OUTER JOIN f_orders o ON (c.id = o.cust_id);

Marge Hohly24 Types of Joins  Oracle Proprietary Joins (8i and prior): Cartesian Product Equijoin Non-equijoin Outer join Self join  SQL: 1999 Compliant Joins: Cross joins Natural joins Using clause Full or two sided outer joins Arbitrary join conditions for outer joins