Queries - Joins

Download as pdf or txt
Download as pdf or txt
You are on page 1of 46

SQL 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).

SELECT prd.product_name , prd.price ,


sup.supplier_name
FROM products prd , suppliers sup
WHERE prd.supplier_id = sup.supplier_id
AND prd.supplier_id = 90
AND prd.price > 60
• You can change the sorting order, thereby
displaying the data items sorted by the product’s
price:
SELECT prd.product_name , prd.price ,
sup.supplier_name
FROM products prd JOIN suppliers sup
WHERE prd.supplier_id = sup.supplier_id
AND prd.supplier_id = 90
AND prd.price > 60
ORDER BY prd.price DESC
JOINING MORE THAN TWO TABLES
• Sometimes you may need to join more than two tables,
for example: displaying
– the product’s name from the Product table,
– the name of the supplier from Suppliers table,
– the name of the region where this supplier resides from
the Regions table.
• Joining an additional table requires us to:
• Specify the table name in the Oracle FROM clause (by
separating one table from another by comma).
• Determine the additional join condition, and specify
additional join condition in the Oracle WHERE clause.
SELECT prd.product_name , prd.price ,
sup.supplier_name, reg.region_name
FROM products prd , suppliers sup , regions reg
WHERE prd.supplier_id = sup.supplier_id
AND sup.region_id = reg.region_id

• The same concept applies to joining four


tables or more – adding the table name at the
Oracle FROM clause, and specifying additional
join condition at the Oracle WHERE clause.
OUTER JOIN
• An outer join is such a join which is similar to
the equi join, but Oracle will also return non
matched rows from the table.
• While the Oracle Inner JOIN statement allows
us to retrieve rows from both tables only if
they match the join condition, Oracle Outer
JOIN statement retrieves all rows from at least
one of the tables, regardless of whether there
is a match on the second table.
• Note that Tim does not appear, and neither does Asia
region. The reason for this is the way those tables were
compared. The comparison between these two tables
was performed by using the following condition:
WHERE sup.region_id = reg.region_id
• That means that as long as the values of the column
Region Number in the Suppliers table are equal to the
values of the column Region Number in the Regions
table, the row will appear in the query result. If a row
does not satisfy a join condition, the row will not
appear in the query result.
• Tim does not appear because his region’s number is
NULL, and NULL cannot be compared to any value.
• The Asia region does not appear because its number is
30, and this value cannot be compared to any value in
the column Region Number within the Suppliers table.
• To display all of the data items that are found on one
table (either left or right), including values that have no
comparable data on the second table, use either Oracle
Left Outer Join or Oracle Right Outer Join.
WHERE sup.supplier_id = reg.supplier_id
(left) (right)
• To display all of the Suppliers who exist on the
table, including those who do not belong to any
region, use the Oracle Outer Join operator. This
operator, a plus sign enclosed withing
parentheses (+), will be placed on the side of the
join condition that is deficient of information –
the right side.
SELECT sup.last_name , sup.salary ,
reg.region_name
FROM Suppliers sup, Regions reg
WHERE sup.region_id = reg.region_id (+)
LEFT OUTER JOIN
• This left outer join displays all matching
records of both table along with the records in
left hand side table of join clause which are
not in right hand side table of join clause.
RIGHT OUTER JOIN
• This right outer join displays all matching
records of both table along with the records in
left hand side table of join clause which are
not in right hand side table of join clause.
SELECT sup.last_name , sup.salary , reg.region_name
FROM Suppliers sup, Regions reg
WHERE sup.region_id (+) = reg.region_id
FULL OUTER JOIN
• A full outer join returns all rows from both the
tables left and right side of the join clause,
extended with nulls if they do not satisfy the
join condition.
NATURAL JOIN
• A natural join is such a join that compares the
common columns of both tables with each
other.
ANTI JOIN
• An antijoin between two tables returns rows
from the first table where no matches are
found in the second table. Anti-Joins are only
available when performing a NOT IN sub-
query
SEMI JOIN
• A semi-join is such a join where the EXISTS
clause is used with a subquery. It can be called
a semi-join because even if duplicate rows are
returned in the subquery, only one set of
matching values in the outer query is
returned.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy