Mastering SQL Joins
1 INNER JOIN LEFT JOIN 6
2 RIGHT JOIN SELF JOIN 7
3 CROSS JOIN FULL JOIN 8
4 NATURAL JOIN SEMI JOIN 9
5 ANTI JOIN THETA JOIN 10
Usman Shahbaz
INNER JOIN
Returns rows where there is a match in both
tables.
Only rows that meet the join condition are
included in the result.
Syntax:
SELECT columns
FROM table1
INNER JOIN table2 ON table1.common_column =
table2.common_column;
LEFT JOIN
Returns all rows from the left table and the
matched rows from the right table.
If there's no match, NULL values are returned for
columns from the right table.
Syntax:
SELECT columns
FROM table1
LEFT JOIN table2 ON table1.common_column =
table2.common_column;
RIGHT JOIN
Returns all rows from the right table and the
matched rows from the left table.
If there's no match, NULL values are returned for
columns from the left table.
Syntax:
SELECT columns
FROM table1
RIGHT JOIN table2 ON table1.common_column =
table2.common_column;
FULL JOIN
Returns rows when there is a match in either the
left or right table.
Rows without a match in either table will have
NULLs for columns from the other table.
Syntax:
SELECT columns
FROM table1
FULL JOIN table2 ON table1.common_column =
table2.common_column;
CROSS JOIN
Returns the Cartesian product of both tables (all
possible combinations of rows).
No join condition is required, and it is not
commonly used unless specifically needed.
Syntax:
SELECT columns
FROM table1
CROSS JOIN table2;
SELF JOIN
Joins a table to itself.
Useful for comparing rows within the same table,
often with an alias to differentiate instances of
the table.
Syntax:
SELECT [Link], [Link]
FROM table_name a, table_name b
WHERE a.common_column = b.common_column;
SEMI JOIN
Returns rows from the left table where there is at
least one matching row in the right table.
It doesn’t return columns from the right table,
only filters the left table based on the right table.
Syntax:
SELECT columns
FROM table1
WHERE EXISTS (SELECT 1 FROM table2 WHERE
table1.common_column =
table2.common_column);
NATURAL JOIN
Combines tables based on all columns with the
same name and data type in both tables.
Automatically detects columns with the same
names in both tables, so you don’t need to specify
the columns explicitly.
Syntax:
SELECT columns
FROM table1
NATURAL JOIN table2;
ANTI JOIN
Returns rows from the left table that do not have a
matching row in the right table.
Like the semi join, it only returns rows from the left
table, but here it excludes matches from the right
table.
Syntax:
SELECT columns
FROM table1
WHERE NOT EXISTS (SELECT 1 FROM table2
WHERE table1.common_column =
table2.common_column);
THETA JOIN
A generalized join where the join condition is any
comparison operator (=, <, >, <=, >=, <>).
Unlike an inner or outer join, it is not restricted to
equality conditions.
Syntax:
SELECT columns
FROM table1, table2
WHERE [Link] > [Link];
keep in touch
I enjoy
connecting
with you