Categories: DBMS

SQL JOIN

What is an SQL Join?

The SQL Join clause is used to combine rows from two or more related tables into a single result set based on a common column or columns, which is typically established through foreign key constraints.

The “on” keyword is used to specify the join condition, which specifies the relationship between the tables being joined.

Types Of Joins:

There are 4 different types of SQL joins:

  • SQL INNER JOIN (sometimes called a simple join)
  • SQL LEFT OUTER JOIN (sometimes called LEFT JOIN)
  • SQL RIGHT OUTER JOIN (sometimes called RIGHT JOIN)
  • SQL FULL OUTER JOIN (sometimes called FULL JOIN)

Note: A table can also join to itself, which is known as a Self Join.

INNER JOIN (simple join):

As long as the condition is met, the INNER JOIN keyword selects all rows from both tables. This keyword will generate the result set by combining all rows from both tables that satisfy the condition, i.e. the value of the common field is the same.

Syntax:

SELECT columns
FROM table1 
INNER JOIN table2
ON table1.column = table2.column;

Example:

Consider a product table,

IDITEM
1Ball
2Chair
3Board
4Desk

and the product_info table,

IDQUANTITY
123
290
305
812

Query for Inner Join:

SELECT * from product INNER JOIN product_info where product.ID = product_info.ID;

Output:

IDITEMIDQUANTITY
1Ball123
2Chair290
3Board305
LEFT JOIN(left outer join):

This join returns all rows from the table on the left side of the join and matches rows from the table on the right side of the join. The result set will contain null for the rows for which there is no matching row on the right side. LEFT JOIN is also known as LEFT OUTER JOIN.

Syntax:

SELECT columns
FROM table1
LEFT [OUTER] JOIN table2
ON table1.column = table2.column;

In some databases, the OUTER keyword is omitted and written simply as LEFT JOIN.

Example:

Considering the product table

Query for Left Outer Join:

SELECT * FROM class LEFT OUTER JOIN product_info ON (product.ID = product_info.ID);

Output:

IDITEMIDQUANTITY
1Ball123
2Chair290
3Board305
4Desknullnull
RIGHT JOIN(right outer join):

RIGHT JOIN is similar to LEFT JOIN. This join returns all rows from the table on the right side of the join as well as matching rows from the table on the left side of the join. The result set will contain null for the rows for which there is no matching row on the left side. RIGHT JOIN is also known as RIGHT OUTER JOIN.

Syntax:

SELECT columns
FROM table1
RIGHT [OUTER] JOIN table2
ON table1.column = table2.column;

In some databases, the OUTER keyword is omitted and written simply as LEFT JOIN.

Example:

Considering the product table

Query for Right Outer Join:

SELECT * FROM class RIGHT OUTER JOIN product_info ON (product.ID = product_info.ID);

Output:

IDITEMIDQUANTITY
1Ball123
2Chair290
3Board305
nullnull812
FULL JOIN(full outer join):

FULL JOIN generates the result set by combining the results of both LEFT JOIN and RIGHT JOIN. The result set will include all the rows from both tables. The result set will contain NULL values for rows without a match.

Syntax:

SELECT columns
FROM table1
FULL [OUTER] JOIN table2
ON table1.column = table2.column;

In some databases, the OUTER keyword is omitted and written simply as LEFT JOIN.

Example:

Considering the product table

Query for Right Outer Join:

SELECT * FROM class FULL OUTER JOIN product_info ON (product.ID = product_info.ID);

Output:

IDITEMIDQUANTITY
1Ball123
2Chair290
3Board305
4Desknullnull
nullnull812

Note: also read about SQL Constraints

Follow Me

Please follow me to read my latest post on programming and technology if you like my post.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Share
Published by
Rabecca Fatima

Recent Posts

Generate Parenthesis | Intuition + Code | Recursion Tree | Backtracking | Java

Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…

3 months ago

Square Root of Integer

Given an integer A. Compute and return the square root of A. If A is…

1 year ago

Build Array From Permutation

Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…

1 year ago

DSA: Heap

A heap is a specialized tree-based data structure that satisfies the heap property. It is…

2 years ago

DSA: Trie

What is a Trie in DSA? A trie, often known as a prefix tree, is…

2 years ago

Trees: Lowest Common Ancestor

What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…

2 years ago