0% found this document useful (0 votes)
44 views

Oracle Joins

Uploaded by

giriraj
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views

Oracle Joins

Uploaded by

giriraj
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 17

Oracle Joins

Join is a query that is used to combine rows from two or more tables, views, or materialized views.
It retrieves data from multiple tables and creates a new table.

Join Conditions
There may be at least one join condition either in the FROM clause or in the WHERE clause for
joining two tables. It compares two columns from different tables and combines pair of rows, each
containing one row from each table, for which join condition is true.

Types of Joins
o Inner Joins (Simple Join)
o Outer Joins
o Left Outer Join (Left Join)
o Right Outer Join (Right Join)
o Full Outer Join (Full Join)
o Equijoins
o Self Joins
o Cross Joins (Cartesian Products)
o Antijoins
o Semijoins

Oracle INNER JOIN


Inner Join is the simplest and most common type of join. It is also known as simple join. It returns
all rows from multiple tables where the join condition is met.

Syntax

1. SELECT columns
2. FROM table1
3. INNER JOIN table2
4. ON table1.column = table2.column;

Image representation of Inner Join


Oracle INNER JOIN Example
Let's take an example to perform Inner Join on two tables "Suppliers" and "Order1".

Suppliers

Order1

This example will return all rows from "suppliers" and "order1" table where there is a matching
supplier_id value in both the suppliers and order1 tables.

Execute the following query


1. SELECT suppliers.supplier_id, suppliers.supplier_name, order1.order_number
2. FROM suppliers
3. INNER JOIN order1
4. ON suppliers.supplier_id = order1.supplier_id;

Output

Oracle INNER JOIN


Inner Join is the simplest and most common type of join. It is also known as simple join. It returns
all rows from multiple tables where the join condition is met.

Syntax

1. SELECT columns
2. FROM table1
3. INNER JOIN table2
4. ON table1.column = table2.column;

Image representation of Inner Join

Oracle INNER JOIN Example


Let's take an example to perform Inner Join on two tables "Suppliers" and "Order1".

Suppliers
Order1

This example will return all rows from "suppliers" and "order1" table where there is a matching
supplier_id value in both the suppliers and order1 tables.

Execute the following query


1. SELECT suppliers.supplier_id, suppliers.supplier_name, order1.order_number
2. FROM suppliers
3. INNER JOIN order1
4. ON suppliers.supplier_id = order1.supplier_id;

Output
Oracle OUTER JOIN
An outer join is similar to equijoin but it gets also the non-matched rows from the table. It is categorized i
Join, Right Outer Join and Full Outer Join by Oracle 9i ANSI/ISO 1999 standard.

Left Outer Join


Left Outer Join returns all rows from the left (first) table specified in the ON condition and only those ro
right (second) table where the join condition is met.

Syntax

SELECT columns
FROM table1
LEFT [OUTER] JOIN table2
ON table1.column = table2.column;

Image representation of left outer join

Keep Watching

Example

In this example, we are performing left outer join on the already created tables ?suppliers? and ?order1?.

The following example would return all records from table ?suppliers? and only those records from tab
where the join fields are equal.

Execute this query

SELECT suppliers.supplier_id, suppliers.supplier_name, order1.order_number


FROM suppliers
LEFT OUTER JOIN order1
ON suppliers.supplier_id = order1.supplier_id;

Output
Right Outer Join
The Right Outer Join returns all rows from the right-hand table specified in the ON condition and only
from the other table where the join condition is met.

Syntax

SELECT columns
FROM table1
RIGHT [OUTER] JOIN table2
ON table1.column = table2.column;

Image representation of Right Outer Join

Example

In this example, we are performing right outer join on the already created tables ?suppliers? and ?order1?.

The following example would return all rows from the order1 table and only those rows from the sup
where the join condition is met.

Execute this query

SELECT order1.order_number, order1.city, suppliers.supplier_name


FROM suppliers
RIGHT OUTER JOIN order1
ON suppliers.supplier_id = order1.supplier_id;

Output

Full Outer Join


The Full Outer Join returns all rows from the left hand table and right hand table. It places NULL wh
condition is not met.

Syntax

SELECT columns
FROM table1
FULL [OUTER] JOIN table2
ON table1.column = table2.column;

Image representation of Full Outer Join

Learn more
volume is gedempt

Example

In this example, we are performing full outer join on the already created tables ?suppliers? and ?order1?.

The following example will return all rows from the ?suppliers? table and all rows from the ?order1
whenever the join condition is not met, it places the NULL value.

Execute this query

SELECT suppliers.supplier_id, suppliers.supplier_name, order1.order_number


FROM suppliers
FULL OUTER JOIN order1
ON suppliers.supplier_id = order1.supplier_id;

Output

Oracle EQUI JOIN


Oracle Equi join returns the matching column values of the associated tables. It uses a comparison ope
WHERE clause to refer equality.

Syntax

SELECT column_list
FROM table1, table2....
WHERE table1.column_name =
table2.column_name;

Equijoin also can be performed by using JOIN keyword followed by ON keyword and then specifying n
columns along with their associated tables to check equality.

Syntax

42.2M
787
HTML Tutorial
Next
Stay
SELECT *
FROM table1
JOIN table2
[ON (join_condition)]

Oracle EQUI JOIN Example


Let' take two tables "agents" and "customer".

Agents table

Agent data

Customer table

Customer data
Execute this query

SELECT agents.agent_city,customer.last_name,
customer.first_name
FROM agents,customer
WHERE agents.agent_id=customer.customer_id;

Output

Oracle SELF JOIN


Self Join is a specific type of Join. In Self Join, a table is joined with itself (Unary relationship). A self
join simply specifies that each rows of a table is combined with itself and every other row of the
table.

Syntax

1. SELECT a.column_name, b.column_name...


2. FROM table1 a, table1 b
3. WHERE a.common_filed = b.common_field;

Oracle SELF JOIN Example


Let's take a table "customers".

Join this table using SELF JOIN as follows:

1. SELECT a.name, b.age, a.SALARY


2. FROM CUSTOMERS a, CUSTOMERS b
3. WHERE a.SALARY < b.SALARY;

Output

Oracle Cross Join (Cartesian Products)


The CROSS JOIN specifies that all rows from first table join with all of the rows of second table. If
there are "x" rows in table1 and "y" rows in table2 then the cross join result set have x*y rows. It
normally happens when no matching join columns are specified.

In simple words you can say that if two tables in a join query have no join condition, then the
Oracle returns their Cartesian product.

Syntax
1. SELECT *
2. FROM table1
3. CROSS JOIN table2;

Or

1. SELECT * FROM table1, table2

Both the above syntax are same and used for Cartesian product. They provide similar result after
execution.

Image representation of cross join

Oracle Cross Join Example


Let's take two tables "customer" and "supplier".

Customer table detail

1. CREATE TABLE "CUSTOMER"


2. ( "CUSTOMER_ID" NUMBER,
3. "FIRST_NAME" VARCHAR2(4000),
4. "LAST_NAME" VARCHAR2(4000)
5. )
6. /
Supplier table detail

1. CREATE TABLE "SUPPLIER"


2. ( "SUPPLIER_ID" NUMBER,
3. "FIRST_NAME" VARCHAR2(4000),
4. "LAST_NAME" VARCHAR2(4000)
5. )
6. /

Execute this query

1. SELECT * FROM customer,supplier

Output
Oracle Anti Join
Anti-join is used to make the queries run faster. It is a very powerful SQL construct Oracle offers for
faster queries.

Anti-join between two tables returns rows from the first table where no matches are found in the
second table. It is opposite of a semi-join. An anti-join returns one copy of each row in the first
table for which no match is found.

Anti-joins are written using the NOT EXISTS or NOT IN constructs.

Example

Let's take two tables "departments" and "customer"

Departments table

1. CREATE TABLE "DEPARTMENTS"


2. ( "DEPARTMENT_ID" NUMBER(10,0) NOT NULL ENABLE,
3. "DEPARTMENT_NAME" VARCHAR2(50) NOT NULL ENABLE,
4. CONSTRAINT "DEPARTMENTS_PK" PRIMARY KEY ("DEPARTMENT_ID") ENABLE
5. )
6. /

Customer table

1. CREATE TABLE "CUSTOMER"


2. ( "CUSTOMER_ID" NUMBER,
3. "FIRST_NAME" VARCHAR2(4000),
4. "LAST_NAME" VARCHAR2(4000),
5. "DEPARTMENT_ID" NUMBER
6. )
7. /
Execute this query

1. SELECT departments.department_id, departments.department_name


2. FROM departments
3. WHERE NOT EXISTS
4. (
5. SELECT 1
6. FROM customer
7. WHERE customer.department_id = departments.department_id
8. )
9. ORDER BY departments.department_id;

Output

Oracle Semi Join


Semi-join is introduced in Oracle 8.0. It provides an efficient method of performing a WHERE
EXISTS sub-query.

A semi-join returns one copy of each row in first table for which at least one match is found.

Semi-joins are written using the EXISTS construct.

Oracle Semi Join Example


Let's take two tables "departments" and "customer"

Departments table

1. CREATE TABLE "DEPARTMENTS"


2. ( "DEPARTMENT_ID" NUMBER(10,0) NOT NULL ENABLE,
3. "DEPARTMENT_NAME" VARCHAR2(50) NOT NULL ENABLE,
4. CONSTRAINT "DEPARTMENTS_PK" PRIMARY KEY ("DEPARTMENT_ID") ENABLE
5. )
6. /
7.

Customer table

1. CREATE TABLE "CUSTOMER"


2. ( "CUSTOMER_ID" NUMBER,
3. "FIRST_NAME" VARCHAR2(4000),
4. "LAST_NAME" VARCHAR2(4000),
5. "DEPARTMENT_ID" NUMBER
6. )
7. /

Execute this query

1. SELECT departments.department_id, departments.department_name


2. FROM departments
3. WHERE EXISTS
4. (
5. SELECT 1
6. FROM customer
7. WHERE customer.department_id = departments.department_id
8. )
9. ORDER BY departments.department_id;

Output

Difference between anti-join and semi-join


While a semi-join returns one copy of each row in the first table for which at least one match is
found, an anti-join returns one copy of each row in the first table for which no match is found.

You might also like