Queries - Joins
Queries - Joins
Queries - Joins
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 equality and non-
equality joins
– View data that generally does not meet a join
condition by using outer joins
– Join a table to itself
Cartesian Product
• 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.
Generating a Cartesian Product
• Every possible combination of the elements of
two or more sets.
• For example, a Cartesian join of (A,B) and (1,2,3)
is (A,1) (A,2) (A,3) (B,1) (B,2) (B,3).
SELECT e.ename, d.dname
FROM emp e, dept d;
• Without a specification of how the join is to be
done, the DB will blithely return, for every row in
order, all of the rows in product. If you have 4,000
emp and 200 dept you will get back 800,000
rows.
• Also called as “cross join”
CUSTOMER TABLE
ORDERS TABLE
JOINS
• Use a join to query data from more than one
table.
• Write the join condition in the WHERE clause.
SELECT table1.column1, table2. column2
FROM table1, table2
WHERE table1. column1 = table2. column2;
• Prefix the column name with the table name
when the same column name appears in more
than one table.
TYPES OF JOINS
• Equijoins
• Non-Equi Joins
• Self Joins
• Cross Join / Cartesian Products
• Inner Joins
• Outer Joins
– Left Outer Join
– Right Outer Join
– Full Outer Join
• Natural Join
• Antijoins
• Semijoins
EQUI JOIN
• An equijoin is a join with a join condition
containing an equality operator. This is
represented by (=) sign. This join retrieves
information by using equality condition.
NON-EQUI JOIN
• An nonequi join is an inner join statement that
uses an unequal operation (i.e.: <>, >, <, !=,
BETWEEN, etc.) to match rows from different
tables.
SELF JOIN
• A self join is such a join in which a table is
joined with itself. For example, when you
require details about an employee and his
manager (also an employee).
INNER JOIN
• Sometimes you may need to use data from more than
one table. In this illustration, the result set displays
information from two separate tables: product name
from Products table, supplier name from Suppliers
table.
• An inner join is a join that returns rows of the tables
that satisfy the join condition.
• Multiple tables (two or more tables) can be linked only
if they have common values (in this case, supplier
number) or a logical connection of some kind.
• Relating between two tables requires you to determine
the join condition. In the example shown above, the
join condition was based on the equality operator (=).
HINTS
• In the Oracle SELECT clause, precede the column name with the
table name for clarity.
• When a column is common to both tables, it must be prefixed with
the table name.
• In the Oracle FROM clause, you need to specify the tables from
which you would like to retrieve the data. These tables are specified
with comma (,) between them.
• After the WHERE keyword, specify the join condition.
• To determine the relation between Products and Suppliers tables –
values in the supplier_id column on both tables must be equal. This
type of relation is referred as an Equi Join.
• Equi joins are also called Simple Joins or Inner Joins.
• Frequently, this relation involves primary key and foreign key
complements.
TABLE ALIAS NAME
• Qualifying column names with table names can be time
consuming, and may result in a very long, unreadable query.
In Oracle, Instead of writing a full table name after each
column, use Table Aliases. Just as Column Alias gives a column
another name, a table alias gives a table another name.
• Use the Oracle FROM clause to define the table aliases.
• Write an alias after each table name.
• You can assign any alias to a table (for example, you can assign
the letter A to the Products table); however, it is advisable to
assign meaningful aliases.
• After defining an alias to a table, that alias must be
substituted for the table name throughout the Oracle SELECT
statement. All explicit references to the table must use the
table alias, not the table name.
• The table alias is valid only for the current Oracle SELECT
statement.
ADDING ADDITIONAL CLAUSES
• you can carry on adding additional clauses to your
Oracle SELECT statement (restricting the rows
returned, adding aggregations, defining the sorting
order and so on).