Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

What are Joins in SQL?

Joins in SQL combines rows from two or more tables. It creates a set of rows in a
temporary table.

Joins two or more tables is one of the most powerful features of relational systems.

If we required data from more than one table, then join condition is used. Rows in one
table can be joined to rows in another table according to common values existing in
corresponding columns, that is, usually primary and foreign key columns

Types of Joins in SQL


 There are many types of joins, some main join in SQL are given below

1. Equijoin
2. Non-equijoin
3. Inner Join
4. Natural Join
5. Self-join
6. Outer join
7. Cross join
1. Equi joins- The join clause is used to combine tables based on a common column
and a join condition. An equijoin is a type of join that combines tables based on
matching values in specified columns. In equi join we use = symbol in the join
condition.

Point to be remember:

The column names do not need to be the same.


The table contains repeated columns.
It is possible to perform an equi join on more than two tables.

There are two ways to use equi join in SQL:

SELECT * FROM TableName1, TableName2

WHERE TableName1.ColumnName = TableName2.ColumnName. ---OR

SELECT * FROM TableName1 JOIN TableName2

ON TableName1.ColumnName = TableName2.ColumnName.
Inner Join: INNER Join is the most frequently used JOIN. It matches the records
from both tables based on the join predicate and returns only the matched rows.
Natural Join: We have already learned that an EQUI JOIN performs a JOIN against
equality or matching column(s) values of the associated tables and an equal sign (=) is
used as comparison operator in the where clause to refer equality. The SQL
NATURAL JOIN is a type of EQUI JOIN and is structured in such a way that,
columns with same name of associate tables will appear once only.

Self-Join: A self-join is a join in which a table is joined with itself (which is also
called Unary relationships), especially when the table has a FOREIGN KEY which
references its own PRIMARY KEY.

To join a table itself means that each row of the table is combined with itself and with
every other row of the table. The self-join can be viewed as a join of two copies of the
same table. The table is not actually copied, but SQL performs the command as
though it were.

Outer Join – An OUTER JOIN doesn’t require each record in the two join tables to
have a matching record. In this type of join, the table retains each record even if no
other matching record exists.
Three types of Outer Joins are:

1. Left Outer Join


2. Right Outer Join
3. Full Outer Join

1. Left Outer Join– LEFT JOIN returns all the rows from the table on the left
even if no matching rows have been found in the table on the right. When no
matching record found in the table on the right, NULL is returned.
2. Right Outer Join- The SQL RIGHT JOIN, joins two tables and fetches rows
based on a condition, which are matching in both the tables, and the unmatched
rows will also be available from the table written after the JOIN clause.
3. Full Outer Join- In SQL the FULL OUTER JOIN combines the results of both
left and right outer joins and returns all (matched or unmatched) rows from the
tables on both sides of the join clause.
Cross join– The SQL CROSS JOIN produces a result set which is the number of rows
in the first table multiplied by the number of rows in the second table, if no WHERE
clause is used along with CROSS JOIN. This kind of result is called as Cartesian
Product. If, WHERE clause is used with CROSS JOIN, it functions like an INNER
JOIN.
An alternative way of achieving the same result is to use column names separated
by commas after SELECT and mentioning the table names involved, after a
FROM clause.

SQL Cross Join Syntax :- Select * FROM table1 CROSS JOIN table2;

You might also like