Returns only matching rows from both tables.
SELECT *
FROM orders
INNER JOIN customers
ON orders.customer_id = customers.id;
Returns all rows from the left table, and matched rows from the right table.
SELECT *
FROM customers
LEFT JOIN orders
ON customers.id = orders.customer_id;
Returns all rows from the right table, and matched rows from the left table.
SELECT *
FROM orders
RIGHT JOIN customers
ON orders.customer_id = customers.id;
Returns all rows when there is a match in one of the tables.
SELECT *
FROM customers
FULL OUTER JOIN orders
ON customers.id = orders.customer_id;
Returns the Cartesian product of both tables.
SELECT *
FROM products
CROSS JOIN categories;
Joins a table to itself.
SELECT A.name AS Employee, B.name AS Manager
FROM employees A
JOIN employees B
ON A.manager_id = B.id;
Simplified join syntax when columns have the same name.
SELECT *
FROM orders
JOIN customers
USING (customer_id);
Automatically joins on all columns with the same name.
SELECT *
FROM employees
NATURAL JOIN departments;
-- ON is for join condition
SELECT *
FROM orders
JOIN customers
ON orders.customer_id = customers.id;
-- WHERE filters results after join
SELECT *
FROM orders
JOIN customers
ON orders.customer_id = customers.id
WHERE customers.country = 'Germany';
USING
or ON
over NATURAL JOIN for clarity.