Manuel Gentile

Back to SQL Course

Joins

[Inner] Join

SELECT *
FROM T1
JOIN T2
ON T1.ID = T2.ID;

Specifies a join between two tables with an explicit join clause.

It’s equal to have a query with WHERE clause.

SELECT * FROM T1, T2 WHERE T1.ID = T2.ID;

It can be simplified with USING clause.

SELECT * FROM T1 JOIN T2 USING (ID);

Left Join

SELECT *
FROM T1
LEFT JOIN T2
ON T1.ID = T2.ID;

Specifies a join between two tables with an explicit join clause, preserving unmatched rows from T1 table.

SELECT *
FROM T1
LEFT JOIN T2
ON T1.ID = T2.ID
WHERE T2.ID IS NULL;

Right Join

SELECT *
FROM T1
RIGHT JOIN T2
ON T1.ID = T2.ID;

Specifies a join between two tables with an explicit join clause, preserving unmatched rows from T2 table.

SELECT *
FROM T1
RIGHT JOIN T2
ON T1.ID = T2.ID
WHERE T1.ID IS NULL;

Full [outer] Join

SELECT *
FROM T1
FULL [OUTER] JOIN T2
ON T1.ID = T2.ID;

Specifies a join between two tables with an explicit join clause, preserving unmatched rows from T2 table.

SELECT *
FROM T1
FULL [OUTER] JOIN T2
ON T1.ID = T2.ID
WHERE T1.ID IS NULL OR T2.ID IS NULL;

Back to SQL Course


Let’s connect

If you want to learn more about the topic, connect or send me a DM.

Website