RIGHT JOIN and FULL JOIN

A RIGHT JOIN keeps every row of the right table. A FULL JOIN keeps unmatched rows from both tables.

Syntax

SELECT … FROM a RIGHT JOIN b ON a.key = b.key;
SELECT … FROM a FULL JOIN b ON a.key = b.key;
  • Where a row has no match, the columns from the other table are NULL.

Example

SELECT c.last_name, o.order_id FROM customers AS c FULL JOIN orders AS o ON o.customer_id = c.customer_id;

Every order and every customer; unmatched sides show NULL.

Tables used: customers, orders. Try it in a lesson below.

Common mistakes

  • A condition on the right table in WHERE removes the NULL rows. Put it in ON instead.

Practise it