SQL Joins


 You will get vast amount of data/ information on this page regarding Oracle SQL JOINS   like:
    

SQL Inner Join

SQL Outer Join

SQL Self Join

SQL Cross Join

SQL Equijoin

 SQL Inner Join

Inner joins (the typical join operation, which uses some comparison operator like = or <>). These include Equi-joins and natural joins.Inner joins use a comparison operator to match rows from two tables based on the values in common columns from each table. For example, retrieving all rows where the student identification number is the same in both the students and courses tables.

SQL Inner Joins Example:

The "CONSUMERS" table:

P_Id
LastName
FirstName
Address
City
1
Kumar
Ram
Delhi
AAA
2
Singh
Laxman
Chandigarh
AAA
3
Sharma
Sameer
Ambala
BBB

The "ORDERS" table:

O_Id
OrderNo
P_Id
1
12355
3
2
12356
3
3
12357
1
4
24562
1
5
34764
15



Now we want to list all the Consumers with any orders.
We use the following SELECT statement:

SELECT Consumers.LastName, Consumers.FirstName, Orders.OrderNo
FROM Consumers
INNER JOIN Orders
ON Consumers.P_Id=Orders.P_Id
ORDER BY Consumers.LastName

The result-set will look like this:

Last Name
First Name
Order No


Kumar
Ram
12357


Kumar
Ram
24562


Sharma
Sameer
12355


Sharma
Sameer
12356



Atleast one match should be there in both tables involved in inner join in order for the query to return the rows.
When to use inner join:
 Use an inner join when you want to match values from both tables.
Why to use Inner Joins:
 Use inner joins to obtain information from two separate tables and combine that information in one result set.
When you use inner joins, consider the following facts and guidelines:
 1. Inner joins are the SQL Server default. You can abbreviate the INNER JOIN clause to  JOIN.
 2. Specify the columns that you want to display in your result set by including the qualified column names in the select list.
 3. Include a WHERE clause to restrict the rows that are returned in the result set.
 4. Do not use a null value as a join condition because null values do not evaluate equally with one another.
 5. SQL Server does not guarantee an order in the result set unless one is specified with an ORDER BY clause. 
  

SQL Outer Join

Outer joins can be a left, a right, or full outer join.
Outer joins are specified with one of the following sets of keywords when they are specified in the FROM clause:

LEFT JOIN or LEFT OUTER JOIN

The result set of a left outer join includes all the rows from the left table specified in the LEFT OUTER clause, not just the ones in which the joined columns match. When a row in the left table has no matching rows in the right table, the associated result set row contains null values for all select list columns coming from the right table.

SQL LEFT JOIN Example

The "CONSUMERS" table:

P_Id
LastName
FirstName
Address
City
1
Kumar
Ram
Delhi
AAA
2
Singh
Laxman
Chandigarh
AAA
3
Sharma
Sameer
Ambala
BBB

 The "ORDERS" table:

O_Id
Order No
P_Id
1
77895
3
2
44678
3
3
22456
1
4
24562
1
5
34764
15

 Now we want to list all the Consumers and their orders - if any, from the tables above.
We use the following SELECT statement:

SELECT Consumers.LastName, Consumers.FirstName, Orders.OrderNo
FROM Consumers
LEFT JOIN Orders
ON Consumers.P_Id=Orders.P_Id
ORDER BY Consumers.LastName

The result-set will look like this:

LastName
FirstName
Order No
Kumar
Ram
22456
Kumar
Ram
24562
Sharma
Sameer
77895
Sharma
Sameer
44678
Singh
Laxman



 The LEFT JOIN keyword returns all the rows from the left table (Consumers), even if there are no matches in the right table (Orders).

RIGHT JOIN or RIGHT OUTER JOIN
 A right outer join is the reverse of a left outer join. All rows from the right table are returned. Null values are returned for the left table any time a right table row has no matching row in the left table.

FULL JOIN or FULL OUTER JOIN

A full outer join returns all rows in both the left and right tables. Any time a row has no match in the other table, the select list columns from the other table contain null values. When there is a match between the tables, the entire result set row contains data values from the base tables.

Why to use outer joins
Use left or right outer joins when you require a complete list of data that is
stored in one of the joined tables in addition to the information that matches the join condition.

When to use outer joins
Use an outer join when you want to find unmatched rows. (because either you want to find missing values from one of the tables, like in this case, or you may want to include all values from your tables even if they are missing values in the other table).

SQL Self Join
Joining a table to itself is called self join.
Example of Self Join

Suppose you have a table that stores an employee identification number, the employee’s name, and the employee identification number of the employee’s manager. You might want to produce a list of all employees and their managers’ names. The problem is that the manager name does not exist as a category in the table:

SELECT * FROM EMP;

ID
NAME
MGR_ID
1
JOHN
0
2
MARY
1
3
STEVE
1
4
JACK
2
5
SUE
2


Why use self join

While self-joins rarely are used on a normalized database, you can use them to reduce the number of queries that you execute when you compare values of different columns of the same table. 
When you use self-joins, consider the following guidelines :
You must specify table aliases to reference two copies of the table. Remember that table aliases are different from column aliases. Table aliases are designated as the table name followed by the alias.
  
When you create self-joins, each row matches itself and pairs are repeated, resulting in duplicate rows.Use a WHERE clause to eliminate these duplicate rows 
In the following example, we have included the table EMP twice in the FROM clause of the query, giving the table two aliases for the purpose of the query. By providing two aliases, it is as if you are selecting from two distinct tables. All managers are also employees, so the JOIN condition between the two tables compares the value of the employee identification number from the first table with the manager identification number in the second table. The first table acts as a table that stores employee information, whereas the second table acts as a table that stores manager information :

SELECT E1.NAME, E2.NAME 
FROM EMP E1, EMP E2
WHERE E1.MGR_ID = E2.ID;                    
                                              
Name
Name
MARY
JOHN
STEVE
JOHN
JACK
MARY
SUE
MARY        

No comments:

Post a Comment