Sobes.tech
Junior — Middle

Can you explain the different types of joins in SQL and their features?

sobes.tech AI

Answer from AI

In SQL, there are several main types of joins that allow combining 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 table. If there is no match, NULLs are used for columns from the right table.
  • RIGHT JOIN (RIGHT OUTER JOIN) — returns all rows from the right table and the matching rows from the left table. If no match, NULLs are used for columns from the left table.
  • FULL JOIN (FULL OUTER JOIN) — returns all rows from both tables, with NULLs in places where there is no match.
  • CROSS JOIN — Cartesian product, combines each row of the first table with every row of the second.

Example of INNER JOIN:

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

This query will return employee names and their departments only if the employee has a corresponding department.

Can you explain the different types of joins in SQL… - sobes.tech