VennDiagram MySQL Joins
VennDiagram MySQL Joins
VennDiagram MySQL Joins
SELECT * SELECT *
FROM Table1 t1 FROM Table1 t1
Table 1 Table 2
LEFT OUTER JOIN Table2 t2 Table 1 Table 2 RIGHT OUTER JOIN Table2 t2
ON t1.fk = t2.id; ON t1.fk = t2.id;
LEFT OUTER JOIN RIGHT OUTER JOIN
SELECT * SELECT *
FROM Table1 t1 FROM Table1 t1
WHERE EXISTS (SELECT 1 Table 1 Table 2 WHERE NOT EXISTS (SELECT 1
Table 1 Table 2
FROM Table2 t2 FROM Table2 t2
WHERE t1.fk = t2.id WHERE t1.fk = t2.id
); );
SEMI JOIN – Similar to INNER JOIN, with less duplication. ANTI SEMI JOIN
SELECT * SELECT *
FROM Table1 t1 FROM Table1 t1
Table 1 Table 2 LEFT OUTER JOIN Table2 t2 Table 1 Table 2 RIGHT OUTER JOIN Table2 t2
ON t1.fk = t2.id ON t1.fk = t2.id
WHERE t2.id is null; WHERE t1.fk is null;
LEFT OUTER JOIN with exclusion RIGHT OUTER JOIN with exclusion
SELECT * SELECT *
Table 3 Table 3
FROM Table1 t1 FROM Table1 t1
INNER JOIN Table2 t2 LEFT OUTER JOIN Table2 t2
Table 1 Table 2 ON t1.fk = t2.id Table 1 Table 2 ON t1.fk = t2.id
INNER JOIN Table3 t3 LEFT OUTER JOIN Table3 t3
ON t1.fk_table3 = t3.id; ON t1.fk_table3 = t3.id;
Two INNER JOINs Two LEFT OUTER JOINS
SELECT *
Table 3
FROM Table1 t1
INNER JOIN Table2 t2
ON t1.fk = t2.id
Table 1 Table 2
LEFT OUTER JOIN Table3 t3
ON t1.fk_table3 = t3.id;
INNER JOIN and a LEFT OUTER JOIN