SQL all kinds of join queries

All kinds of SQL Queries

Type 1: INNER JOIN - only where both tables match

1.) INNER JOIN aka JOIN

1
2
3
SELECT *
FROM table1 as a (INNER) JOIN table2 as b
ON a.id = b.id;

Type 2: OUTER JOINS where either one or both tables match

1.) LEFT OUTER JOIN aka LEFT JOIN

1
2
3
SELECT *
FROM table1 as a LEFT (OUTER) JOIN table2 as b
ON a.id = b.id;

2.) RIGHT OUTER JOIN aka RIGHT JOIN

1
2
3
SELECT *
FROM table1 as a RIGHT (OUTER) JOIN table2 as b
ON a.id = b.id;

3.) FULL OUTER JOIN aka FULL JOIN
(supported depending on what database program)

1
2
3
SELECT *
FROM table1 as a FULL OUTER JOIN table2 as b
ON a.id = b.id;

Type 3: CROSS JOIN - Cartesian product(all possible combos of each table)

(supported depending on what database program)

1
2
SELECT *
FROM table1 as a CROSS JOIN table2 as b;

Useful Links

stackoverflow on SQL

Cross Join & Self Join

Share Comments