Displaying Data from Multiple Tables (SQL99 Syntax with examples)

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

Sometimes you need to use data from more than one table. In example1, the report displays data from two separate tables. Employee IDs exist in the EMPLOYEES.
Chapter 4 Joining Multiple Tables
A Guide to SQL, Seventh Edition. Objectives Use joins to retrieve data from more than one table Use the IN and EXISTS operators to query multiple tables.
Displaying Data from Multiple Tables
4-1 Copyright  Oracle Corporation, All rights reserved. Types of Joins Equijoin Non-equijoin Outer join Self join.
Chapter 4 JOINING TABLES & FUNCTION Lecture by Ty Rasmey
Introduction To SQL Lynnwood Brown President System Managers LLC Copyright System Managers LLC 2003 all rights reserved.
Multiple-Column Subqueries 12. Objectives After completing this lesson, you should be able to do the following: Write a multiple-column subquery Describe.
6-1 Copyright  Oracle Corporation, All rights reserved. Types of Subqueries Single-row subquery Main query Subquery returns CLERK Multiple-row subquery.
Multiple-Column Subqueries. Objectives After completing this lesson, you should be able to do the following: Write a Multiple-column subquery Describe.
Copyright س Oracle Corporation, All rights reserved. 7 Multiple-Column Subqueries.
Copyright  Oracle Corporation, All rights reserved. 7 Multiple-Column Subqueries.
Copyright  Oracle Corporation, All rights reserved. 6 Writing Correlated Subqueries.
5 Copyright © 2004, Oracle. 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.
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.
4 Copyright © Oracle Corporation, All rights reserved. Displaying Data from Multiple Tables.
Copyright  Oracle Corporation, All rights reserved. 1 Writing Basic SQL Statements.
Lecture 2 of Advanced Databases Advanced SQL Instructor: Mr.Ahmed Al Astal.
Chapter 9 Joining Data from Multiple Tables
Oracle Database Administration Lecture 2 SQL language.
A Guide to MySQL 5. 2 Objectives Use joins to retrieve data from more than one table Use the IN and EXISTS operators to query multiple tables Use a subquery.
Subqueries.
2 Writing Basic SELECT Statements. 1-2 Copyright  Oracle Corporation, All rights reserved. Capabilities of SQL SELECT Statements Selection Projection.
1 Information Retrieval and Use (IRU) CE An Introduction To SQL Part 1.
Joins & Sub-queries. Oracle recognizes that you may want data that resides in multiple tables drawn together in some meaningful way. One of the most important.
SELECT Statements Lecture Notes Sree Nilakanta Fall 2010 (rev)
7 Multiple-Column Subqueries. 7-2 Objectives At the end of this lesson, you should be able to: Write a multiple-column subquery Describe and explain the.
SQL- DQL (Oracle Version). 2 SELECT Statement Syntax SELECT [DISTINCT] column_list FROM table_list [WHERE conditional expression] [GROUP BY column_list]
Chapter 4 Multiple-Table Queries
Chapter 4Introduction to Oracle9i: SQL1 Chapter 4 Joining Multiple Tables.
Unit 4 Queries and Joins. Key Concepts Using the SELECT statement Statement clauses Subqueries Multiple table statements Using table pseudonyms Inner.
4 Copyright © Oracle Corporation, All rights reserved. Displaying Data from Multiple Tables.
1 Multiple Table Queries. 2 Objectives  Retrieve data from more than one table by joining tables  Using IN and EXISTS to query multiple tables  Nested.
SQL SeQueL -Structured Query Language SQL SQL better support for Algebraic operations SQL Post-Relational row and column types,
5 Copyright © 2004, Oracle. All rights reserved. Displaying Data from Multiple Tables.
5 Copyright © 2004, Oracle. All rights reserved. Displaying Data from Multiple Tables.
Database Programming Sections 4 – Joins. Marge Hohly2 Overview  Oracle Proprietary Joins (8i and prior): Cartesian Product Equijoin Non-equijoin Outer.
Database Programming Section 15 – Oracle Proprietary Join Syntax and Review 1.
CSCI N311: Oracle Database Programming 4-1 Chapter 12: When One Query Depends Upon Another Correlated Subqueries. Subqueries in the where clause. Anti-Joins.
A Guide to SQL, Eighth Edition Chapter Five Multiple-Table Queries.
Copyright س Oracle Corporation, All rights reserved. I Introduction.
In this session, you will learn to: Query data by using joins Query data by using subqueries Objectives.
1-1 Copyright  Oracle Corporation, All rights reserved. Logging In to SQL*Plus From Windows environment:From Windows environment: From command line:From.
Writing Basic SQL Statements. Objectives After completing this lesson, you should be able to do the following: –List the capabilities of SQL SELECT statements.
1 ORACLE I 3 – SQL 1 Salim Phone: YM: talim_bansal.
Copyright  Oracle Corporation, All rights reserved. 4 Displaying Data from Multiple Tables.
4 Copyright © Oracle Corporation, All rights reserved. Displaying Data from Multiple Tables.
Multiple-Column Subqueries
Displaying Data from Multiple Tables
Displaying Data from Multiple Tables
Multiple Table Queries
Subqueries.
Subqueries Schedule: Timing Topic 25 minutes Lecture
Displaying Data from Multiple Tables
Displaying Data from Multiple Tables
Writing Correlated Subqueries
جملة الاستعلام الأساسية
JOINS.
Multi-table queries Subqueries
Displaying Data from Multiple Tables Using Joins
(SQL) Displaying Data from Multiple Tables
Subqueries Schedule: Timing Topic 25 minutes Lecture
Displaying Data from Multiple Tables
Displaying Data from Multiple Tables
Displaying Data from Multiple Tables
Displaying Data from Multiple Tables
Subqueries Schedule: Timing Topic 25 minutes Lecture
Database Programming Using Oracle 11g
Presentation transcript:

Displaying Data from Multiple Tables (SQL99 Syntax with examples)

SQL99 Syntax

Cross Join The CROSS keyword indicates that a cross join is being performed. A cross join produces the cross-product of two relations and is essentially the same as the comma-delimited Oracle notation.

Cross Join Example Select all employees and departments SQL99 SELECT ename, dname FROM emp CROSS JOIN dept; Oracle SELECT ename, dname FROM emp, dept;

Natural Join The NATURAL keyword indicates that a natural join is being performed. A natural join 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 the relevant columns. When specifying columns that are involved in the natural join, do not qualify the column name with a table name or table alias. Restriction: You cannot specify a LOB column or a collection column as part of a natural join.

Natural Join Example (Equijoin) Select employees, their job and their department, and departments location SQL99 SELECT empno, ename, job, dname, loc FROM emp NATURAL JOIN dept; Oracle SELECT a.empno, a.ename, a.job, b.dname, b.loc FROM emp a, dept b WHERE a.deptno = b.deptno;

USING column When you are specifying an equijoin of columns that have the same name in both tables, the USING column clause indicates the columns to be used. You can use this clause only if the join columns in both tables have the same name. Do not qualify the column name with a table name or table alias. Restriction: You cannot specify a LOB column or a collection column in the USING column clause.

USING column Example Select employees and their department, and departments location SQL99 SELECT a.empno, a.ename, b.dname, b.loc FROM emp a JOIN dept b USING (deptno);

ON condition Use the ON clause to specify a join condition. Doing so lets you specify join conditions separate from any search or filter conditions in the WHERE clause.

ON condition Example Select employees and their department, and departments location SQL99 SELECT a.empno, a.ename, b.dname, b.loc FROM emp a JOIN dept b ON (a.deptno = b.deptno);

Self Join A self join is a join of a table to itself. This table appears twice in the FROM clause and is followed by table aliases that qualify column names in the join condition. To perform a self join, Oracle combines and returns rows of the table that satisfy the join condition.

Self Join Example Select employees and their managers SQL99 SELECT a.ename employee, b.ename manager FROM emp a JOIN emp b ON (a.empno = b.mgr);

Three-Way Join Example (1) Select employees and their departments and customers that they work with SQL99 SELECT a.ename employee, b.dname departmant, c.name customer FROM emp a JOIN dept b ON (a.deptno = b.deptno) JOIN customer c ON (a.empno = c.repid);

Three-Way Join Example (2) Select employees and their departments and customers that they work with Oracle SELECT a.ename employee, b.dname departmant, c.name customer FROM emp a, dept b, customer c WHERE a.deptno = b.deptno AND a.empno = c.repid;

Outer Join (1) An outer join extends the result of a simple join. An outer join returns all rows that satisfy the join condition and also returns some or all of those rows from one table for which no rows from the other satisfy the join condition. –To write a query that performs an outer join of tables A and B and returns all rows from A (a left outer join), use the ANSI LEFT [OUTER] JOIN syntax, or apply the outer join operator (+) to all columns of B in the join condition. For all rows in A that have no matching rows in B, Oracle returns null for any select list expressions containing columns of B.

Outer Join (2) –To write a query that performs an outer join of tables A and B and returns all rows from B (a right outer join), use the ANSI RIGHT [OUTER] syntax, or apply the outer join operator (+) to all columns of A in the join condition. For all rows in B that have no matching rows in A, Oracle returns null for any select list expressions containing columns of A. –To write a query that performs an outer join and returns all rows from A and B, extended with nulls if they do not satisfy the join condition (a full outer join), use the ANSI FULL [OUTER] JOIN syntax.

Left Outer Join Example Select all employees, their job and customers that they work with and customers addresses SQL99 SELECT a.ename, a.job, b.name, b.address FROM emp a LEFT OUTER JOIN customer b ON (a.empno = b.repid);

Right Outer Join Example Select all departments, their location and employees that work in them SQL99 SELECT a.ename, b.dname, b.loc FROM emp a RIGHT OUTER JOIN dept b ON (a.deptno = b.deptno);