DEV Community

Cover image for SQL Join explained!
Davide Santangelo
Davide Santangelo

Posted on

SQL Join explained!

SQL JOIN is a type of clause that is used to combine rows from two or more tables based on a related column between them. The purpose of a JOIN clause is to retrieve data from multiple tables and combine it into a single result set that can be used in various operations, such as selecting, updating, or deleting data.

There are several types of JOIN clauses in SQL, each with its own specific purpose and syntax. The most commonly used JOIN clauses are INNER JOIN, OUTER JOIN, and CROSS JOIN.

INNER JOIN is the most basic type of JOIN clause and is used to combine rows from two or more tables that have a matching column. For example, if we have a "customers" table and an "orders" table, we can use an INNER JOIN to combine the two tables on the "customer_id" column, which is shared by both tables. This will return only the rows from both tables where the "customer_id" value is the same.

OUTER JOIN is a type of JOIN clause that is used to combine rows from two or more tables, even if they do not have a matching column. There are two types of OUTER JOIN: LEFT OUTER JOIN and RIGHT OUTER JOIN. LEFT OUTER JOIN combines rows from the left table with rows from the right table, and includes all rows from the left table, even if they do not have a matching row in the right table. RIGHT OUTER JOIN is the opposite, and includes all rows from the right table, even if they do not have a matching row in the left table.

CROSS JOIN is a type of JOIN clause that is used to combine every row from one table with every row from another table. This type of JOIN does not require a common column between the tables, and will create a new, combined table that contains all possible combinations of rows from the original tables.

In summary, SQL JOIN clauses are an essential part of SQL and are used to combine rows from multiple tables into a single result set. There are several different types of JOIN clauses, each with its own specific purpose and syntax, and they can be used to perform a variety of operations on data from multiple tables.

Top comments (1)

Collapse
 
linuxguist profile image
Nathan S.R.

Thanks for this very useful post. I just wanted to add that, there is a very easy way now, to test all the SQLs described here, using the free & portable tools, mentioned in my latest post here : dev.to/linuxguist/learn-sql-quickl...