Sobes.tech
Junior — Middle

What types of table joins exist in relational databases?

sobes.tech AI

Answer from AI

In relational databases, there are several main types of joins that allow combining data from two or more tables:

  • INNER JOIN — returns only the records that have matches in both tables.
  • LEFT JOIN (LEFT OUTER JOIN) — returns all records from the left table and the matching records from the right; if there are no matches, the right table fields will be NULL.
  • RIGHT JOIN (RIGHT OUTER JOIN) — conversely, returns all records from the right table and the matching records from the left.
  • FULL JOIN (FULL OUTER JOIN) — returns all records from both tables, filling with NULL where there are no matches.
  • CROSS JOIN — the Cartesian product of two tables, returning all possible combinations of rows.

Example of INNER JOIN:

SELECT users.name, orders.id
FROM users
INNER JOIN orders ON users.id = orders.user_id;

This query will return users with their orders only if the user has an order.