SQL Joins

A JOIN clause is used to combine rows from two or more tables, based on a related column between them.

Different Types of SQL JOINs

Here are the different types of the JOINs in SQL:
  • (INNER) JOIN: Returns records that have matching values in both tables
  • LEFT (OUTER) JOIN: Return all records from the left table, and the matched records from the right table
  • RIGHT (OUTER) JOIN: Return all records from the right table, and the matched records from the left table
  • FULL (OUTER) JOIN: Return all records when there is a match in either left or right table











SQL INNER JOIN Keyword

The INNER JOIN keyword selects records that have matching values in both tables.

INNER JOIN Syntax

SELECT column_name(s)
FROM table1
INNER JOIN table2 ON table1.column_name = table2.column_name;
SQL INNER JOIN

Oracle INNER JOIN Example

Let's take an example to perform Inner Join on two tables "Suppliers" and "Order1".
Suppliers
Oracle Inner Join
Oracle Inner Join supplier
Order1
Oracle Inner Join
Oracle Inner Join order
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

SQL LEFT JOIN Keyword

The LEFT JOIN keyword returns all records from the left table (table1), and the matched records from the right table (table2). The result is NULL from the right side, if there is no match.
Syntax
  1. SELECT columns  
  2. FROM table1  
  3. LEFT [OUTERJOIN table2  
  4. ON table1.column = table2.column
Note: In some databases LEFT JOIN is called LEFT OUTER JOIN.
SQL LEFT JOIN

Execute this query
  1. SELECT suppliers.supplier_id, suppliers.supplier_name, order1.order_number  
  2. FROM suppliers  
  3. LEFT OUTER JOIN order1  
  4. ON suppliers.supplier_id = order1.supplier_id;  
Output
Oracle Left Outer Join 2

SQL RIGHT JOIN Keyword

The RIGHT JOIN keyword returns all records from the right table (table2), and the matched records from the left table (table1). The result is NULL from the left side, when there is no match.

RIGHT JOIN Syntax

SELECT column_name(s)
FROM table1
RIGHT JOIN table2 ON table1.column_name = table2.column_name;
Note: In some databases RIGHT JOIN is called RIGHT OUTER JOIN.
SQL RIGHT JOIN

Execute this query
  1. SELECT order1.order_number, order1.city, suppliers.supplier_name  
  2. FROM suppliers  
  3. RIGHT OUTER JOIN order1  
  4. ON suppliers.supplier_id = order1.supplier_id;  
Output
Oracle Right Outer Join 2

SQL FULL OUTER JOIN Keyword

The FULL OUTER JOIN keyword return all records when there is a match in either left (table1) or right (table2) table records. It places NULL where the join condition is not met.
Note: FULL OUTER JOIN can potentially return very large result-sets!

FULL OUTER JOIN Syntax

SELECT column_name(s)
FROM table1
FULL OUTER JOIN table2 ON table1.column_name = table2.column_name;
SQL FULL OUTER JOIN


Below is a selection from the "Customers" table:
CustomerIDCustomerNameContactNameAddressCityPostalCodeCountry
1
Alfreds FutterkisteMaria AndersObere Str. 57Berlin12209Germany
2Ana Trujillo Emparedados y heladosAna TrujilloAvda. de la Constitución 2222México D.F.05021Mexico
3Antonio Moreno TaqueríaAntonio MorenoMataderos 2312México D.F.05023Mexico
And a selection from the "Orders" table:
OrderIDCustomerIDEmployeeIDOrderDateShipperID
10308271996-09-183
103093731996-09-191
103107781996-09-202

SELECT Customers.CustomerName, Orders.OrderID
FROM Customers
FULL OUTER JOIN Orders ON Customers.CustomerID=Orders.CustomerID
ORDER BY Customers.CustomerName;
A selection from the result set may look like this:
CustomerNameOrderID
Alfreds Futterkiste 
Ana Trujillo Emparedados y helados10308
Antonio Moreno Taquería10365
 10382
 10351
Note: The FULL OUTER JOIN keyword returns all the rows from the left table (Customers), and all the rows from the right table (Orders). If there are rows in "Customers" that do not have matches in "Orders", or if there are rows in "Orders" that do not have matches in "Customers", those rows will be listed as well.

Comments

Popular posts from this blog

gsutil Vs Storage Transfer Service Vs Transfer Appliance

SQL basic interview question