Sobes.tech
Junior — Middle

Selgitage erinevust INNER JOIN ja OUTER JOIN vahel SQL-is ning millistes olukordades igaüks neist kasutatakse.

sobes.tech AI

Vastus AI-lt

INNER JOIN returns only the rows that have matches in both tables according to the join condition.

OUTER JOIN returns all rows from one table and the matching rows from the other. If there are no matches, the missing data points will be NULL.

Types of OUTER JOIN:

  • LEFT OUTER JOIN — all rows from the left table + matching rows from the right.
  • RIGHT OUTER JOIN — all rows from the right table + matching rows from the left.
  • FULL OUTER JOIN — all rows from both tables, with NULL where there are no matches.

Usage examples:

  • INNER JOIN — when only related data is needed, e.g., orders with existing customers.
  • LEFT OUTER JOIN — when you want to get all customers, including those without orders.

SQL example:

SELECT customers.name, orders.id
FROM customers
LEFT JOIN orders ON customers.id = orders.customer_id;

This query will return all customers and their orders if they exist, otherwise NULL for orders.