JOINS

Inner Join 

The INNER JOIN selects all rows from both participating tables as long as there is a match between the columns. An SQL INNER JOIN is same as JOIN clause, combining rows from two or more tables.

Pictorial presentation of SQL Inner Join:

SQL inner join

Syntax:

SELECT * 
FROM table1 INNER JOIN table2 
ON table1.column_name = table2.column_name; 

OR

SELECT * 
FROM table1
JOIN table2 
ON table1.column_name = table2.column_name; 

The INNER JOIN in SQL joins two tables according to the matching of a certain criteria using a comparison operator.

Syntax diagram - INNER JOIN

SQL INNER JOIN syntax

Example: SQL INNER JOIN between two tables

Here is an example of inner join in SQL between two tables.

Sample table: foods


Sample table: company


To join item name, item unit columns from foods table and company name, company city columns from company table, with the following condition -

1. company_id of foods and company table must be same,

the following SQL statement can be used :

SQL Code:

SELECT foods.item_name,foods.item_unit,
company.company_name,company.company_city 
FROM foods 
INNER JOIN company 
ON foods.company_id =company.company_id;

Output:

ITEM_NAME                 ITEM_ COMPANY_NAME              COMPANY_CITY
------------------------- ----- ------------------------- --------------
Chex Mix                  Pcs   Akas Foods                Delhi
Cheez-It                  Pcs   Jack Hill Ltd             London
BN Biscuit                Pcs   Jack Hill Ltd             London
Mighty Munch              Pcs   Foodies.                  London
Pot Rice                  Pcs   Jack Hill Ltd             London
Jaffa Cakes               Pcs   Order All                 Boston




LEFT JOIN

The SQL LEFT JOIN (specified with the keywords LEFT JOIN and ON) joins two tables and fetches all matching rows of two tables for which the SQL-expression is true, plus rows from the frist table that do not match any row in the second table.

Pictorial presentation of SQL Left Join:

Sql left join image

Left Join: Syntax

SELECT *
FROM table1
LEFT [ OUTER ] JOIN table2
ON table1.column_name=table2.column_name;

SQL LEFT join fetches a complete set of records from table1, with the matching records (depending on the availability) in table2. The result is NULL in the right side when no matching will take place.

Syntax diagram - LEFT JOIN

Syntax diagram - SQL LEFT JOIN

Example of SQL Left Join

To get company name and company id columns from company table and company id, item name, item unit columns from foods table, after an OUTER JOINING with these mentioned tables, the following SQL statement can be used :

Sample table: foods


Sample table: company


SQL Code:

SELECT company.company_id,company.company_name,
company.company_city,foods.company_id,foods.item_name
FROM   company
LEFT JOIN foods
ON company.company_id = foods.company_id;

Explanation:

This SQL statement would return all rows from the company table and only those rows from the foods table where the joined fields are equal and if the ON clause matches no records in the 'foods' table, the join will still return rows, but the NULL in each column of the right table.

Output:

COMPANY_ID COMPANY_NAME              COMPANY_CITY              COMPANY_ID ITEM_NAME
---------- ------------------------- ------------------------- ---------- --------------
16         Akas Foods                Delhi                     16         Chex Mix
15         Jack Hill Ltd             London                    15         Cheez-It
15         Jack Hill Ltd             London                    15         BN Biscuit
17         Foodies.                  London                    17         Mighty Munch
15         Jack Hill Ltd             London                    15         Pot Rice
18         Order All                 Boston                    18         Jaffa Cakes
19         sip-n-Bite.               New York




RIGHT JOIN

The SQL RIGHT JOIN, joins two tables and fetches rows based on a condition, which is matching in both the tables ( before and after the JOIN clause mentioned in the syntax below) , and the unmatched rows will also be available from the table written after the JOIN clause ( mentioned in the syntax below ).

Pictorial presentation of SQL Right Join:

Sql right join image

Syntax:

SELECT *
FROM table1
RIGHT [ OUTER ] JOIN table2
ON table1.column_name=table2.column_name;

SQL RIGHT join fetches a complete set of records from table2, i.e. the rightmost table after JOIN clause, with the matching records (depending on the availability) in table1. The result is NULL in the left side when no matching will take place.

Syntax diagram - SQL Right Join

Syntax diagram - SQL RIGHT JOIN

Example of SQL Right Join or right outer join

Sample table: foods


Sample table: company


To get company ID, company name and company city columns from company table and company ID, item name columns from foods table, after an OUTER JOINING with these mentioned tables, the following SQL statement can be used:

SQL Code:

SELECT company.company_id,company.company_name,
company.company_city,foods.company_id,foods.item_name
FROM   company
RIGHT JOIN foods
ON company.company_id = foods.company_id;

Explanation:

This SQL statement would return all rows from the foods table and only those rows from the company table where the joined fields are equal and if the ON clause matches no records in the company table, the join will still return rows, but the NULL in each column of company table.

Output:

COMPANY_ID COMPANY_NAME              COMPANY_CITY              COMPANY_ID ITEM_NAME
---------- ------------------------- ------------------------- ---------- --------------
18         Order All                 Boston                    18         Jaffa Cakes
15         Jack Hill Ltd             London                    15         Pot Rice
15         Jack Hill Ltd             London                    15         BN Biscuit
15         Jack Hill Ltd             London                    15         Cheez-It
16         Akas Foods                Delhi                     16         Chex Mix
17         Foodies.                  London                    17         Mighty Munch
                                                                          Salt n Shake


Post a Comment

Previous Post Next Post