Question №22
Remaining:

Explain the Difference Between INNER JOIN and OUTER JOIN

Sample Answer

Show Answer by Default

INNER JOIN:

  • Returns only records that have matching entries in both joined tables.
  • If there is no match, the record is not included in the result.

Example:

MySQL 8.4
SELECT *
FROM employees e
INNER JOIN departments d ON e.department_id = d.id;

OUTER JOIN:

  • Returns matching records, as well as records from one table that do not have a match in the other table.

Types of OUTER JOIN:

LEFT OUTER JOIN (LEFT JOIN):

  • Returns all records from the left table and the matching records from the right table.
  • If there is no match, the columns from the right table will be NULL.

Example:

MySQL 8.4
SELECT *
FROM employees e
LEFT JOIN departments d ON e.department_id = d.id;

RIGHT OUTER JOIN (RIGHT JOIN):

  • Returns all records from the right table and the matching records from the left table.
  • If there is no match, the columns from the left table will be NULL.

Example:

MySQL 8.4
SELECT *
FROM employees e
RIGHT JOIN departments d ON e.department_id = d.id;

FULL OUTER JOIN (FULL JOIN):

  • Returns all records from both tables, combining the matching rows.
  • If a row has no match, the columns of the other table will be NULL.
  • MySQL doesn't support FULL OUTER JOIN: combine a LEFT JOIN and a RIGHT JOIN with UNION.
MySQL 8.4
SELECT *
FROM employees e
FULL OUTER JOIN departments d ON e.department_id = d.id;