Sobes.tech
Junior — Middle

Can you explain the differences between various types of table joins in SQL?

sobes.tech AI

Answer from AI

In SQL, there are several main types of joins that allow you to combine data from two or more tables:

  • INNER JOIN — returns only the rows where there is a match in both tables.
  • LEFT JOIN (LEFT OUTER JOIN) — returns all rows from the left table and the matching rows from the right; if there is no match, NULLs are used for the right table.
  • RIGHT JOIN (RIGHT OUTER JOIN) — returns all rows from the right table and the matching rows from the left.
  • FULL JOIN (FULL OUTER JOIN) — returns all rows from both tables, filling in NULLs where there are no matches.
  • CROSS JOIN — the Cartesian product of two tables, returning all possible combinations of rows.

Example of INNER JOIN:

SELECT employees.name, departments.name
FROM employees
INNER JOIN departments ON employees.department_id = departments.id;

This query will return the names of employees and their department names only for those employees who have a department.

Can you explain the differences between various types… - sobes.tech