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.

Slides:



Advertisements
Similar presentations
WHERE Clause Chapter 2. Objectives Limit rows by using a WHERE clause Use the LIKE operator Effect of NULL values Use compound conditions Use the BETWEEN.
Advertisements

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.
© Abdou Illia MIS Spring 2014
Join Queries CS 146. Introduction: Join Queries So far, our SELECT queries have retrieved data from a single table Usually queries combine data from multiple.
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. Objectives After completing this lesson, you should Be able to do the following: Write SELECT statements to accessWrite.
Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall 1 Modern Database Management 11 th Edition Jeffrey A. Hoffer, V. Ramesh, Heikki Topi.
Displaying Data from Multiple Tables
Copyright  Oracle Corporation, All rights reserved. 3 Displaying Data from Multiple Tables.
Displaying Data from Multiple Tables. Objectives After completing this lesson, you should be able to do the following:  Write SELECT statements to access.
Displaying Data from Multiple Tables. Objectives After completing this lesson, you should be able to do the following: Write SELECT statements to access.
After completing this lesson, you should be able to do the following: Write SELECT statements to access data from more than one table using equality and.
Chapter 4 JOINING TABLES & FUNCTION Lecture by Ty Rasmey
Writing Basic SQL SELECT Statements. Capabilities of SQL SELECT Statements A SELECT statement retrieves information from the database. Using a SELECT.
5 Copyright © 2004, Oracle. All rights reserved. Displaying Data from Multiple Tables.
Introduction to Oracle9i: SQL1 Basic SQL SELECT Statements.
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.
WRITING BASIC SQL SELECT STATEMENTS Lecture 7 1. Outlines  SQL SELECT statement  Capabilities of SELECT statements  Basic SELECT statement  Selecting.
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.
4 Copyright © Oracle Corporation, All rights reserved. Displaying Data from Multiple Tables.
4 Copyright © Oracle Corporation, All rights reserved. Displaying Data from Multiple Tables.
Lecture 2 of Advanced Databases Advanced SQL Instructor: Mr.Ahmed Al Astal.
4 Copyright © Oracle Corporation, All rights reserved. Displaying Data from Multiple Tables.
Chapter 9 Joining Data from Multiple Tables
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.
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 TRANSACTION. Transaction It is a logical unit of work that must succeed or fail in its entirety. A transaction is an atomic operation which may.
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.
Copyright  Oracle Corporation, All rights reserved. 2 Restricting and Sorting Data.
SQL- DQL (Oracle Version). 2 SELECT Statement Syntax SELECT [DISTINCT] column_list FROM table_list [WHERE conditional expression] [GROUP BY column_list]
Chapter 4Introduction to Oracle9i: SQL1 Chapter 4 Joining Multiple Tables.
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.
Copyright س Oracle Corporation, All rights reserved. 4 Displaying Data from Multiple Tables.
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.
Multiple Table Queries (Inner Joins, Equijoins) Week 3.
Database Programming Section 15 – Oracle Proprietary Join Syntax and Review 1.
4 Copyright © Oracle Corporation, All rights reserved. Displaying Data from Multiple Tables.
Working with Columns, Characters, and Rows. 2 home back first prev next last What Will I Learn? In this lesson, you will learn to: –Apply the concatenation.
A Guide to SQL, Eighth Edition Chapter Five Multiple-Table Queries.
Database Programming Sections 3 – Oracle Joins. Marge Hohly2 Obtaining Data from Multiple Tables: Using Joins.
CHAPTER 2 : RELATIONAL DATA MODEL Prepared by : nbs.
4 Copyright © 2004, Oracle. All rights reserved. Displaying Data from Multiple Tables.
Copyright  Oracle Corporation, All rights reserved. 4 Displaying Data from Multiple Tables.
Displaying Data from Multiple Tables. Objectives After completing this lesson, you should be able to do the following: –Write SELECT statements to access.
4 Displaying Data from Multiple Tables. 4-2 Objectives At the end of this lesson, you should be able to: Write SELECT statements to access data from more.
4 Copyright © Oracle Corporation, All rights reserved. Displaying Data from Multiple Tables.
Displaying Data from Multiple Tables
Displaying Data from Multiple Tables
Displaying Data from Multiple Tables
Oracle Join Syntax.
Displaying Data from Multiple Tables
Displaying Data from Multiple Tables
Displaying Data from Multiple Tables
Writing Basic SQL SELECT Statements
Displaying Data from Multiple Tables Using Joins
Oracle Join Syntax.
(SQL) 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
Presentation transcript:

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 table. Department IDs exist in both the EMPLOYEES and DEPARTMENTS tables. Location IDs exist in the DEPARTMENTS table. To produce the report, you need to link the EMPLOYEES and DEPARTMENTS tables and access data from both of them.

A Cartesian product is formed when: A join condition is omitted A join condition is invalid All rows in the first table are joined to all rows in the second table To avoid a Cartesian product, always include a valid join condition in a WHERE clause.

All rows in the first table are JOINED to All rows in the second table

Use a join to query data from more than one table. SELECT table1.column, table2.column FROM table1, table2 WHERE table1.column1 = table2.column2; Write the join condition in the WHERE clause. Prefix the column name with the table namewhen the same column name appears in more than one table.

When writing a SELECT statement that joins tables, precede the column name with the table name for clarity and to enhance database access. (optional) If the same column name appears in more than one table, the column name must be prefixed with the table name.(mandatory) To join n tables together, you need a minimum of n-1 join conditions. For example, to join four tables, a minimum of three joins is required. This rule may not apply if your table has a concatenated primary key, in which case more than one column is required to uniquely identify each row. [Bonus] Q:How to define a table with a composite primary key?

To determine an employees department name you compare the : value in the DEPARTMENT_ID column in the EMPLOYEES table, with the DEPARTMENT_ID values in the DEPARTMENTS table. The relationship between the EMPLOYEES and DEPARTMENTS tables is an equijoin, that is, values in the DEPARTMENT_ID column on both tables must be equal. Frequently, this type of join involves primary and foreign key complements. Note: Equijoins are also called simple joins or inner joins

Equijoin condition: Specifies how tables to be joined

Note: In the previews example, the DEPARTMENT_ID column is common to both tables, it must be prefixed by the table name to avoid ambiguity.

Use table prefixes to qualify column names that are in multiple tables. Improve performance by using table prefixes. For simplicity: We can distinguish columns that have identical names but reside in different tables by using column aliases.

Sometimes you may need to join more than two tables. For example, to display the last name, the department name, and the city for each employee, you have to join the EMPLOYEES, DEPARTMENTS, and LOCATIONS tables. (see Example 2)

A nonequijoin is a join condition containing something other than an equality operator. The relationship between the EMPLOYEES table and the JOB_GRADES table has an example of a nonequijoin. A relationship between the two tables is that the SALARY column in the EMPLOYEES table must be between the values in the LOWEST_SALARY and HIGHEST_SALARY columns of the JOB_GRADES table. The relationship is obtained using an operator other than equals (=).

Nonequijoin condition: Specifies that salary must be between any Pair of low and high sal

Note that : other conditions such as = can be used also. But BETWEEN condition is the simplest.

If a row does not satisfy a join condition, the row will not appear in the query result. For example, in the equijoin condition of EMPLOYEES and DEPARTMENTS tables, employee Grant does not appear because there is no department ID recorded for her in the EMPLOYEES table Instead of seeing 20 employees in the result set, you see 19 records. (see Example 3)

To see rows that do not meet the join condition we use the outer join The outer join operator is the plus sign (+). SELECT table1.column, table2.column FROM table1, table2 WHERE table1.column(+) = table2.column; SELECT table1.column, table2.column FROM table1, table2 WHERE table1.column = table2.column(+);

table1.column = is the condition that joins (or relates) the tables together. table2.column (+) is the outer join symbol, which can be placed on either side of the WHERE clause condition, but not on both sides. (Place the outer join symbol following the name of the column in the table without the matching rows.)

The slide example displays employee last names, department IDs and department names. The Contracting department does not have any employees. The empty value is shown in the output shown. Outer Join Restrictions The outer join operator can appear on only one side of the expression: the side that has information missing. It returns those rows from one table that have no direct match in the other table. A condition involving an outer join cannot use the IN operator or be linked to another condition by the OR operator.

Write a query to display the last name, department number, and department name for all employees. Write a query to display the employee last name, department name, location ID, and city of all employees who earn a commission