SQL Joins

Inner Join

SQL Query :

SELECT * FROM A INNER JOIN B ON A.KEY = B.KEY

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 …


This content originally appeared on DEV Community and was authored by Danyson

Inner Join

image

SQL Query :

SELECT * FROM A INNER JOIN B ON A.KEY = B.KEY

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

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

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

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

Full Outer Join

image

SQL Query :

SELECT * FROM A FULL OUTER JOIN B ON A.KEY = B.KEY

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. This is similar to the set theory operation Union of the sets A and B, where A ∪ B, is the set of distinct elements that belong to set A or set B, or both.

Left Outer Join

image
SQL Query :

SELECT * FROM A LEFT OUTER JOIN B ON A.KEY = B.KEY WHERE B.KEY IS NULL

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. This is similar to the set theory operation Set Difference ‘A – B’, which is the set containing elements that are in A but not in B. For example : all elements of A except the elements of B.

Right Outer Join

image
SQL Query :

SELECT * FROM A RIGHT OUTER JOIN B ON A.KEY = B.KEY WHERE A.KEY IS NULL

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. This is similar to the set theory operation Set Difference ‘B – A’, which is the set containing elements that are in B but not in A. For example : all elements of B except the elements of A.

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

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, we perform the same left outer join or right outer join, then exclude the records we don't want from the right side or left side via a where clause. Which is similar to the set operation A – B ∪ B – A.

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


This content originally appeared on DEV Community and was authored by Danyson


Print Share Comment Cite Upload Translate Updates
APA

Danyson | Sciencx (2021-05-08T22:28:45+00:00) SQL Joins. Retrieved from https://www.scien.cx/2021/05/08/sql-joins/

MLA
" » SQL Joins." Danyson | Sciencx - Saturday May 8, 2021, https://www.scien.cx/2021/05/08/sql-joins/
HARVARD
Danyson | Sciencx Saturday May 8, 2021 » SQL Joins., viewed ,<https://www.scien.cx/2021/05/08/sql-joins/>
VANCOUVER
Danyson | Sciencx - » SQL Joins. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/05/08/sql-joins/
CHICAGO
" » SQL Joins." Danyson | Sciencx - Accessed . https://www.scien.cx/2021/05/08/sql-joins/
IEEE
" » SQL Joins." Danyson | Sciencx [Online]. Available: https://www.scien.cx/2021/05/08/sql-joins/. [Accessed: ]
rf:citation
» SQL Joins | Danyson | Sciencx | https://www.scien.cx/2021/05/08/sql-joins/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.