Overview
Joins in SQL are utilized to combine data from two or more tables. There are four essential sorts of SQL joins: inner, left, right, and full. Inner Joins return only rows with coordinating values in both tables, Left Joins return all rows from the left table, Right Joins return all rows from the proper table, and Full Joins return all rows from both tables in any case of matches.
Different Types of SQL JOINs
Krishna was a SQL engineer who had just begun and had a new tech startup job. He was energized to be part of the group but had a part to learn about the language. One of the primary subjects his boss had him learn was the essentials of joins in SQL. Krishna had a difficult time understanding the concept to begin with. He spent hours perusing articles and observing instructional exercises but still felt confounded. Let's make things less complex for him.
The four fundamental sorts of SQL JOINs are Inner Join, Left Join, Right Join, and Full Join.
Syntax
SELECT column_name(s)
FROM table1 INNER JOIN table2 ON table1.column_name = table2.column_name;
In Inner join returns all the rows from both tables that match the specified criteria based on a common column value. It is also known as an equi-join.
SELECT column_name(s)
FROM table1 LEFT JOIN table2 ON table1.column_name = table2.column_name;
In Left join, returns all the rows from the left table (table1), even if there are no matches in the right table (table2). If there are no matches, NULL values will be returned in the columns of the right table.
SELECT column_name(s)
FROM table1 RIGHT JOIN table2 ON table1.column_name = table2.column_name;
In Right join returns all the rows from the right table (table2), even if there are no matches in the left table (table1). If there are no matches, NULL values will be returned in the columns of the left table.
SELECT column_name(s)
FROM table1 FULL OUTER JOIN table2 ON table1.column_name = table2.column_name;
In Full outer join returns all the rows from both tables, even if there are no matches in either table. If there are no matches, NULL values will be returned in the columns of the joined tables.
Examples
CustomerID | Name | OrderID |
---|---|---|
1 | Bob | 5 |
2 | Mary | 3 |
2 | Mary | 4 |
3 | Steve | 6 |
4 | Joe | 1 |
4 | Joe | 2 |
SELECT c.CustomerID, [c.Name](<http://c.name/>), o.OrderID
FROM Customers c
INNER JOIN Orders o
ON c.CustomerID = o.CustomerID;
This query will return all the customers who have placed orders, along with the OrderID of each order.
CustomerID | Name | OrderID |
---|---|---|
1 | John Smith | 10248 |
2 | Jane Doe | 10249 |
3 | Joe Bloggs | 10250 |
4 | Amy Jones | 10251 |
5 | Sue Brown | NULL |
SELECT c.CustomerID, [c.Name](<http://c.name/>), o.OrderID
FROM Customers c
LEFT JOIN Orders o
ON c.CustomerID = o.CustomerID;
This query will return all the customers, along with the OrderID of each order (if any). If a customer has not placed any orders yet, NULL values will be returned in the "OrderID" column.
CustomerID | Name | OrderID |
---|---|---|
12345 | John | 45678 |
54321 | Jane | 56789 |
SELECT c.CustomerID, [c.Name](<http://c.name/>), o.OrderID
FROM Customers c
RIGHT JOIN Orders o
ON c.CustomerID = o.CustomerID;
This query will return all the orders, along with the CustomerID of each customer (if any). NULL values will be returned in the "CustomerID" column if an order has no customer.
CustomerID | Name | OrderID |
---|---|---|
1 | John | 1 |
2 | Jane | 2 |
3 | Joe | 3 |
4 | Tom | 4 |
5 | Sally | 5 |
SELECT c.CustomerID, [c.Name](<http://c.name/>), o.OrderID
FROM Customers c
FULL OUTER JOIN Orders o
ON c.CustomerID = o.CustomerID;
This query will return all the clients and orders, besides the CustomerID and OrderID of each (in case any). In the event that a client has not set any orders, however, or in the event that an order has no client, Null values will be returned within the individual columns.
Conclusion
Krishna worked hard to ace the basics of SQL joins, which paid off. After investing hours, considering, and practicing, he finally got the hang of it. His boss was awed by his advancement and gave him some more challenging assignments to work on.
Krishna was presently sure in his capacities and delighted in the challenge of learning modern things. He felt more secure in his job and was looking forward to what the long term would bring.
Key takeaways
Quiz
Answer: b. Outer Join
Answer: c. Left Join
Answer: d. Right Join
Answer: a. Inner Join
Top Tutorials
Related Articles