DEV Community

Doge Algo Engineering
Doge Algo Engineering

Posted on • Updated on

SQL Joins

Inner Join

image

SQL Query :

SELECT * FROM A INNER JOIN B ON A.KEY = B.KEY
Enter fullscreen mode Exit fullscreen mode

Definition :

Select tables A & B and compare key values of table A and table B and return records which only have the identical key values in both tables of A & B. This is similar to the Set theory operation Intersection which is denoted by A ∩ B, the set containing all elements of A that also belong to B.

Left Join

image

SQL Query :

SELECT * FROM A LEFT JOIN B ON A.KEY = B.KEY
Enter fullscreen mode Exit fullscreen mode

Definition :

Select tables A & B and compare key values of table A and table B and return all records of A which is in the left side of the sql query but in case of B which is in right side of sql query, only returns records that have the same key values of table A.

Right Join

image

SQL Query :

SELECT * FROM A RIGHT JOIN B ON A.KEY = B.KEY
Enter fullscreen mode Exit fullscreen mode

Definition :

Select tables A & B and compare key values of table A and table B and return all records of B which is in the right side of the sql query but in case of A which is in left side of sql query, only returns records that have the same key values of table B.

Note :
1. ON clause is the more general than USING clause. One can join tables on a column, a set of columns and even a condition. For example:

SELECT * FROM city JOIN country ON city.country_code = country.country_code
Enter fullscreen mode Exit fullscreen mode

2. USING is useful when both tables share a column of the exact same name on which they join. For example:

SELECT city.name, city_id FROM city JOIN country USING city_id
Enter fullscreen mode Exit fullscreen mode

Full Outer Join

image

SQL Query :

SELECT * FROM A FULL OUTER JOIN B ON A.KEY = B.KEY
Enter fullscreen mode Exit fullscreen mode

Definition :

Select tables A & B and compare key values of table A and table B and return all records of both A & B and fill in NULL for missing matches on either side.

Left Outer Join

image
SQL Query :

SELECT * FROM A LEFT OUTER JOIN B ON A.KEY = B.KEY WHERE B.KEY IS NULL
Enter fullscreen mode Exit fullscreen mode

Definition :

Left outer join returns set of records from A, with the matching records in B (which are available). If there is no match, the right side will have null.

Right Outer Join

image
SQL Query :

SELECT * FROM A RIGHT OUTER JOIN B ON A.KEY = B.KEY WHERE A.KEY IS NULL
Enter fullscreen mode Exit fullscreen mode

Definition :

Right outer join returns set of records from B, with the matching records in A (which are available). If there is no match, the left side will have null.

Full Outer Join without Intersection

image

SELECT * FROM A FULL OUTER JOIN B ON A.KEY = B.KEY WHERE A.KEY IS NULL OR B.KEY IS NULL
Enter fullscreen mode Exit fullscreen mode

Definition :

Full outer join without intersection returns the set of records only in table A, but not in table B or the set of records only in table B, but not in table A. This clause returns all records that match the ON condition, excluding those are in common between two tables, or those records exist in both tables.

My Personal Blog : https://danyson.github.io/

Discussion (0)